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

73 lines
1.4 KiB
Go
Raw Normal View History

package provider
import (
"context"
"time"
2023-07-15 01:17:02 +03:00
"github.com/ethereum-optimism/optimism/op-ufm/pkg/config"
)
type Provider struct {
2023-07-12 19:49:37 +03:00
name string
config *config.ProviderConfig
signerConfig *config.SignerServiceConfig
walletConfig *config.WalletConfig
2023-07-12 22:20:23 +03:00
txPool *NetworkTransactionPool
2023-07-15 00:08:02 +03:00
cancelFunc context.CancelFunc
}
2023-07-12 19:49:37 +03:00
func New(name string, cfg *config.ProviderConfig,
signerConfig *config.SignerServiceConfig,
2023-07-12 22:20:23 +03:00
walletConfig *config.WalletConfig,
txPool *NetworkTransactionPool) *Provider {
p := &Provider{
2023-07-12 19:49:37 +03:00
name: name,
config: cfg,
signerConfig: signerConfig,
walletConfig: walletConfig,
2023-07-12 22:20:23 +03:00
txPool: txPool,
}
return p
}
func (p *Provider) Start(ctx context.Context) {
providerCtx, cancelFunc := context.WithCancel(ctx)
p.cancelFunc = cancelFunc
2023-07-15 00:08:02 +03:00
schedule(providerCtx, time.Duration(p.config.ReadInterval), p.Heartbeat)
if !p.config.ReadOnly {
schedule(providerCtx, time.Duration(p.config.SendInterval), p.RoundTrip)
}
}
func (p *Provider) Shutdown() {
if p.cancelFunc != nil {
p.cancelFunc()
}
}
func (p *Provider) Name() string {
return p.name
}
func (p *Provider) URL() string {
return p.config.URL
}
func schedule(ctx context.Context, interval time.Duration, handler func(ctx context.Context)) {
go func() {
for {
timer := time.NewTimer(interval)
handler(ctx)
select {
case <-timer.C:
case <-ctx.Done():
timer.Stop()
return
}
}
}()
}