engine

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 26 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClientClosed = errors.New("client is closed")

ErrClientClosed is returned when attempting to use a closed client.

Functions

func FormatQueryParam added in v1.4.0

func FormatQueryParam(v any) string

FormatQueryParam converts a value to string for query parameters. Optimized to avoid fmt.Sprintf allocations for common types.

func ReleaseResponse added in v1.4.0

func ReleaseResponse(r *Response)

ReleaseResponse returns a Response to the pool for reuse. Call this when the Response data has been consumed and copied elsewhere. After calling this, the Response must not be used.

Types

type Client

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

func NewClient

func NewClient(config *Config, opts ...clientOption) (*Client, error)

func (*Client) Close

func (c *Client) Close() error

func (*Client) GetHealthStatus added in v1.1.0

func (c *Client) GetHealthStatus() HealthStatus

GetHealthStatus returns the current health status of the client.

func (*Client) IsClosed added in v1.4.0

func (c *Client) IsClosed() bool

IsClosed returns true if the client has been closed.

func (*Client) IsHealthy added in v1.1.0

func (c *Client) IsHealthy() bool

IsHealthy returns true if the client is healthy (error rate < 10%).

func (*Client) Request

func (c *Client) Request(ctx context.Context, method, url string, options ...RequestOption) (*Response, error)

type ClientError

type ClientError struct {
	Type       ErrorType
	Message    string
	Cause      error
	URL        string
	Method     string
	Attempts   int
	StatusCode int    // HTTP status code if applicable
	Host       string // Host for circuit breaker errors
}

func (*ClientError) Code

func (e *ClientError) Code() string

func (*ClientError) Error

func (e *ClientError) Error() string

func (*ClientError) IsRetryable

func (e *ClientError) IsRetryable() bool

IsRetryable determines if the error is retryable based on its type and cause.

func (*ClientError) Unwrap

func (e *ClientError) Unwrap() error

func (*ClientError) WithType added in v1.4.0

func (e *ClientError) WithType(t ErrorType) *ClientError

WithType returns a copy of the error with the specified type set.

type Config

type Config struct {
	Timeout               time.Duration
	DialTimeout           time.Duration
	KeepAlive             time.Duration
	TLSHandshakeTimeout   time.Duration
	ResponseHeaderTimeout time.Duration
	IdleConnTimeout       time.Duration
	MaxIdleConns          int
	MaxIdleConnsPerHost   int
	MaxConnsPerHost       int
	ProxyURL              string

	// System proxy configuration
	EnableSystemProxy bool // Automatically detect and use system proxy settings

	TLSConfig               *tls.Config
	MinTLSVersion           uint16
	MaxTLSVersion           uint16
	InsecureSkipVerify      bool
	MaxResponseBodySize     int64
	MaxDecompressedBodySize int64
	ValidateURL             bool
	ValidateHeaders         bool
	AllowPrivateIPs         bool
	ExemptNets              []*net.IPNet
	StrictContentLength     bool

	MaxRetries    int
	RetryDelay    time.Duration
	MaxRetryDelay time.Duration
	BackoffFactor float64
	Jitter        bool

	// CustomRetryPolicy allows providing a custom retry policy implementation.
	// If set, it overrides the built-in retry logic.
	CustomRetryPolicy types.RetryPolicy

	UserAgent       string
	Headers         map[string]string
	FollowRedirects bool
	MaxRedirects    int
	EnableHTTP2     bool

	CookieJar     http.CookieJar
	EnableCookies bool

	// DNS configuration
	EnableDoH   bool
	DoHCacheTTL time.Duration

	// Redirect whitelist configuration
	RedirectWhitelist *security.DomainWhitelist

	// Certificate pinning
	CertificatePinner security.CertificatePinner
}

Config defines the HTTP client configuration. Config should be treated as immutable after creation.

type ErrorType

type ErrorType int
const (
	ErrorTypeUnknown ErrorType = iota
	ErrorTypeNetwork
	ErrorTypeTimeout
	ErrorTypeContextCanceled
	ErrorTypeResponseRead
	ErrorTypeTransport
	ErrorTypeRetryExhausted
	ErrorTypeTLS
	ErrorTypeCertificate
	ErrorTypeDNS
	ErrorTypeValidation
	ErrorTypeHTTP
)

type HealthStatus added in v1.2.0

type HealthStatus struct {
	Healthy            bool
	TotalRequests      int64
	SuccessfulRequests int64
	FailedRequests     int64
	AverageLatency     time.Duration
	ErrorRate          float64
}

HealthStatus represents basic health Metrics for the client.

type Metrics added in v1.3.8

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

Metrics collects and tracks HTTP client performance Metrics. All methods are safe for concurrent use.

func (*Metrics) GetHealthStatus added in v1.3.8

func (m *Metrics) GetHealthStatus() HealthStatus

GetHealthStatus returns the current health status of the client. A client is considered healthy if its error rate is below 10%.

func (*Metrics) IsHealthy added in v1.3.8

func (m *Metrics) IsHealthy() bool

IsHealthy returns true if the client is healthy (error rate < 10%).

func (*Metrics) RecordRequest added in v1.3.8

func (m *Metrics) RecordRequest(latencyNs int64, success bool)

RecordRequest records the result of a single request. It updates the request counters and rolling average latency.

func (*Metrics) Reset added in v1.3.8

func (m *Metrics) Reset()

Reset resets all Metrics to zero.

func (*Metrics) Snapshot added in v1.3.8

func (m *Metrics) Snapshot() MetricsSnapshot

Snapshot returns a point-in-time copy of the current Metrics. Each field is individually atomic, but the snapshot is not transactionally consistent — concurrent calls may cause total != success + failed.

type MetricsSnapshot added in v1.3.8

type MetricsSnapshot struct {
	TotalRequests      int64
	SuccessfulRequests int64
	FailedRequests     int64
	AverageLatency     time.Duration
}

MetricsSnapshot represents a point-in-time snapshot of client Metrics.

type Request

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

func (*Request) Body

func (r *Request) Body() any

func (*Request) Context

func (r *Request) Context() context.Context

func (*Request) Cookies

func (r *Request) Cookies() []http.Cookie

func (*Request) FollowRedirects added in v1.3.0

func (r *Request) FollowRedirects() *bool

func (*Request) Headers

func (r *Request) Headers() map[string]string

func (*Request) MaxRedirects added in v1.3.0

func (r *Request) MaxRedirects() *int

func (*Request) MaxRetries

func (r *Request) MaxRetries() int

func (*Request) Method

func (r *Request) Method() string

Accessors (implement RequestMutator)

func (*Request) OnRequest added in v1.3.8

func (r *Request) OnRequest() requestCallback

Callback accessors

func (*Request) OnResponse added in v1.3.8

func (r *Request) OnResponse() responseCallback

func (*Request) QueryParams

func (r *Request) QueryParams() map[string]any

func (*Request) SetBody added in v1.3.8

func (r *Request) SetBody(v any)

func (*Request) SetContext added in v1.3.8

func (r *Request) SetContext(v context.Context)

func (*Request) SetCookies added in v1.3.8

func (r *Request) SetCookies(v []http.Cookie)

func (*Request) SetFollowRedirects added in v1.3.8

func (r *Request) SetFollowRedirects(v *bool)

func (*Request) SetHeader added in v1.3.8

func (r *Request) SetHeader(key, value string)

func (*Request) SetHeaders added in v1.3.8

func (r *Request) SetHeaders(v map[string]string)

func (*Request) SetMaxRedirects added in v1.3.8

func (r *Request) SetMaxRedirects(v *int)

func (*Request) SetMaxRetries added in v1.3.8

func (r *Request) SetMaxRetries(v int)

func (*Request) SetMethod added in v1.3.8

func (r *Request) SetMethod(v string)

Mutators

func (*Request) SetOnRequest added in v1.3.8

func (r *Request) SetOnRequest(cb requestCallback)

func (*Request) SetOnResponse added in v1.3.8

func (r *Request) SetOnResponse(cb responseCallback)

func (*Request) SetQueryParams added in v1.3.8

func (r *Request) SetQueryParams(v map[string]any)

func (*Request) SetStreamBody added in v1.4.0

func (r *Request) SetStreamBody(v bool)

func (*Request) SetTimeout added in v1.3.8

func (r *Request) SetTimeout(v time.Duration)

func (*Request) SetURL added in v1.3.8

func (r *Request) SetURL(v string)

func (*Request) StreamBody added in v1.4.0

func (r *Request) StreamBody() bool

func (*Request) Timeout

func (r *Request) Timeout() time.Duration

func (*Request) URL

func (r *Request) URL() string

type RequestOption

type RequestOption func(*Request) error

type Response

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

Response represents an HTTP response. Response objects are safe to read from multiple goroutines after they are returned.

func (*Response) Attempts

func (r *Response) Attempts() int

func (*Response) Body

func (r *Response) Body() string

func (*Response) ContentLength

func (r *Response) ContentLength() int64

func (*Response) Cookies

func (r *Response) Cookies() []*http.Cookie

func (*Response) Duration

func (r *Response) Duration() time.Duration

func (*Response) Headers

func (r *Response) Headers() http.Header

func (*Response) Proto

func (r *Response) Proto() string

func (*Response) RawBody

func (r *Response) RawBody() []byte

func (*Response) RawBodyReader added in v1.4.0

func (r *Response) RawBodyReader() io.ReadCloser

func (*Response) RedirectChain added in v1.3.0

func (r *Response) RedirectChain() []string

func (*Response) RedirectCount added in v1.3.0

func (r *Response) RedirectCount() int

func (*Response) RequestHeaders added in v1.3.0

func (r *Response) RequestHeaders() http.Header

func (*Response) RequestMethod added in v1.3.8

func (r *Response) RequestMethod() string

func (*Response) RequestURL added in v1.3.8

func (r *Response) RequestURL() string

func (*Response) SetAttempts added in v1.3.8

func (r *Response) SetAttempts(v int)

func (*Response) SetBody added in v1.3.8

func (r *Response) SetBody(v string)

func (*Response) SetContentLength added in v1.3.8

func (r *Response) SetContentLength(v int64)

func (*Response) SetCookies added in v1.3.8

func (r *Response) SetCookies(v []*http.Cookie)

func (*Response) SetDuration added in v1.3.8

func (r *Response) SetDuration(v time.Duration)

func (*Response) SetHeader added in v1.3.8

func (r *Response) SetHeader(key string, values ...string)

SetHeader sets a header with multiple values (implements ResponseMutator)

func (*Response) SetHeaders added in v1.3.8

func (r *Response) SetHeaders(v http.Header)

func (*Response) SetProto added in v1.3.8

func (r *Response) SetProto(v string)

func (*Response) SetRawBody added in v1.3.8

func (r *Response) SetRawBody(v []byte)

func (*Response) SetRedirectChain added in v1.3.8

func (r *Response) SetRedirectChain(v []string)

func (*Response) SetRedirectCount added in v1.3.8

func (r *Response) SetRedirectCount(v int)

func (*Response) SetRequestHeaders added in v1.3.8

func (r *Response) SetRequestHeaders(v http.Header)

func (*Response) SetRequestMethod added in v1.3.8

func (r *Response) SetRequestMethod(v string)

func (*Response) SetRequestURL added in v1.3.8

func (r *Response) SetRequestURL(v string)

func (*Response) SetStatus added in v1.3.8

func (r *Response) SetStatus(v string)

func (*Response) SetStatusCode added in v1.3.8

func (r *Response) SetStatusCode(v int)

Mutators (implement ResponseMutator)

func (*Response) Status

func (r *Response) Status() string

func (*Response) StatusCode

func (r *Response) StatusCode() int

Accessors (implement ResponseAccessor)

Jump to

Keyboard shortcuts

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