client

package
v0.0.0-...-2eb6bad Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultLabelRequestHost = "-"
	DefaultLabelRequestURI  = "/"
)

Variables

This section is empty.

Functions

func DefaultUnifyRequestHostHook

func DefaultUnifyRequestHostHook(url *url.URL) string

func DefaultUnifyRequestURIHook

func DefaultUnifyRequestURIHook(url *url.URL) string

func DynamicDebugLogging

func DynamicDebugLogging(req *resty.Request)

DynamicDebugLogging open the debug level logging in dynamically

func NewHTTP

func NewHTTP(config *Config) *http.Client

Types

type Client

type Client struct {
	*resty.Client
	// contains filtered or unexported fields
}

func New

func New(config *Config) *Client

New creates the resty client

func NewWithConfigOptions

func NewWithConfigOptions(opts ...Option) *Client

NewWithConfigOptions creates the resty client with Optional config creates resty client with Default config if not pass opts

func (*Client) WithContext

func (c *Client) WithContext(ctx context.Context) *resty.Request

WithContext would retrieve the request with context, so we can attach the tracing transaction into context.

type Config

type Config struct {
	// DialTimeout controls time maximum timeout of the tcp connect
	DialTimeout time.Duration

	// Timeout controls the maximum timeout of the api request which
	// contains dns lookup, dial, wait for response, etc...
	Timeout time.Duration

	// KeepAlive specifies the interval between keep-alive
	// probes for an active network connection.
	// If zero, keep-alive probes are sent with a default value
	// (currently 15 seconds), if supported by the protocol and operating
	// system. Network protocols or operating systems that do
	// not support keep-alives ignore this field.
	// If negative, keep-alive probes are disabled.
	KeepAlive time.Duration

	// MaxIdleConns controls the maximum number of idle (keep-alive)
	// connections across all hosts. Zero means no limit.
	MaxIdleConns int

	// MaxIdleConnsPerHost, if non-zero, controls the maximum idle
	// (keep-alive) connections to keep per-host. If zero,
	// DefaultMaxIdleConnsPerHost is used.
	MaxIdleConnsPerHost int

	// MaxConnsPerHost optionally limits the total number of
	// connections per host, including connections in the dialing,
	// active, and idle states. On limit violation, dials will block.
	//
	// Zero means no limit.
	MaxConnsPerHost int

	// IdleConnTimeout is the maximum amount of time an idle
	// (keep-alive) connection will remain idle before closing
	// itself.
	// Zero means no limit.
	IdleConnTimeout time.Duration

	// EnabledMetric if true, would export api client _metrics to the prometheus.
	// e.g. dns/tls/response latency
	EnabledMetric bool

	// Deprecated
	// DisabledMetricHostLabel if true, the host label in metric would be pined as '-'.
	// Allow the user to disable the host label in resty metric when the hostname was
	// unpredictable.
	DisabledMetricHostLabel bool

	// Deprecated
	// EnabledMetricWithURI if true, metric would record the uri label. Be carefully
	// when the uri contains the param part may cause the metric exponential growth and OOM,
	// so you MUST know what you do before enabling the option.
	EnabledMetricWithURI bool

	// EnabledTrace if true, use new relic to trace the api request
	EnabledTrace bool

	// EnabledOtelTrace if true, use openTelemetry to trace the api request
	EnabledOtelTrace bool

	// Config for TLS Client
	TLSClientConfig *tls.Config

	// UnifyRequestURIHook allows users to customize the URI in metrics.
	// If UnifyRequestURIHook is nil, then would not record the uri label.
	// Be careful, when the uri contains the param part that may cause the metric exponential growth and OOM,
	// so you MUST handle this situation.
	// And can't return an empty string.
	UnifyRequestURIHook func(url *url.URL) string

	// UnifyRequestHostHook allows users to customize the host in metrics.
	// If UnifyRequestHostHook is nil, then would not record the host label.
	// Be careful, when the client be used to request multiple hosts cause the metric exponential growth and OOM,
	// so you MUST handle this situation.
	// And can't return an empty string.
	UnifyRequestHostHook func(url *url.URL) string

	// ForceAttemptHTTP2 controls whether HTTP/2 is enabled when a non-zero
	// Dial, DialTLS, or DialContext func or TLSClientConfig is provided.
	// By default, use of any those fields conservatively disables HTTP/2.
	// To use a custom dialer or TLS config and still attempt HTTP/2
	// upgrades, set this to true.
	ForceAttemptHTTP2 bool
}

func DefaultConfig

func DefaultConfig() *Config

func NewConfig

func NewConfig(opts ...Option) *Config

NewConfig returns a default config if opts is nil, or returns custom config if pass opts

func (*Config) ApplyOptions

func (config *Config) ApplyOptions(opts ...Option)

ApplyOptions Change config designated values

func (*Config) ParseHost

func (config *Config) ParseHost(url *url.URL) (host string)

func (*Config) ParseURI

func (config *Config) ParseURI(url *url.URL) (uri string)

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option overrides values of Config

func WithDialTimeout

func WithDialTimeout(d time.Duration) Option

func WithDisabledMetricHostLabel

func WithDisabledMetricHostLabel(disabled bool) Option

Deprecated

func WithEnabledMetric

func WithEnabledMetric(enabled bool) Option

func WithEnabledMetricWithURI

func WithEnabledMetricWithURI(enabled bool) Option

Deprecated

func WithEnabledOtelTrace

func WithEnabledOtelTrace(enabled bool) Option

func WithEnabledTrace

func WithEnabledTrace(enabled bool) Option

func WithForceAttemptHTTP2

func WithForceAttemptHTTP2(forceAttemptHTTP2 bool) Option

func WithIdleConnTimeout

func WithIdleConnTimeout(d time.Duration) Option

func WithKeepAlive

func WithKeepAlive(d time.Duration) Option

func WithMaxConnsPerHost

func WithMaxConnsPerHost(conns int) Option

func WithMaxIdleConns

func WithMaxIdleConns(conns int) Option

func WithMaxIdleConnsPerHost

func WithMaxIdleConnsPerHost(conns int) Option

func WithTLSClientConfig

func WithTLSClientConfig(tls *tls.Config) Option

func WithTimeout

func WithTimeout(d time.Duration) Option

func WithUnifyRequestHostHook

func WithUnifyRequestHostHook(hook func(url *url.URL) string) Option

func WithUnifyRequestURIHook

func WithUnifyRequestURIHook(hook func(url *url.URL) string) Option

Jump to

Keyboard shortcuts

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