962b0ccf172a8aa3228b5afa9e7d7fe53e09bd35
Extend retry loop to handle HTTP 429 Too Many Requests: when the server includes a Retry-After header, that duration is used as the retry delay; otherwise falls back to the configured BackOffDelay. Add DoJSONRequest[Req, Resp] free function that serialises the request body as JSON, sets Content-Type, and delegates response decoding to DoJSON. Bump logz and xerrors from v0.9.0 to v1.0.0. API committed as stable.
httpclient
Resilient HTTP client with automatic retry and circuit breaking.
Install
go get code.nochebuena.dev/go/httpclient
Usage
client := httpclient.NewWithDefaults(logger)
// or with custom config:
client = httpclient.New(logger, httpclient.Config{
Name: "payment-api",
MaxRetries: 3,
CBThreshold: 10,
})
resp, err := client.Do(req)
Typed JSON helper
order, err := httpclient.DoJSON[Order](ctx, client, req)
Error mapping
httpclient.MapStatusToError(404, "not found") // → xerrors.ErrNotFound
| HTTP status | xerrors code |
|---|---|
| 404 | ErrNotFound |
| 400 | ErrInvalidInput |
| 401 | ErrUnauthorized |
| 403 | ErrPermissionDenied |
| 409 | ErrAlreadyExists |
| 429 | ErrUnavailable |
| 5xx | ErrInternal |
Configuration
| Env var | Default | Description |
|---|---|---|
HTTP_CLIENT_NAME |
http |
Circuit breaker name |
HTTP_TIMEOUT |
30s |
Overall request timeout |
HTTP_DIAL_TIMEOUT |
5s |
TCP dial timeout |
HTTP_MAX_RETRIES |
3 |
Retry attempts |
HTTP_RETRY_DELAY |
1s |
Base retry delay |
HTTP_CB_THRESHOLD |
10 |
Consecutive failures before open |
HTTP_CB_TIMEOUT |
1m |
Time before half-open retry |
Releases
2
Release v1.0.0
Latest
Languages
Go
100%