Documentation
¶
Overview ¶
Package httpx provides shared HTTP helpers for webhook-style sinks.
Index ¶
- Constants
- Variables
- func NewStatusError(status int, rawURL, retryAfterHeader string) error
- func PostJSON(ctx context.Context, dest string, payload any) error
- func PostJSONWithHeaders(ctx context.Context, dest string, payload any, policy RetryPolicy, ...) error
- func PostJSONWithRetry(ctx context.Context, dest string, payload any, policy RetryPolicy) error
- func Retriable(err error) bool
- func Retry(ctx context.Context, policy RetryPolicy, fn func(ctx context.Context) error) error
- type HeaderFunc
- type RetryPolicy
Constants ¶
const DefaultTimeout = 10 * time.Second
DefaultTimeout bounds every webhook POST in time. The 10s budget covers dial + TLS + body upload and is well within Slack/Teams rate-limit hints. Exported so sinks that must inject their own *http.Client (e.g. slack-go) share the same per-request ceiling instead of redefining the constant. This bounds one HTTP attempt; the surrounding per-sink and per-route budgets are documented at dispatchTimeout in controller.go.
Variables ¶
var DefaultRetry = RetryPolicy{MaxAttempts: 3, BaseDelay: 200 * time.Millisecond, MaxDelay: time.Second}
DefaultRetry is applied when PostJSON is called via the variadic overload with no explicit policy. Three attempts with exponential backoff and full jitter, capped at one second. The whole retry loop (attempts + backoff sleeps) runs inside the caller's context, so the per-sink timeout caps it; see the timeout budget at dispatchTimeout in controller.go.
Functions ¶
func NewStatusError ¶
NewStatusError builds an HTTP-status failure carrying an optional Retry-After hint; Retry honors both when scheduling the next attempt. rawURL is sanitized so webhook secrets never reach logs.
func PostJSON ¶
PostJSON marshals payload, POSTs it to url, and returns an error on transport failure or HTTP status >= 400. Empty url is a no-op. Transient failures (network errors, 408, 425, 429, 5xx) are retried with exponential backoff bounded by DefaultRetry. A `Retry-After` header is honored when present.
func PostJSONWithHeaders ¶
func PostJSONWithHeaders(ctx context.Context, dest string, payload any, policy RetryPolicy, header HeaderFunc) error
PostJSONWithHeaders is PostJSONWithRetry plus a hook to add per-request headers (auth tokens, HMAC signatures). It owns the marshal + retry + status-handling loop so sinks that need custom headers do not re-implement it. Empty dest is a no-op; header may be nil.
func PostJSONWithRetry ¶
PostJSONWithRetry is the explicit-policy form of PostJSON.
func Retriable ¶
Retriable reports whether err is worth retrying: retriable HTTP statuses (via statusError or any error exposing HTTPStatusCode, e.g. slack-go's StatusCodeError) and transport errors qualify; context cancellation and fatal 4xx do not.
func Retry ¶
Retry runs fn under the policy's exponential backoff until it succeeds, returns a non-retriable error, or attempts are exhausted. fn owns request construction so bodies are rebuilt per attempt. Sinks whose HTTP calls go through third-party clients (Slack, PagerDuty) wrap their sends with this.
Types ¶
type HeaderFunc ¶
HeaderFunc sets per-attempt request headers on the JSON POST. It receives the marshaled body so signatures (e.g. HMAC) can be computed over it, and runs on every retry so time-sensitive headers (timestamps, signatures) stay fresh within the receiver's replay window.