web

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2018 License: MIT Imports: 10 Imported by: 1

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 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

func DownloadFile(url, file string) error

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 IsTimeoutErr

func IsTimeoutErr(e error) bool

IsTimeoutErr checks if the error is a timeout.

func NewJSONPost

func NewJSONPost(url string, v interface{}) (*http.Request, error)

NewJSONPost returns a Request with json encoded and header set.

func ParseJSONRequest added in v0.3.0

func ParseJSONRequest(r *http.Request, v interface{}) error

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

func RequestWithClose(cl *http.Client, req *http.Request) (status int, body []byte, err error)

RequestWithClose sends the request and returns statusCode and raw body. It reads and closes Response.Body, return any error occurs.

func ShouldRetry

func ShouldRetry(statusCode int) bool

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.

func (*Backoff) Next

func (b *Backoff) Next(previous int) int

Next returns the next sleep time computed by the previous one; the *Decorrelated Jitter* is: sleep = min(cap, random_between(base, sleep * 3)).

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL