httpx

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package httpx provides shared HTTP helpers for webhook-style sinks.

Index

Constants

View Source
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

View Source
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

func NewStatusError(status int, rawURL, retryAfterHeader string) error

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

func PostJSON(ctx context.Context, dest string, payload any) error

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

func PostJSONWithRetry(ctx context.Context, dest string, payload any, policy RetryPolicy) error

PostJSONWithRetry is the explicit-policy form of PostJSON.

func Retriable

func Retriable(err error) bool

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

func Retry(ctx context.Context, policy RetryPolicy, fn func(ctx context.Context) error) error

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

type HeaderFunc func(req *http.Request, body []byte)

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.

type RetryPolicy

type RetryPolicy struct {
	MaxAttempts int           // total attempts including the first call
	BaseDelay   time.Duration // initial backoff, doubled per attempt
	MaxDelay    time.Duration // ceiling on backoff
}

RetryPolicy controls how PostJSON retries transient failures.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL