Skip to content

Commit

Permalink
Miner info: use exponential retry
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Jul 31, 2024
1 parent c9569a8 commit 3e4e454
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions split-car-fetcher/miner-info.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func NewMinerInfo(
) *MinerInfoCache {
minerInfoCache := ttlcache.New[string, *MinerInfo](
ttlcache.WithTTL[string, *MinerInfo](cacheTTL),
ttlcache.WithDisableTouchOnHit[string, *MinerInfo]())
ttlcache.WithDisableTouchOnHit[string, *MinerInfo](),
)

return &MinerInfoCache{
lotusClient: lotusClient,
Expand All @@ -47,7 +48,15 @@ func (d *MinerInfoCache) GetProviderInfo(ctx context.Context, provider address.A
return file.Value(), nil
}

minerInfo, err := (&MinerInfoFetcher{Client: d.lotusClient}).GetProviderInfo(ctx, provider.String())
ctx, cancel := context.WithTimeout(ctx, d.requestTimeout)
defer cancel()
minerInfo, err := retryExponentialBackoff(ctx,
func() (*MinerInfo, error) {
return (&MinerInfoFetcher{Client: d.lotusClient}).GetProviderInfo(ctx, provider.String())
},
time.Second*2,
5,
)
if err != nil {
return nil, err
}
Expand All @@ -59,6 +68,29 @@ type MinerInfoFetcher struct {
Client jsonrpc.RPCClient
}

func retryExponentialBackoff[T any](
ctx context.Context,
fn func() (T, error),
startingBackoff time.Duration,
maxRetries int,
) (T, error) {
var err error
var out T
for i := 0; i < maxRetries; i++ {
out, err = fn()
if err == nil {
return out, nil
}
select {
case <-ctx.Done():
return out, fmt.Errorf("context done: %w; last error: %s", ctx.Err(), err)
case <-time.After(startingBackoff):
startingBackoff *= 2
}
}
return out, err
}

func (m *MinerInfoFetcher) GetProviderInfo(ctx context.Context, provider string) (*MinerInfo, error) {
minerInfo := new(MinerInfo)
err := m.Client.CallFor(ctx, minerInfo, "Filecoin.StateMinerInfo", provider, nil)
Expand Down

0 comments on commit 3e4e454

Please sign in to comment.