Documentation
¶
Overview ¶
Package httpclient provides a resilient HTTP client with automatic retry, circuit breaking, request-ID propagation, and typed JSON helpers.
Basic Usage ¶
client := httpclient.NewWithDefaults(logger) resp, err := client.Do(req)
Typed JSON Helpers ¶
DoJSON decodes the response body into T without needing a manual http.Request:
type UserResp struct{ ID, Name string }
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
user, err := httpclient.DoJSON[UserResp](ctx, client, req)
DoJSONRequest marshals a request body, sends it, and decodes the response:
result, err := httpclient.DoJSONRequest[CreateReq, CreateResp](
ctx, client, http.MethodPost, url, createReq)
Request ID Propagation ¶
If the context carries a request ID (set by core/logz.WithRequestID or the web/mw.RequestID middleware), it is forwarded as X-Request-ID on every outbound attempt, including retries.
Resilience ¶
The retry loop (avast/retry-go) wraps individual HTTP attempts. The circuit breaker (sony/gobreaker) wraps the entire retry sequence, so the breaker opens after CBThreshold fully-exhausted retry sequences fail — not after CBThreshold individual HTTP errors.
Configuration ¶
EINHERJAR_HTTP_CLIENT_NAME — circuit breaker label; default "http" EINHERJAR_HTTP_TIMEOUT — total request timeout; default 30s EINHERJAR_HTTP_DIAL_TIMEOUT — TCP dial timeout; default 5s EINHERJAR_HTTP_MAX_RETRIES — attempts per request; default 3 EINHERJAR_HTTP_RETRY_DELAY — base delay between retries; default 1s EINHERJAR_HTTP_CB_THRESHOLD — consecutive failures to open breaker; default 10 EINHERJAR_HTTP_CB_TIMEOUT — breaker half-open probe interval; default 1m
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Module observability.Identifiable = &moduleID{}
Module identifies this package to observability systems. httpclient is a stateless provider — it is not registered with the launcher as a lifecycle component. Register Module manually with any version registry if needed.
Functions ¶
func DoJSON ¶
DoJSON executes req and decodes the JSON response body into T. Returns a xerrors-typed error for HTTP 4xx/5xx responses.
func DoJSONRequest ¶
func DoJSONRequest[Req, Resp any](ctx context.Context, client Provider, method, rawURL string, body Req) (*Resp, error)
DoJSONRequest marshals body as JSON, sends it with the given method to rawURL, and decodes the response into Resp. For requests without a body, use DoJSON instead.
func MapStatusToError ¶
MapStatusToError maps an HTTP status code to the matching xerrors type.
Types ¶
type Config ¶
type Config struct {
// Name identifies this client in logs and circuit breaker metrics.
Name string `env:"EINHERJAR_HTTP_CLIENT_NAME" envDefault:"http"`
Timeout time.Duration `env:"EINHERJAR_HTTP_TIMEOUT" envDefault:"30s"`
DialTimeout time.Duration `env:"EINHERJAR_HTTP_DIAL_TIMEOUT" envDefault:"5s"`
MaxRetries uint `env:"EINHERJAR_HTTP_MAX_RETRIES" envDefault:"3"`
RetryDelay time.Duration `env:"EINHERJAR_HTTP_RETRY_DELAY" envDefault:"1s"`
CBThreshold uint32 `env:"EINHERJAR_HTTP_CB_THRESHOLD" envDefault:"10"`
CBTimeout time.Duration `env:"EINHERJAR_HTTP_CB_TIMEOUT" envDefault:"1m"`
}
Config holds configuration for the HTTP client.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config with sensible production defaults.
type Provider ¶
Provider executes HTTP requests with automatic retry and circuit breaking. Inject Provider into services that make outbound HTTP calls; construct with New or NewWithDefaults.
func NewWithDefaults ¶
NewWithDefaults returns a Provider with sensible defaults.