refactor: http retry for retryable errors (#20)
This commit is contained in:
42
util/http.go
Normal file
42
util/http.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/go-retryablehttp"
|
||||
"github.com/rs/zerolog"
|
||||
"go.uber.org/ratelimit"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func NewRetryableHttpClient(timeout time.Duration, rl ratelimit.Limiter, log *zerolog.Logger) *http.Client {
|
||||
retryClient := retryablehttp.NewClient()
|
||||
retryClient.RetryMax = 10
|
||||
retryClient.RetryWaitMin = 1 * time.Second
|
||||
retryClient.RetryWaitMax = 10 * time.Second
|
||||
retryClient.RequestLogHook = func(l retryablehttp.Logger, request *http.Request, i int) {
|
||||
// rate limit
|
||||
if rl != nil {
|
||||
rl.Take()
|
||||
}
|
||||
|
||||
// log
|
||||
if log != nil && request != nil && request.URL != nil {
|
||||
switch i {
|
||||
case 0:
|
||||
// first
|
||||
log.Trace().
|
||||
Str("url", request.URL.String()).
|
||||
Msg("Sending request")
|
||||
default:
|
||||
// retry
|
||||
log.Debug().
|
||||
Str("url", request.URL.String()).
|
||||
Int("attempt", i).
|
||||
Msg("Retrying failed request")
|
||||
}
|
||||
}
|
||||
}
|
||||
retryClient.HTTPClient.Timeout = timeout
|
||||
retryClient.Logger = nil
|
||||
return retryClient.StandardClient()
|
||||
}
|
||||
Reference in New Issue
Block a user