refactor: http retry for retryable errors (#20)

This commit is contained in:
l3uddz
2021-02-21 14:01:21 +00:00
committed by GitHub
parent a2848439b9
commit db9fdc97a2
21 changed files with 150 additions and 115 deletions

42
util/http.go Normal file
View 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()
}