forked from skip-mev/connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
60 lines (49 loc) · 1.5 KB
/
provider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package oracle
import (
"context"
"fmt"
"math/big"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
providertypes "github.com/skip-mev/slinky/providers/types"
oracletypes "github.com/skip-mev/slinky/x/oracle/types"
)
var CtxErrors = map[error]struct{}{
context.Canceled: {},
context.DeadlineExceeded: {},
}
// StartProviders starts all providers.
func (o *OracleImpl) StartProviders(ctx context.Context) {
providerGroup, ctx := errgroup.WithContext(ctx)
providerGroup.SetLimit(len(o.providers))
for _, p := range o.providers {
providerGroup.Go(o.execProviderFn(ctx, p))
}
o.providerCh <- providerGroup.Wait()
close(o.providerCh)
}
// execProvider executes a given provider. The provider continues
// to concurrently run until the context is canceled.
func (o *OracleImpl) execProviderFn(
ctx context.Context,
p providertypes.Provider[oracletypes.CurrencyPair, *big.Int],
) func() error {
return func() error {
defer func() {
if r := recover(); r != nil {
o.logger.Error("recovered from panic", zap.Error(fmt.Errorf("%v", r)))
}
}()
o.logger.Info("starting provider routine", zap.String("name", p.Name()))
err := p.Start(ctx)
o.logger.Info("provider exiting", zap.String("name", p.Name()), zap.Error(err))
// If the context is canceled, or the deadline is exceeded,
// we want to exit the provider and trigger the error group
// to exit for all providers.
if _, ok := CtxErrors[err]; ok {
return err
}
// Otherwise, we gracefully exit the go routine.
return nil
}
}