Simple HTTP request interceptor
- define the interceptor to use for every outgoing request
- create a request
- provide an HTTP client
- send the request with interceptor
- retry while destination service is unavailable
interceptor.New(
interceptor.Retry(serviceConfig.Client.MaxRetries),
interceptor.WithInterval(time.Duration(serviceConfig.Client.RetryInterval)),
interceptor.WhileFails(func(r *http.Response) error {
if r.StatusCode == 503 {
return errors.ServiceUnavailable("service is unavailable")
}
return nil
}),
).Then(http.DefaultClient).Do(req)
- throttle
var (
ticker = time.NewTicker(duration)
icp = interceptor.New(interceptor.Throttle(ticker.C)).Then(http.DefaultClient)
)
icp.Do(req)
icp.Do(req)
. . .
icp.Do(req)