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

73 lines
1.4 KiB
Go
Raw Permalink Normal View History

package provider
import (
"context"
"time"
2023-07-14 15:17:02 -07:00
"github.com/ethereum-optimism/optimism/op-ufm/pkg/config"
)
type Provider struct {
2023-07-12 09:49:37 -07:00
name string
config *config.ProviderConfig
signerConfig *config.SignerServiceConfig
walletConfig *config.WalletConfig
2023-07-12 12:20:23 -07:00
txPool *NetworkTransactionPool
2023-07-14 14:08:02 -07:00
cancelFunc context.CancelFunc
}
2023-07-12 09:49:37 -07:00
func New(name string, cfg *config.ProviderConfig,
signerConfig *config.SignerServiceConfig,
2023-07-12 12:20:23 -07:00
walletConfig *config.WalletConfig,
txPool *NetworkTransactionPool) *Provider {
p := &Provider{
2023-07-12 09:49:37 -07:00
name: name,
config: cfg,
signerConfig: signerConfig,
walletConfig: walletConfig,
2023-07-12 12:20:23 -07:00
txPool: txPool,
}
return p
}
func (p *Provider) Start(ctx context.Context) {
providerCtx, cancelFunc := context.WithCancel(ctx)
p.cancelFunc = cancelFunc
2023-07-14 14:08:02 -07: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
}
}
}()
}