Documentation ¶
Index ¶
- func Do(ctx context.Context, req *protocol.Request, resp *protocol.Response) error
- func DoDeadline(ctx context.Context, req *protocol.Request, resp *protocol.Response, ...) error
- func DoRedirects(ctx context.Context, req *protocol.Request, resp *protocol.Response, ...) error
- func DoTimeout(ctx context.Context, req *protocol.Request, resp *protocol.Response, ...) error
- func Get(ctx context.Context, dst []byte, url string) (statusCode int, body []byte, err error)
- func GetDeadline(ctx context.Context, dst []byte, url string, deadline time.Time) (statusCode int, body []byte, err error)
- func GetTimeout(ctx context.Context, dst []byte, url string, timeout time.Duration) (statusCode int, body []byte, err error)
- func Post(ctx context.Context, dst []byte, url string, postArgs *protocol.Args) (statusCode int, body []byte, err error)
- func WithClientReadTimeout(t time.Duration) config.ClientOption
- func WithDialFunc(f client.DialFunc) config.ClientOption
- func WithDialTimeout(dialTimeout time.Duration) config.ClientOption
- func WithDialer(d network.Dialer) config.ClientOption
- func WithKeepAlive(b bool) config.ClientOption
- func WithMaxConnDuration(t time.Duration) config.ClientOption
- func WithMaxConnWaitTimeout(t time.Duration) config.ClientOption
- func WithMaxConnsPerHost(mc int) config.ClientOption
- func WithMaxIdempotentCallAttempts(n int) config.ClientOption
- func WithMaxIdleConnDuration(t time.Duration) config.ClientOption
- func WithResponseBodyStream(b bool) config.ClientOption
- func WithTLSConfig(cfg *tls.Config) config.ClientOption
- type Client
- func (c *Client) CloseIdleConnections()
- func (c *Client) Do(ctx context.Context, req *protocol.Request, resp *protocol.Response) error
- func (c *Client) DoDeadline(ctx context.Context, req *protocol.Request, resp *protocol.Response, ...) error
- func (c *Client) DoRedirects(ctx context.Context, req *protocol.Request, resp *protocol.Response, ...) error
- func (c *Client) DoTimeout(ctx context.Context, req *protocol.Request, resp *protocol.Response, ...) error
- func (c *Client) Get(ctx context.Context, dst []byte, url string, ...) (statusCode int, body []byte, err error)
- func (c *Client) GetDeadline(ctx context.Context, dst []byte, url string, deadline time.Time, ...) (statusCode int, body []byte, err error)
- func (c *Client) GetTimeout(ctx context.Context, dst []byte, url string, timeout time.Duration, ...) (statusCode int, body []byte, err error)
- func (c *Client) Post(ctx context.Context, dst []byte, url string, postArgs *protocol.Args, ...) (statusCode int, body []byte, err error)
- func (c *Client) SetProxy(p protocol.Proxy)
- func (c *Client) Use(mws ...Middleware)
- type Endpoint
- type Middleware
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Do ¶
Do performs the given http request and fills the given http response.
Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.©
Client determines the server to be requested in the following order:
- from RequestURI if it contains full url with scheme and host;
- from Host header otherwise.
The function doesn't follow redirects. Use Get* for following redirects.
Response is ignored if resp is nil.
ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.
It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.
func DoDeadline ¶
func DoDeadline(ctx context.Context, req *protocol.Request, resp *protocol.Response, deadline time.Time) error
DoDeadline performs the given request and waits for response until the given deadline.
Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.
Client determines the server to be requested in the following order:
- from RequestURI if it contains full url with scheme and host;
- from Host header otherwise.
The function doesn't follow redirects. Use Get* for following redirects.
Response is ignored if resp is nil.
errTimeout is returned if the response wasn't returned until the given deadline.
ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.
It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.
func DoRedirects ¶
func DoRedirects(ctx context.Context, req *protocol.Request, resp *protocol.Response, maxRedirectsCount int) error
DoRedirects performs the given http request and fills the given http response, following up to maxRedirectsCount redirects. When the redirect count exceeds maxRedirectsCount, ErrTooManyRedirects is returned.
Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.
Client determines the server to be requested in the following order:
- from RequestURI if it contains full url with scheme and host;
- from Host header otherwise.
Response is ignored if resp is nil.
ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.
It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.
func DoTimeout ¶
func DoTimeout(ctx context.Context, req *protocol.Request, resp *protocol.Response, timeout time.Duration) error
DoTimeout performs the given request and waits for response during the given timeout duration.
Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.
Client determines the server to be requested in the following order:
- from RequestURI if it contains full url with scheme and host;
- from Host header otherwise.
The function doesn't follow redirects. Use Get* for following redirects.
Response is ignored if resp is nil.
errTimeout is returned if the response wasn't returned during the given timeout.
ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.
It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.
Warning: DoTimeout does not terminate the request itself. The request will continue in the background and the response will be discarded. If requests take too long and the connection pool gets filled up please try using a Client and setting a ReadTimeout.
func Get ¶
Get returns the status code and body of url.
The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.
The function follows redirects. Use Do* for manually handling redirects.
func GetDeadline ¶
func GetDeadline(ctx context.Context, dst []byte, url string, deadline time.Time) (statusCode int, body []byte, err error)
GetDeadline returns the status code and body of url.
The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.
The function follows redirects. Use Do* for manually handling redirects.
errTimeout error is returned if url contents couldn't be fetched until the given deadline.
func GetTimeout ¶
func GetTimeout(ctx context.Context, dst []byte, url string, timeout time.Duration) (statusCode int, body []byte, err error)
GetTimeout returns the status code and body of url.
The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.
The function follows redirects. Use Do* for manually handling redirects.
errTimeout error is returned if url contents couldn't be fetched during the given timeout.
func Post ¶
func Post(ctx context.Context, dst []byte, url string, postArgs *protocol.Args) (statusCode int, body []byte, err error)
Post sends POST request to the given url with the given POST arguments.
The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.
The function follows redirects. Use Do* for manually handling redirects.
Empty POST body is sent if postArgs is nil.
func WithClientReadTimeout ¶
func WithClientReadTimeout(t time.Duration) config.ClientOption
WithClientReadTimeout sets maximum duration for full response reading (including body).
func WithDialFunc ¶
func WithDialFunc(f client.DialFunc) config.ClientOption
WithDialFunc is used to set dialer function
NOTE: By default, hertz client uses dialer.DialConnection as DialFunc
func WithDialTimeout ¶
func WithDialTimeout(dialTimeout time.Duration) config.ClientOption
WithDialTimeout sets dial timeout.
func WithDialer ¶
func WithDialer(d network.Dialer) config.ClientOption
WithDialer sets the specific dialer.
func WithKeepAlive ¶
func WithKeepAlive(b bool) config.ClientOption
WithKeepAlive determines whether use keep-alive connection.
func WithMaxConnDuration ¶
func WithMaxConnDuration(t time.Duration) config.ClientOption
WithMaxConnDuration sets max connection duration, keep-alive connections are closed after this duration.
func WithMaxConnWaitTimeout ¶
func WithMaxConnWaitTimeout(t time.Duration) config.ClientOption
WithMaxConnWaitTimeout sets maximum duration for waiting for a free connection.
func WithMaxConnsPerHost ¶
func WithMaxConnsPerHost(mc int) config.ClientOption
WithMaxConnsPerHost sets maximum number of connections per host which may be established.
func WithMaxIdempotentCallAttempts ¶
func WithMaxIdempotentCallAttempts(n int) config.ClientOption
WithMaxIdempotentCallAttempts sets maximum number of attempts for idempotent calls.
func WithMaxIdleConnDuration ¶
func WithMaxIdleConnDuration(t time.Duration) config.ClientOption
WithMaxIdleConnDuration sets max idle connection duration, idle keep-alive connections are closed after this duration.
func WithResponseBodyStream ¶
func WithResponseBodyStream(b bool) config.ClientOption
WithResponseBodyStream is used to determine whether read body in stream or not.
func WithTLSConfig ¶
func WithTLSConfig(cfg *tls.Config) config.ClientOption
WithTLSConfig sets tlsConfig to create a tls connection.
Types ¶
type Client ¶
type Client struct { // Client name. Used in User-Agent request header. // // Default client name is used if not set. Name string // NoDefaultUserAgentHeader when set to true, causes the default // User-Agent header to be excluded from the Request. NoDefaultUserAgentHeader bool // Callback for establishing new connections to hosts. // // Default Dial is used if not set. Dial client.DialFunc // Timeout for establishing new connections to hosts. // // Default DialTimeout is used if not set. DialTimeout time.Duration // Attempt to connect to both ipv4 and ipv6 addresses if set to true. // // This option is used only if default TCP dialer is used, // i.e. if Dial is blank. // // By default client connects only to ipv4 addresses, // since unfortunately ipv6 remains broken in many networks worldwide :) DialDualStack bool // TLS config for https connections. // // Default TLS config is used if not set. TLSConfig *tls.Config // Maximum number of connections per each host which may be established. // // DefaultMaxConnsPerHost is used if not set. MaxConnsPerHost int // Idle keep-alive connections are closed after this duration. // // By default idle connections are closed // after DefaultMaxIdleConnDuration. MaxIdleConnDuration time.Duration // Keep-alive connections are closed after this duration. // // By default connection duration is unlimited. MaxConnDuration time.Duration // Maximum number of attempts for idempotent calls // // DefaultMaxIdempotentCallAttempts is used if not set. MaxIdempotentCallAttempts int // Per-connection buffer size for responses' reading. // This also limits the maximum header size. // // Default buffer size is used if 0. ReadBufferSize int // Per-connection buffer size for requests' writing. // // Default buffer size is used if 0. WriteBufferSize int // Maximum duration for full response reading (including body). // // By default response read timeout is unlimited. ReadTimeout time.Duration // Maximum duration for full request writing (including body). // // By default request write timeout is unlimited. WriteTimeout time.Duration // Maximum response body size. // // The client returns ErrBodyTooLarge if this limit is greater than 0 // and response body is greater than the limit. // // By default response body size is unlimited. MaxResponseBodySize int // Header names are passed as-is without normalization // if this option is set. // // Disabled header names' normalization may be useful only for proxying // responses to other clients expecting case-sensitive header names. // // By default request and response header names are normalized, i.e. // The first letter and the first letters following dashes // are uppercased, while all the other letters are lowercased. // Examples: // // * HOST -> Host // * content-type -> Content-Type // * cONTENT-lenGTH -> Content-Length DisableHeaderNamesNormalizing bool // Path values are sent as-is without normalization // // Disabled path normalization may be useful for proxying incoming requests // to servers that are expecting paths to be forwarded as-is. // // By default path values are normalized, i.e. // extra slashes are removed, special characters are encoded. DisablePathNormalizing bool // Maximum duration for waiting for a free connection. // // By default will not waiting, return ErrNoFreeConns immediately MaxConnWaitTimeout time.Duration // RetryIf controls whether a retry should be attempted after an error. // // By default will use isIdempotent function RetryIf client.RetryIfFunc // Connection will close after each request when set this to true. KeepAlive bool // ResponseBodyStream enables response body streaming ResponseBodyStream bool // Proxy specifies a function to return a proxy for a given // Request. If the function returns a non-nil error, the // request is aborted with the provided error. // // The proxy type is determined by the URL scheme. // "http" and "https" are supported. If the scheme is empty, // "http" is assumed. // // If Proxy is nil or returns a nil *URL, no proxy is used. Proxy protocol.Proxy // contains filtered or unexported fields }
Client implements http client.
Copying Client by value is prohibited. Create new instance instead.
It is safe calling Client methods from concurrently running goroutines.
func NewClient ¶
func NewClient(opts ...config.ClientOption) (*Client, error)
NewClient return a client with options
func (*Client) CloseIdleConnections ¶
func (c *Client) CloseIdleConnections()
CloseIdleConnections closes any connections which were previously connected from previous requests but are now sitting idle in a "keep-alive" state. It does not interrupt any connections currently in use.
func (*Client) Do ¶
Do performs the given http request and fills the given http response.
Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.
Client determines the server to be requested in the following order:
- from RequestURI if it contains full url with scheme and host;
- from Host header otherwise.
Response is ignored if resp is nil.
The function doesn't follow redirects. Use Get* for following redirects.
ErrNoFreeConns is returned if all Client.MaxConnsPerHost connections to the requested host are busy.
It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.
func (*Client) DoDeadline ¶
func (c *Client) DoDeadline(ctx context.Context, req *protocol.Request, resp *protocol.Response, deadline time.Time) error
DoDeadline performs the given request and waits for response until the given deadline.
Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.
Client determines the server to be requested in the following order:
- from RequestURI if it contains full url with scheme and host;
- from Host header otherwise.
The function doesn't follow redirects. Use Get* for following redirects.
Response is ignored if resp is nil.
errTimeout is returned if the response wasn't returned until the given deadline.
ErrNoFreeConns is returned if all Client.MaxConnsPerHost connections to the requested host are busy.
It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.
func (*Client) DoRedirects ¶
func (c *Client) DoRedirects(ctx context.Context, req *protocol.Request, resp *protocol.Response, maxRedirectsCount int) error
DoRedirects performs the given http request and fills the given http response, following up to maxRedirectsCount redirects. When the redirect count exceeds maxRedirectsCount, ErrTooManyRedirects is returned.
Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.
Client determines the server to be requested in the following order:
- from RequestURI if it contains full url with scheme and host;
- from Host header otherwise.
Response is ignored if resp is nil.
ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections to the requested host are busy.
It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.
func (*Client) DoTimeout ¶
func (c *Client) DoTimeout(ctx context.Context, req *protocol.Request, resp *protocol.Response, timeout time.Duration) error
DoTimeout performs the given request and waits for response during the given timeout duration.
Request must contain at least non-zero RequestURI with full url (including scheme and host) or non-zero Host header + RequestURI.
Client determines the server to be requested in the following order:
- from RequestURI if it contains full url with scheme and host;
- from Host header otherwise.
The function doesn't follow redirects. Use Get* for following redirects.
Response is ignored if resp is nil.
errTimeout is returned if the response wasn't returned during the given timeout.
ErrNoFreeConns is returned if all Client.MaxConnsPerHost connections to the requested host are busy.
It is recommended obtaining req and resp via AcquireRequest and AcquireResponse in performance-critical code.
Warning: DoTimeout does not terminate the request itself. The request will continue in the background and the response will be discarded. If requests take too long and the connection pool gets filled up please try setting a ReadTimeout.
func (*Client) Get ¶
func (c *Client) Get(ctx context.Context, dst []byte, url string, requestOptions ...config.RequestOption) (statusCode int, body []byte, err error)
Get returns the status code and body of url.
The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.
The function follows redirects. Use Do* for manually handling redirects.
func (*Client) GetDeadline ¶
func (c *Client) GetDeadline(ctx context.Context, dst []byte, url string, deadline time.Time, requestOptions ...config.RequestOption) (statusCode int, body []byte, err error)
GetDeadline returns the status code and body of url.
The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.
The function follows redirects. Use Do* for manually handling redirects.
errTimeout error is returned if url contents couldn't be fetched until the given deadline.
func (*Client) GetTimeout ¶
func (c *Client) GetTimeout(ctx context.Context, dst []byte, url string, timeout time.Duration, requestOptions ...config.RequestOption) (statusCode int, body []byte, err error)
GetTimeout returns the status code and body of url.
The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.
The function follows redirects. Use Do* for manually handling redirects.
errTimeout error is returned if url contents couldn't be fetched during the given timeout.
func (*Client) Post ¶
func (c *Client) Post(ctx context.Context, dst []byte, url string, postArgs *protocol.Args, requestOptions ...config.RequestOption) (statusCode int, body []byte, err error)
Post sends POST request to the given url with the given POST arguments.
The contents of dst will be replaced by the body and returned, if the dst is too small a new slice will be allocated.
The function follows redirects. Use Do* for manually handling redirects.
Empty POST body is sent if postArgs is nil.
func (*Client) SetProxy ¶
SetProxy is used to set client proxy.
Don't SetProxy twice for a client. If you want to use another proxy, please create another client and set proxy to it.
func (*Client) Use ¶
func (c *Client) Use(mws ...Middleware)
type Middleware ¶
Middleware deal with input Endpoint and output Endpoint.