httputils

package
v0.0.0-...-c6dce4a Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2020 License: Apache-2.0 Imports: 17 Imported by: 0

README

HTTP Client

HttpClient struct wrapper http client, provides some new feature.

1. Local DNS Cache

2. Retry Exponential Backoff And Jitter Strategy

Exponential Backoff and Jitter

3. Multi-Goroutine Download

4. Allow Custom Max Redirects

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultHttpClient = &HttpClient{client: http.DefaultClient}
View Source
var DefaultHttpClientConfig = &HttpClientConfig{
	TimeoutMs:             30000,
	MaxRetry:              0,
	RetryWaitTimeMs:       0,
	MaxRetryWaitTimeMs:    0,
	AllowRedirect:         false,
	KeepAliveMs:           30000,
	MaxIdleConns:          100,
	MaxIdleConnsPerHost:   0,
	IdleConnTimeoutMs:     90000,
	TLSHandshakeTimeoutMs: 10000,
}

Functions

func Backoff

func Backoff(execFunc func() (*http.Response, error), configureFuncs ...ConfigureFunc) error

Types

type ConfigureFunc

type ConfigureFunc func(options *Options)

func MaxRetries

func MaxRetries(value int) ConfigureFunc

func MaxRetryWaitTime

func MaxRetryWaitTime(value time.Duration) ConfigureFunc

func RetryAfterFun

func RetryAfterFun(retryAfterFunc RetryAfterFunc) ConfigureFunc

func RetryConditions

func RetryConditions(conditions []RetryConditionFunc) ConfigureFunc

func RetryWaitTime

func RetryWaitTime(value time.Duration) ConfigureFunc

type DnsResolver

type DnsResolver struct {
	TTL time.Duration // default 5 min.
	// contains filtered or unexported fields
}

func (*DnsResolver) GetAllEntities

func (r *DnsResolver) GetAllEntities() []*cacheEntity

func (*DnsResolver) LookupHost

func (r *DnsResolver) LookupHost(ctx context.Context, host string) (addrs []string, err error)

type HttpClient

type HttpClient struct {
	MaxRetry         int
	RetryWaitTime    time.Duration
	MaxRetryWaitTime time.Duration

	AllowRedirect     bool
	MaxAllowRedirects int

	// Custom parse response HTTP Retry-After header.
	// See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
	// e.g. Retry-After: Fri, 31 Dec 1999 23:59:59 GMT
	// e.g. Retry-After: 120
	RetryAfterFunc RetryAfterFunc

	// Custom  judge response is need to retry.
	// you can implement your custom strategy, for example: response status code is not 200.
	RetryConditions []RetryConditionFunc
	// contains filtered or unexported fields
}

HttpClient struct wrapper http client, provides some new feature. 1. Local DNS Cache. 2. Retry Exponential Backoff And Jitter Strategy. See: https://aws.amazon.com/cn/blogs/architecture/exponential-backoff-and-jitter/ 3. Multi-Goroutine Download. 4. Allow Custom Max Redirects.

func NewHttpClient

func NewHttpClient(config *HttpClientConfig) (*HttpClient, error)

func (*HttpClient) Cookies

func (c *HttpClient) Cookies(u *netUrl.URL) []*http.Cookie

func (*HttpClient) Do

func (c *HttpClient) Do(req *http.Request) (*Response, error)

func (*HttpClient) Get

func (c *HttpClient) Get(header map[string]string, url string, params map[string]string) (*Response, error)

func (*HttpClient) Post

func (c *HttpClient) Post(header map[string]string, url, contentType string, body []byte) (*Response, error)

func (*HttpClient) PostForm

func (c *HttpClient) PostForm(header map[string]string, url string, params map[string]string) (*Response, error)

type HttpClientConfig

type HttpClientConfig struct {
	TimeoutMs int64 // default timeout 30 seconds, contain connection timeout and request timeout.

	MaxRetry           int // default not retry.
	RetryWaitTimeMs    int64
	MaxRetryWaitTimeMs int64

	AllowRedirect     bool
	MaxAllowRedirects int // default allow 10 redirects.

	KeepAliveMs           int64 // default keep-alive time is 30 seconds.
	MaxIdleConns          int   // default max idle connections is 100.
	MaxIdleConnsPerHost   int   // default per-host max idle connections is 0, no limit.
	IdleConnTimeoutMs     int   // default idle connections timeout is 90 seconds.
	TLSHandshakeTimeoutMs int   // default TLS hand shake timeout is 10 seconds.

	ProxyUrl    string // support http, https, socks proxy.
	ProxyUname  string
	ProxyPasswd string
}

type Options

type Options struct {
	// contains filtered or unexported fields
}

type Response

type Response struct {
	RawResponse *http.Response
	Header      http.Header
	Body        []byte
	StatusCode  int
}

func (*Response) String

func (res *Response) String() string

type RetryAfterFunc

type RetryAfterFunc func(response *http.Response) time.Duration

Custom parse response HTTP Retry-After header. If Retry-After time greater than config max retry wait time, use config max retry wait time. See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

type RetryConditionFunc

type RetryConditionFunc func(response *http.Response, err error) bool

Judge response it's need to retry.

Jump to

Keyboard shortcuts

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