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 ¶
- func AssertMethod(method string, h http.HandlerFunc) http.HandlerFunc
- func DownloadFile(url, file string) error
- func ErrResponse(w http.ResponseWriter, err error, code int)
- func GetLocalIP() string
- func HandleResponse(w http.ResponseWriter, data []byte, status int, err error)
- func IsTimeoutErr(e error) bool
- func NewJSONPost(url string, v interface{}) (*http.Request, error)
- func ParseJSONRequest(r *http.Request, v interface{}) error
- func QuickResponse(w http.ResponseWriter, code int)
- func RequestWithClose(cl *http.Client, req *http.Request) (status int, body []byte, err error)
- func ShouldRetry(statusCode int) bool
- type Backoff
- type Client
- type ClientOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertMethod ¶ added in v0.3.0
func AssertMethod(method string, h http.HandlerFunc) http.HandlerFunc
AssertMethod checks the request's HTTP method and response 403 if fails.
func DownloadFile ¶ added in v0.3.0
DownloadFile downloads a file from the url.
func ErrResponse ¶ added in v0.3.0
func ErrResponse(w http.ResponseWriter, err error, code int)
ErrResponse is mainly for handling non-GET response.
func GetLocalIP ¶ added in v0.3.0
func GetLocalIP() string
GetLocalIP tries to check the IP of local machine.
func HandleResponse ¶ added in v0.3.0
func HandleResponse(w http.ResponseWriter, data []byte, status int, err error)
HandleResponse is mainly for handling GET response.
func NewJSONPost ¶
NewJSONPost returns a Request with json encoded and header set.
func ParseJSONRequest ¶ added in v0.3.0
ParseJSONRequest reads & parses JSON requests.
func QuickResponse ¶ added in v0.3.0
func QuickResponse(w http.ResponseWriter, code int)
QuickResponse writes standard HTTP code & text.
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.