infra/op-ufm/pkg/service/service.go

112 lines
2.9 KiB
Go
Raw Normal View History

package service
import (
"context"
2023-07-18 20:51:22 +03:00
"net"
2023-07-15 01:17:02 +03:00
"github.com/ethereum-optimism/optimism/op-ufm/pkg/config"
"github.com/ethereum-optimism/optimism/op-ufm/pkg/metrics"
"github.com/ethereum-optimism/optimism/op-ufm/pkg/provider"
"github.com/ethereum/go-ethereum/log"
)
type Service struct {
Config *config.Config
2023-07-15 00:08:02 +03:00
Healthz *HealthzServer
Metrics *MetricsServer
Providers map[string]*provider.Provider
}
func New(cfg *config.Config) *Service {
s := &Service{
Config: cfg,
2023-07-15 00:08:02 +03:00
Healthz: &HealthzServer{},
Metrics: &MetricsServer{},
Providers: make(map[string]*provider.Provider, len(cfg.Providers)),
}
return s
}
func (s *Service) Start(ctx context.Context) {
log.Info("service starting")
if s.Config.Healthz.Enabled {
2023-07-18 20:51:22 +03:00
addr := net.JoinHostPort(s.Config.Healthz.Host, s.Config.Healthz.Port)
2023-08-31 23:31:34 +03:00
log.Info("starting healthz server",
"addr", addr)
go func() {
2023-07-18 20:51:22 +03:00
if err := s.Healthz.Start(ctx, addr); err != nil {
2023-08-31 23:31:34 +03:00
log.Error("error starting healthz server",
"err", err)
}
}()
}
2023-07-15 00:08:02 +03:00
metrics.Debug = s.Config.Metrics.Debug
if s.Config.Metrics.Enabled {
2023-07-18 20:51:22 +03:00
addr := net.JoinHostPort(s.Config.Metrics.Host, s.Config.Metrics.Port)
2023-08-31 23:31:34 +03:00
log.Info("starting metrics server",
"addr", addr)
go func() {
2023-07-15 00:08:02 +03:00
if err := s.Metrics.Start(ctx, addr); err != nil {
2023-08-31 23:31:34 +03:00
log.Error("error starting metrics server",
"err", err)
}
}()
}
2023-07-12 22:20:23 +03:00
// map networks to its providers
networks := make(map[string][]string)
for name, providerConfig := range s.Config.Providers {
networks[providerConfig.Network] = append(networks[providerConfig.Network], name)
}
txpool := &provider.TransactionPool{}
for name, providers := range networks {
if len(providers) == 1 {
2023-08-31 23:31:34 +03:00
log.Warn("can't measure first seen for network, please another provider",
"network", name)
2023-07-12 22:20:23 +03:00
}
(*txpool)[name] = &provider.NetworkTransactionPool{}
(*txpool)[name].Transactions = make(map[string]*provider.TransactionState)
// set expected number of providers for this network
// -1 since we don't wait for acking from the same provider
(*txpool)[name].Expected = len(providers) - 1
2023-07-12 22:20:23 +03:00
}
for name, providerConfig := range s.Config.Providers {
2023-07-12 22:20:23 +03:00
s.Providers[name] = provider.New(name,
providerConfig,
&s.Config.Signer,
s.Config.Wallets[providerConfig.Wallet],
(*txpool)[providerConfig.Network])
s.Providers[name].Start(ctx)
2023-08-31 23:31:34 +03:00
log.Info("provider started",
"provider", name)
}
log.Info("service started")
}
func (s *Service) Shutdown() {
log.Info("service shutting down")
if s.Config.Healthz.Enabled {
if err := s.Healthz.Shutdown(); err != nil {
log.Error("Error shutting down healthz server", err)
}
log.Info("healthz stopped")
}
2023-07-15 00:08:02 +03:00
if s.Config.Metrics.Enabled {
if err := s.Metrics.Shutdown(); err != nil {
log.Error("Error shutting down metrics server", err)
}
2023-07-15 00:08:02 +03:00
log.Info("metrics stopped")
}
for name, provider := range s.Providers {
provider.Shutdown()
2023-08-31 23:31:34 +03:00
log.Info("provider stopped",
"provider", name)
}
log.Info("service stopped")
}