Documentation
¶
Overview ¶
Package httpclient wraps net/http with retry, circuit breaker, and tracing so consumers stop reimplementing the same outbound-call boilerplate.
c := httpclient.New(
httpclient.WithBaseURL("https://api.example.com"),
httpclient.WithTimeout(5 * time.Second),
httpclient.WithRetries(3),
httpclient.WithBreaker(httpclient.NewBreaker()),
)
res, err := c.Do(ctx, req)
Index ¶
- Variables
- func IsRetriable(err error) bool
- type Breaker
- type BreakerOption
- type Client
- type Option
- func WithBackoff(opts ...retry.Option) Option
- func WithBaseURL(url string) Option
- func WithBreaker(b *Breaker) Option
- func WithHTTPClient(c *http.Client) Option
- func WithHeaders(fn func() http.Header) Option
- func WithRetries(n int) Option
- func WithTimeout(d time.Duration) Option
- func WithTransport(rt http.RoundTripper) Option
- type State
Constants ¶
This section is empty.
Variables ¶
var ErrBreakerOpen = errors.New("httpclient: circuit breaker open")
ErrBreakerOpen is returned by the client when the circuit is open.
Functions ¶
func IsRetriable ¶
IsRetriable reports whether an error is one the client retried on.
Types ¶
type Breaker ¶
type Breaker struct {
// contains filtered or unexported fields
}
Breaker is a small failure-rate-based circuit breaker.
func NewBreaker ¶
func NewBreaker(opts ...BreakerOption) *Breaker
NewBreaker constructs a breaker. Defaults: threshold 5, cooldown 30s.
func (*Breaker) RecordFailure ¶
func (b *Breaker) RecordFailure()
RecordFailure increments the consecutive-failure counter and trips the breaker once it reaches the threshold.
type BreakerOption ¶
type BreakerOption func(*Breaker)
func WithCooldown ¶
func WithCooldown(d time.Duration) BreakerOption
func WithThreshold ¶
func WithThreshold(n int) BreakerOption
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an http.Client wrapper with retry + breaker + per-attempt timeout.
type Option ¶
type Option func(*Client)
func WithBackoff ¶
WithBackoff overrides the retry policy options. Defaults to exponential backoff starting at 100ms.
func WithBaseURL ¶
WithBaseURL prepends a base URL to relative request paths.
func WithBreaker ¶
WithBreaker installs a circuit breaker. When the breaker is open, Do returns ErrBreakerOpen immediately without contacting the server.
func WithHTTPClient ¶
WithHTTPClient overrides the underlying http.Client. Useful when callers want shared connection pooling. Resets timeout to the supplied client's.
func WithHeaders ¶
WithHeaders installs a header factory invoked per attempt. Headers from the inbound request are preserved; factory headers are added on top (typical use: Authorization, X-Tenant-ID).
func WithRetries ¶
WithRetries sets the total number of attempts (1 = no retry, default 1).
func WithTimeout ¶
WithTimeout sets the per-attempt timeout. The http.Client's own timeout is left alone; this wraps the request context.
func WithTransport ¶
func WithTransport(rt http.RoundTripper) Option
WithTransport overrides the RoundTripper. Combine with NewTracingTransport from go/kit/transport/rest for OTEL spans.