Documentation
¶
Overview ¶
Package web provides useful features to Go's http.Client following some best practices for production, such as timeout, retries and backoff.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewJSONPost ¶
NewJSONPost returns a Request with json encoded and header set.
func RequestWithClose ¶
RequestWithClose sends the request and returns statusCode and raw body. It reads and closes Response.Body, return any error occurs.
func ShouldRetry ¶
ShouldRetry determines if the client should repeat the request without modifications at any later time; returns true for http 408 and 5xx status.
Types ¶
type Backoff ¶
type Backoff struct {
BaseSleep, MaxSleep int
}
Backoff implements the exponential backoff algorithm with jitter for performing remote calls. It use an alternative method as described in https://www.awsarchitectureblog.com/2015/03/backoff.html.
type Client ¶
type Client interface {
// Do sends the request with at most maxTries time.
// Retries happen in following conditions:
// 1. timeout error occurs;
// 2. should-retry status code is returned.
// It also normalize the HTTP response as:
// (tries, status int, body []byte, err error),
// for #requests made, status code for the final request,
// response body and error respectively.
Do(req *http.Request, maxTries int) (tries, status int, body []byte, err error)
}
Client provides additional features upon http.Client, e.g., io Reader handle and request retry with backoff.
func NewClient ¶
func NewClient(ops ...ClientOption) Client
NewClient returns a client with default setting: 1. retry on all errors; 2. http.Client set Timeout to 5s; 3. Backoff{100, 5000}.
type ClientOption ¶
type ClientOption func(*client)
ClientOption allows functional pattern options for client.
func TimeoutOnly ¶
func TimeoutOnly() ClientOption
TimeoutOnly sets the client to retry only on timeout error instead of all errors.
func WithBackoff ¶
func WithBackoff(b Backoff) ClientOption
WithBackoff substitutes the default Backoff.
func WithHTTPClient ¶
func WithHTTPClient(cl *http.Client) ClientOption
WithHTTPClient substitutes the default 5s timeout http.Client with a custom one.