fix: properly setup collectible providers exponential backoff

This commit is contained in:
Dario Gabriel Lipicar 2024-02-27 15:00:20 -03:00 committed by dlipicar
parent 168398d7a5
commit 9fef2d3558
3 changed files with 33 additions and 23 deletions

View File

@ -129,14 +129,13 @@ func (o *Client) doPostWithJSON(ctx context.Context, url string, payload any) (*
} }
func (o *Client) doWithRetries(req *http.Request) (*http.Response, error) { func (o *Client) doWithRetries(req *http.Request) (*http.Response, error) {
b := backoff.ExponentialBackOff{ b := backoff.NewExponentialBackOff()
InitialInterval: time.Millisecond * 1000, b.InitialInterval = time.Millisecond * 1000
RandomizationFactor: 0.1, b.RandomizationFactor = 0.1
Multiplier: 1.5, b.Multiplier = 1.5
MaxInterval: time.Second * 32, b.MaxInterval = time.Second * 32
MaxElapsedTime: time.Second * 128, b.MaxElapsedTime = time.Second * 70
Clock: backoff.SystemClock,
}
b.Reset() b.Reset()
op := func() (*http.Response, error) { op := func() (*http.Response, error) {
@ -151,12 +150,13 @@ func (o *Client) doWithRetries(req *http.Request) (*http.Response, error) {
err = fmt.Errorf("unsuccessful request: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode)) err = fmt.Errorf("unsuccessful request: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
if resp.StatusCode == http.StatusTooManyRequests { if resp.StatusCode == http.StatusTooManyRequests {
log.Error("doWithRetries failed with http.StatusTooManyRequests", "provider", o.ID(), "elapsed time", b.GetElapsedTime(), "next backoff", b.NextBackOff())
return nil, err return nil, err
} }
return nil, backoff.Permanent(err) return nil, backoff.Permanent(err)
} }
return backoff.RetryWithData(op, &b) return backoff.RetryWithData(op, b)
} }
func (o *Client) FetchCollectibleOwnersByContractAddress(ctx context.Context, chainID walletCommon.ChainID, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) { func (o *Client) FetchCollectibleOwnersByContractAddress(ctx context.Context, chainID walletCommon.ChainID, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {

View File

@ -157,7 +157,9 @@ func (o *ClientV2) fetchAssets(ctx context.Context, chainID walletCommon.ChainID
body, err := o.client.doGetRequest(ctx, url, o.apiKey) body, err := o.client.doGetRequest(ctx, url, o.apiKey)
if err != nil { if err != nil {
o.connectionStatus.SetIsConnected(false) if ctx.Err() == nil {
o.connectionStatus.SetIsConnected(false)
}
return nil, err return nil, err
} }
o.connectionStatus.SetIsConnected(true) o.connectionStatus.SetIsConnected(true)
@ -274,7 +276,9 @@ func (o *ClientV2) fetchCollectionDataBySlug(ctx context.Context, chainID wallet
body, err := o.client.doGetRequest(ctx, url, o.apiKey) body, err := o.client.doGetRequest(ctx, url, o.apiKey)
if err != nil { if err != nil {
o.connectionStatus.SetIsConnected(false) if ctx.Err() == nil {
o.connectionStatus.SetIsConnected(false)
}
return nil, err return nil, err
} }
o.connectionStatus.SetIsConnected(true) o.connectionStatus.SetIsConnected(true)

View File

@ -143,14 +143,13 @@ func (o *Client) doPostWithJSON(ctx context.Context, url string, payload any, ap
} }
func (o *Client) doWithRetries(req *http.Request, apiKey string) (*http.Response, error) { func (o *Client) doWithRetries(req *http.Request, apiKey string) (*http.Response, error) {
b := backoff.ExponentialBackOff{ b := backoff.NewExponentialBackOff()
InitialInterval: time.Millisecond * 1000, b.InitialInterval = time.Millisecond * 1000
RandomizationFactor: 0.1, b.RandomizationFactor = 0.1
Multiplier: 1.5, b.Multiplier = 1.5
MaxInterval: time.Second * 32, b.MaxInterval = time.Second * 32
MaxElapsedTime: time.Second * 128, b.MaxElapsedTime = time.Second * 70
Clock: backoff.SystemClock,
}
b.Reset() b.Reset()
req.Header.Set("X-API-KEY", apiKey) req.Header.Set("X-API-KEY", apiKey)
@ -167,12 +166,13 @@ func (o *Client) doWithRetries(req *http.Request, apiKey string) (*http.Response
err = fmt.Errorf("unsuccessful request: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode)) err = fmt.Errorf("unsuccessful request: %d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
if resp.StatusCode == http.StatusTooManyRequests { if resp.StatusCode == http.StatusTooManyRequests {
log.Error("doWithRetries failed with http.StatusTooManyRequests", "provider", o.ID(), "elapsed time", b.GetElapsedTime(), "next backoff", b.NextBackOff())
return nil, err return nil, err
} }
return nil, backoff.Permanent(err) return nil, backoff.Permanent(err)
} }
return backoff.RetryWithData(op, &b) return backoff.RetryWithData(op, b)
} }
func (o *Client) FetchCollectibleOwnersByContractAddress(ctx context.Context, chainID walletCommon.ChainID, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) { func (o *Client) FetchCollectibleOwnersByContractAddress(ctx context.Context, chainID walletCommon.ChainID, contractAddress common.Address) (*thirdparty.CollectibleContractOwnership, error) {
@ -197,7 +197,9 @@ func (o *Client) FetchCollectibleOwnersByContractAddress(ctx context.Context, ch
resp, err := o.doQuery(ctx, url, o.getAPIKey(chainID)) resp, err := o.doQuery(ctx, url, o.getAPIKey(chainID))
if err != nil { if err != nil {
o.connectionStatus.SetIsConnected(false) if ctx.Err() == nil {
o.connectionStatus.SetIsConnected(false)
}
return nil, err return nil, err
} }
o.connectionStatus.SetIsConnected(true) o.connectionStatus.SetIsConnected(true)
@ -258,7 +260,9 @@ func (o *Client) FetchAllAssetsByOwner(ctx context.Context, chainID walletCommon
resp, err := o.doQuery(ctx, url, o.getAPIKey(chainID)) resp, err := o.doQuery(ctx, url, o.getAPIKey(chainID))
if err != nil { if err != nil {
o.connectionStatus.SetIsConnected(false) if ctx.Err() == nil {
o.connectionStatus.SetIsConnected(false)
}
return nil, err return nil, err
} }
o.connectionStatus.SetIsConnected(true) o.connectionStatus.SetIsConnected(true)
@ -398,7 +402,9 @@ func (o *Client) FetchCollectionsDataByContractID(ctx context.Context, contractI
resp, err := o.doQuery(ctx, url, o.getAPIKey(contractID.ChainID)) resp, err := o.doQuery(ctx, url, o.getAPIKey(contractID.ChainID))
if err != nil { if err != nil {
o.connectionStatus.SetIsConnected(false) if ctx.Err() == nil {
o.connectionStatus.SetIsConnected(false)
}
return nil, err return nil, err
} }
o.connectionStatus.SetIsConnected(true) o.connectionStatus.SetIsConnected(true)