httpclient

package
v9.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package httpclient constructs HTTP clients with optional OpenTelemetry tracing instrumentation.

Clients are built with functional options:

client := httpclient.NewHTTPClient(
	httpclient.WithTimeout(5*time.Second),
	httpclient.WithTracing(true),
)

Options are applied in order, so a later one overrides an earlier one. An environment-loaded Config expresses itself as Options via Config.Options, so a config-driven client is built the same way, and individual settings can still be overridden after it:

client := httpclient.NewHTTPClient(append(cfg.Options(), httpclient.WithTracing(true))...)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHTTPClient

func NewHTTPClient(opts ...Option) *http.Client

NewHTTPClient provides an HTTP client. With no options it returns a client with the package defaults; pass Config.Options to drive it from an environment-loaded Config.

func RegisterHTTPClient

func RegisterHTTPClient(i do.Injector, opts ...Option)

RegisterHTTPClient registers an *http.Client with the injector, built from the injector's *Config. Any opts are applied after the Config and so override it.

Types

type Config

type Config struct {
	Timeout             time.Duration `env:"TIMEOUT"                 json:"timeout"             yaml:"timeout"`
	MaxIdleConns        int           `env:"MAX_IDLE_CONNS"          json:"maxIdleConns"        yaml:"maxIdleConns"`
	MaxIdleConnsPerHost int           `env:"MAX_IDLE_CONNS_PER_HOST" json:"maxIdleConnsPerHost" yaml:"maxIdleConnsPerHost"`
	EnableTracing       bool          `env:"ENABLE_TRACING"          json:"enableTracing"       yaml:"enableTracing"`
}

Config configures an HTTP client.

func (*Config) EnsureDefaults

func (cfg *Config) EnsureDefaults()

EnsureDefaults sets default values for zero fields.

func (*Config) Options

func (cfg *Config) Options() []Option

Options expresses the config as the equivalent list of Options, which is how a Config reaches NewHTTPClient:

client := httpclient.NewHTTPClient(cfg.Options()...)

Callers can append further Options to override individual settings. Zero-valued numeric fields yield Options that leave the package defaults in place, matching EnsureDefaults; EnableTracing is applied as given. A nil Config yields no Options.

func (*Config) ValidateWithContext

func (cfg *Config) ValidateWithContext(ctx context.Context) error

ValidateWithContext validates the config.

type Option

type Option func(*clientConfig)

Option customizes the HTTP client returned by NewHTTPClient. Options are applied in order, so a later Option overrides an earlier one.

func WithMaxIdleConns

func WithMaxIdleConns(n int) Option

WithMaxIdleConns sets the transport's maximum number of idle connections across all hosts. A non-positive value leaves the default in place. It has no effect alongside WithTransport.

func WithMaxIdleConnsPerHost

func WithMaxIdleConnsPerHost(n int) Option

WithMaxIdleConnsPerHost sets the transport's maximum number of idle connections per host. A non-positive value leaves the default in place. It has no effect alongside WithTransport.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the client's overall request timeout, which also bounds the dial. A non-positive duration leaves the default (defaultTimeout) in place.

func WithTracing

func WithTracing(enabled bool) Option

WithTracing toggles wrapping the transport in OpenTelemetry instrumentation. Tracing is off by default.

func WithTransport

func WithTransport(transport http.RoundTripper) Option

WithTransport uses the given RoundTripper as the client's base transport rather than building one, which is the seam for stubbing responses in tests or layering custom middleware. The connection-pool options are ignored when it is set; tracing, if enabled, still wraps it. A nil RoundTripper is ignored.

Jump to

Keyboard shortcuts

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