2019-10-29 17:51:17 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-02 11:29:47 +00:00
|
|
|
"errors"
|
2022-11-25 10:20:53 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-10-29 17:51:17 +00:00
|
|
|
"net/http"
|
2023-10-11 10:24:16 +00:00
|
|
|
"sync/atomic"
|
2019-10-29 17:51:17 +00:00
|
|
|
|
2022-07-08 16:10:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap"
|
2019-10-29 17:51:17 +00:00
|
|
|
)
|
|
|
|
|
2019-12-04 06:48:32 +00:00
|
|
|
// Service serves metrics.
|
2019-10-29 17:51:17 +00:00
|
|
|
type Service struct {
|
2022-11-25 10:20:53 +00:00
|
|
|
http []*http.Server
|
2022-07-08 16:10:46 +00:00
|
|
|
config config.BasicService
|
2019-12-30 07:43:05 +00:00
|
|
|
log *zap.Logger
|
2019-12-04 06:48:32 +00:00
|
|
|
serviceType string
|
2023-10-11 10:24:16 +00:00
|
|
|
started atomic.Bool
|
2022-11-25 10:20:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService configures logger and returns new service instance.
|
|
|
|
func NewService(name string, httpServers []*http.Server, cfg config.BasicService, log *zap.Logger) *Service {
|
|
|
|
return &Service{
|
|
|
|
http: httpServers,
|
|
|
|
config: cfg,
|
|
|
|
serviceType: name,
|
|
|
|
log: log.With(zap.String("service", name)),
|
|
|
|
}
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Start runs http service with the exposed endpoint on the configured port.
|
2022-11-25 10:20:53 +00:00
|
|
|
func (ms *Service) Start() error {
|
2019-10-29 17:51:17 +00:00
|
|
|
if ms.config.Enabled {
|
2023-04-27 15:49:19 +00:00
|
|
|
if !ms.started.CompareAndSwap(false, true) {
|
2022-11-25 10:20:53 +00:00
|
|
|
ms.log.Info("service already started")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, srv := range ms.http {
|
|
|
|
ms.log.Info("starting service", zap.String("endpoint", srv.Addr))
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", srv.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to listen on %s: %w", srv.Addr, err)
|
|
|
|
}
|
|
|
|
srv.Addr = ln.Addr().String() // set Addr to the actual address
|
|
|
|
|
|
|
|
go func(s *http.Server) {
|
|
|
|
err = s.Serve(ln)
|
|
|
|
if !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
ms.log.Error("failed to start service", zap.String("endpoint", s.Addr), zap.Error(err))
|
|
|
|
}
|
|
|
|
}(srv)
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-12-30 07:43:05 +00:00
|
|
|
ms.log.Info("service hasn't started since it's disabled")
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
2022-11-25 10:20:53 +00:00
|
|
|
return nil
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ShutDown stops the service.
|
2019-10-29 17:51:17 +00:00
|
|
|
func (ms *Service) ShutDown() {
|
2022-07-26 14:19:30 +00:00
|
|
|
if !ms.config.Enabled {
|
|
|
|
return
|
|
|
|
}
|
2023-04-27 15:49:19 +00:00
|
|
|
if !ms.started.CompareAndSwap(true, false) {
|
2022-11-25 10:20:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, srv := range ms.http {
|
|
|
|
ms.log.Info("shutting down service", zap.String("endpoint", srv.Addr))
|
|
|
|
err := srv.Shutdown(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
ms.log.Error("can't shut service down", zap.String("endpoint", srv.Addr), zap.Error(err))
|
|
|
|
}
|
2019-10-29 17:51:17 +00:00
|
|
|
}
|
|
|
|
}
|