client

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(ctx context.Context, req *protocol.Request, resp *protocol.Response) error

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.

Warning: DoDeadline 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 customized Client instance with a ReadTimeout config or set the request level read timeout like: `req.SetOptions(config.WithReadTimeout(1 * time.Second))`

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 customized Client instance with a ReadTimeout config or set the request level read timeout like: `req.SetOptions(config.WithReadTimeout(1 * time.Second))`

func Get

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

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

Warning: GetDeadline 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 customized Client instance with a ReadTimeout config or set the request level read timeout like: `GetDeadline(ctx, dst, url, timeout, config.WithReadTimeout(1 * time.Second))`

func GetTimeout

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

Warning: GetTimeout 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 customized Client instance with a ReadTimeout config or set the request level read timeout like: `GetTimeout(ctx, dst, url, timeout, config.WithReadTimeout(1 * time.Second))`

func Post

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

func WithClientReadTimeout(t time.Duration) config.ClientOption

WithClientReadTimeout sets maximum duration for full response reading (including body).

func WithConnStateObserve

func WithConnStateObserve(hs config.HostClientStateFunc, interval ...time.Duration) config.ClientOption

WithConnStateObserve sets the connection state observation function. The first param is used to set hostclient state func. The second param is used to set observation interval, default value is 5 seconds. Warn: Do not start go routine in HostClientStateFunc.

func WithDialFunc

func WithDialFunc(f network.DialFunc, dialers ...network.Dialer) config.ClientOption

WithDialFunc is used to set dialer function. Note: WithDialFunc will overwrite custom dialer.

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 WithDisableHeaderNamesNormalizing

func WithDisableHeaderNamesNormalizing(disable bool) config.ClientOption

WithDisableHeaderNamesNormalizing is used to set whether disable header names normalizing.

func WithDisablePathNormalizing

func WithDisablePathNormalizing(isDisablePathNormalizing bool) config.ClientOption

WithDisablePathNormalizing sets whether disable path normalizing.

func WithHostClientConfigHook

func WithHostClientConfigHook(h func(hc interface{}) error) config.ClientOption

WithHostClientConfigHook is used to set the function hook for re-configure the host client.

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 WithMaxIdleConnDuration

func WithMaxIdleConnDuration(t time.Duration) config.ClientOption

WithMaxIdleConnDuration sets max idle connection duration, idle keep-alive connections are closed after this duration.

func WithName

func WithName(name string) config.ClientOption

WithName sets client name which used in User-Agent Header.

func WithNoDefaultUserAgentHeader

func WithNoDefaultUserAgentHeader(isNoDefaultUserAgentHeader bool) config.ClientOption

WithNoDefaultUserAgentHeader sets whether no default User-Agent header.

func WithResponseBodyStream

func WithResponseBodyStream(b bool) config.ClientOption

WithResponseBodyStream is used to determine whether read body in stream or not.

func WithRetryConfig

func WithRetryConfig(opts ...retry.Option) config.ClientOption

func WithTLSConfig

func WithTLSConfig(cfg *tls.Config) config.ClientOption

WithTLSConfig sets tlsConfig to create a tls connection.

func WithWriteTimeout

func WithWriteTimeout(t time.Duration) config.ClientOption

WithWriteTimeout sets write timeout.

Types

type Client

type Client struct {

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

	// RetryIfFunc sets the retry decision function. If nil, the client.DefaultRetryIf will be applied.
	RetryIfFunc client.RetryIfFunc
	// 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

func (c *Client) Do(ctx context.Context, req *protocol.Request, resp *protocol.Response) error

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 using a customized Client instance with a ReadTimeout config or set the request level read timeout like: `req.SetOptions(config.WithReadTimeout(1 * time.Second))`

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

func (c *Client) GetDialerName() (dName string, err error)

GetDialerName returns the name of the dialer

func (*Client) GetOptions

func (c *Client) GetOptions() *config.ClientOptions

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

func (c *Client) SetClientFactory(cf suite.ClientFactory)

func (*Client) SetProxy

func (c *Client) SetProxy(p protocol.Proxy)

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) SetRetryIf deprecated

func (c *Client) SetRetryIf(fn func(request *protocol.Request) bool)

Deprecated: use SetRetryIfFunc instead of SetRetryIf

func (*Client) SetRetryIfFunc

func (c *Client) SetRetryIfFunc(retryIf client.RetryIfFunc)

func (*Client) TakeOutLastMiddleware

func (c *Client) TakeOutLastMiddleware() Middleware

TakeOutLastMiddleware will return the set middleware and remove it from client.

Remember to set it back after chain it with other middleware.

func (*Client) Use

func (c *Client) Use(mws ...Middleware)

func (*Client) UseAsLast

func (c *Client) UseAsLast(mw Middleware) error

UseAsLast is used to add middleware to the end of the middleware chain.

Will return an error if last middleware has been set before, to ensure all middleware has the change to work, Please use `TakeOutLastMiddleware` to take out the already set middleware. Chain the middleware after or before is both Okay - but remember to put it back.

type Endpoint

type Endpoint func(ctx context.Context, req *protocol.Request, resp *protocol.Response) (err error)

Endpoint represent one method for calling from remote.

type Middleware

type Middleware func(Endpoint) Endpoint

Middleware deal with input Endpoint and output Endpoint.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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