Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultRetryDecider ¶
func DefaultRetryDecider(_ *http.Request, resp *http.Response, err error, now time.Time) (bool, time.Duration)
DefaultRetryDecider is generic: Retry-After on 429/503, transient 5xx, and retryable network errors.
func NewAuthenticatedClient ¶
func NewAuthenticatedClient(token string, base http.RoundTripper, allowedHosts ...string) *http.Client
NewAuthenticatedClient returns an HTTP client that adds a Bearer token only when the request URL's host matches one of allowedHosts (case-insensitive; port in allowedHosts entries is ignored; request hosts are compared via URL.Hostname()).
When token is empty, or no allowedHosts remain after normalization, it returns a client with only base (no Authorization injection), avoiding accidentally leaking credentials to unrelated hosts (e.g. release-asset CDNs).
When base is nil, it uses http.DefaultTransport.
Types ¶
type RateLimitStateExtractor ¶
type RateLimitStateExtractor func(resp *http.Response) (remaining int64, resetAt time.Time, ok bool)
RateLimitStateExtractor extracts provider-specific rate-limit state from a response. If ok is false, the transport keeps the previous values.
type RetryDecider ¶
type RetryDecider func(req *http.Request, resp *http.Response, err error, now time.Time) (retry bool, wait time.Duration)
RetryDecider decides whether a request should be retried and for how long. Returning wait=0 means the transport should use exponential backoff.
type RetryTransport ¶
type RetryTransport struct {
// Base is the underlying RoundTripper. If nil, http.DefaultTransport is used.
Base http.RoundTripper
// MaxRetries is the maximum number of retries per request.
MaxRetries int
// MaxBackoff caps exponential backoff sleep for transient retries.
MaxBackoff time.Duration
// Sleep is overridable for tests.
Sleep func(ctx context.Context, d time.Duration) error
// Now is overridable for tests.
Now func() time.Time
// Jitter is optional extra delay added to exponential backoff sleeps.
Jitter func() time.Duration
// Decider customizes retry policy.
Decider RetryDecider
// StateExtractor optionally records provider-specific rate-limit state.
StateExtractor RateLimitStateExtractor
// contains filtered or unexported fields
}
RetryTransport wraps an http.RoundTripper with retry and shared pause logic. It is safe for concurrent use.
func NewRetryTransport ¶
func NewRetryTransport(base http.RoundTripper) *RetryTransport
NewRetryTransport constructs a RetryTransport with sensible defaults.
func (*RetryTransport) PauseFor ¶
func (t *RetryTransport) PauseFor(pause time.Duration)
PauseFor records a rate-limit pause that all callers through this transport will respect.
func (*RetryTransport) RateLimitRemaining ¶
func (t *RetryTransport) RateLimitRemaining() int64
RateLimitRemaining returns the most recently observed provider remaining value.
func (*RetryTransport) RateLimitReset ¶
func (t *RetryTransport) RateLimitReset() time.Time
RateLimitReset returns the most recently observed provider reset time.