Documentation ¶
Overview ¶
Package utils provides useful features to Go's http.Client following some best practices for production, such as timeout, retries and backoff.
Index ¶
- func IsTimeoutErr(e error) bool
- func NewFormPost(url string, v url.Values, f RequestHook) (*http.Request, error)
- func NewJSONPost(url string, v interface{}, f RequestHook) (*http.Request, error)
- func ShouldRetry(statusCode int) bool
- type Backoff
- type HTTPClient
- type RequestHook
- type SafeClient
- func (c *SafeClient) DoRequest(method, url string, content []byte, maxTries int, f RequestHook) (tries, status int, body []byte, err error)
- func (c *SafeClient) PostFormWithRetry(url string, v url.Values, maxTries int, f RequestHook) (tries, status int, body []byte, err error)
- func (c *SafeClient) PostJSONWithRetry(url string, v interface{}, maxTries int, f RequestHook) (tries, status int, body []byte, err error)
- func (c *SafeClient) RequestWithClose(req *http.Request) (status int, body []byte, err error)
- func (c *SafeClient) RequestWithRetry(req *http.Request, maxTries int) (tries, status int, body []byte, err error)
- type Semaphore
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsTimeoutErr ¶
IsTimeoutErr checks if an error is timeout by cast it to net.Error.
func NewFormPost ¶
NewFormPost returns a Request with default "Content-type: text/plain".
func NewJSONPost ¶
func NewJSONPost(url string, v interface{}, f RequestHook) (*http.Request, error)
NewJSONPost returns a Request with json encoded and header set; additional headers or cookies can be set through the RequestHook.
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 client sending remote calls. It use an alternative method described in https://www.awsarchitectureblog.com/2015/03/backoff.html:
type HTTPClient ¶
type HTTPClient interface { RequestWithRetry(req *http.Request, maxTries int) (tries, status int, body []byte, err error) DoRequest(method, url string, content []byte, maxTries int, f RequestHook) (tries, status int, body []byte, err error) }
HTTPClient provides additional features upon http.Client, e.g., io Reader handle and request retry; it also normalize the HTTP response.
type RequestHook ¶
RequestHook can modify the Request anyway it wants.
type SafeClient ¶
SafeClient implements HTTPClient; it wraps a http.Client underneath (safe for concurrent use by multiple goroutines).
func (*SafeClient) DoRequest ¶
func (c *SafeClient) DoRequest(method, url string, content []byte, maxTries int, f RequestHook) (tries, status int, body []byte, err error)
DoRequest is the generalized version of RequestWithRetry that initialize a Request each time to ensure Body get consumed. Additional headers or cookies can be set through the RequestHook.
func (*SafeClient) PostFormWithRetry ¶
func (c *SafeClient) PostFormWithRetry(url string, v url.Values, maxTries int, f RequestHook) (tries, status int, body []byte, err error)
PostFormWithRetry is a convenient method for form POST requests.
func (*SafeClient) PostJSONWithRetry ¶
func (c *SafeClient) PostJSONWithRetry(url string, v interface{}, maxTries int, f RequestHook) (tries, status int, body []byte, err error)
PostJSONWithRetry is a convenient method for JSON POST requests.
func (*SafeClient) RequestWithClose ¶
RequestWithClose sends the request and returns statusCode and raw body. It reads and closes Response.Body, return any error occurs.
func (*SafeClient) RequestWithRetry ¶
func (c *SafeClient) RequestWithRetry(req *http.Request, maxTries int) (tries, status int, body []byte, err error)
RequestWithRetry wraps RequestWithClose and exponential-backoff retries in following conditions: 1. timeout error occurs (mostly client-side); 2. server-side should-retry statusCode returned. It returns the last response if tries run out. NOTICE: retry works for request with no body only before go1.9.
type Semaphore ¶
type Semaphore interface { // Obtain puts one into the semaphore, returns true if succeeds. // It blocks utils succeeds or the context cancelled. // Obtaining from a closed semaphore should return false. Obtain(context.Context) bool // Release takes one from the semaphore, returns true if succeeds. // It should never blocks. Release() bool // Capacity returns semaphore's max concurrent resources. Capacity() int // Count returns semaphore's current used resources. Count() int // Close stops obtaining resources from semaphore, // it makes Obtain() return false ever since. Close() // Closed tells if semaphore is closed. Closed() bool }
Semaphore is bounded resources abstraction. Ref: https://github.com/golang/go/wiki/BoundingResourceUse
func NewSemaphore ¶
NewSemaphore returns an internal semaphore. This is the exported interface for using semaphore.