probe

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CredentialLocationHeader = "header"
	CredentialLocationCookie = "cookie"
	CredentialLocationQuery  = "query"
	CredentialLocationBody   = "body"
)

Variables

View Source
var ErrMaxRetriesExceeded = errors.New("probe: max retries exceeded")

ErrMaxRetriesExceeded is returned when all retry attempts have been exhausted.

Functions

func Do added in v0.3.0

func Do(ctx context.Context, client *http.Client, req *http.Request) (statusCode int, header http.Header, body []byte, duration time.Duration, err error)

Do sends req via client and returns the response's status code, headers, fully-drained body, and duration. The response body is fully read and closed before Do returns.

func NewCredentialRequest added in v0.3.0

func NewCredentialRequest(ctx context.Context, target string, value string, loc CredentialLocation) (*http.Request, error)

NewCredentialRequest builds a request against target with value placed at loc. Header, cookie, and query placements produce a GET; body placement switches to POST via WithFormCredential.

func NewRequest added in v0.3.0

func NewRequest(ctx context.Context, method, target string, body io.Reader, mutators ...RequestMutator) (*http.Request, error)

NewRequest builds an *http.Request via http.NewRequestWithContext and applies mutators to it in order. It is the single entry point checks should use instead of calling http.NewRequestWithContext directly.

Types

type CredentialLocation added in v0.3.0

type CredentialLocation struct {
	In     string // CredentialLocationHeader (default), *Cookie, *Query, or *Body
	Name   string // header/cookie/query/form-field name
	Prefix string // value prefix, e.g. "Bearer "
}

CredentialLocation describes where to place a credential value in an HTTP request: a header (default), a cookie, a query parameter, or a form-encoded body field. It's the declarative counterpart to the With* mutators above, for callers (e.g. a security check) that pick the placement at runtime instead of hardcoding one mutator.

func (CredentialLocation) Validate added in v0.3.0

func (l CredentialLocation) Validate() error

Validate rejects an unknown In value.

func (CredentialLocation) WithDefaults added in v0.3.0

func (l CredentialLocation) WithDefaults() CredentialLocation

WithDefaults fills unset fields, defaulting to an Authorization: Bearer header when nothing is overridden.

type Option

type Option func(*config)

Option configures a Probe.

func WithMaxRetries

func WithMaxRetries(n int) Option

WithMaxRetries sets the maximum number of retry attempts (0 = no retries, one attempt only).

func WithRetryDelay

func WithRetryDelay(d time.Duration) Option

WithRetryDelay sets the base delay between retries. The actual delay grows exponentially with full jitter, capped at 60 seconds.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the timeout on the *http.Client returned by Client(). It has no effect on the underlying transport.

func WithTransport

func WithTransport(t http.RoundTripper) Option

WithTransport sets the underlying http.RoundTripper used for actual requests. Defaults to http.DefaultTransport.

func WithUserAgent

func WithUserAgent(ua string) Option

WithUserAgent sets the User-Agent header sent on every request.

type Probe

type Probe struct {
	// contains filtered or unexported fields
}

Probe implements http.RoundTripper with retry, rate-limit compliance, and User-Agent injection. Use Client() to get a standard *http.Client backed by this probe, giving access to the full net/http API with no verb restrictions.

func New

func New(opts ...Option) *Probe

New creates a Probe with the given options.

func (*Probe) Client

func (p *Probe) Client() *http.Client

Client returns a *http.Client backed by this probe. All requests made through the returned client benefit from retry, rate-limit compliance, and User-Agent injection — for any HTTP method.

func (*Probe) RoundTrip

func (p *Probe) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper. It injects the User-Agent header, retries on 429/503 and transient network errors, and respects rate-limit directives from Retry-After and X-RateLimit-Reset response headers.

type RequestBuilder added in v0.3.0

type RequestBuilder func(ctx context.Context) (*http.Request, error)

RequestBuilder builds a request for a single probe attempt.

type RequestMutator added in v0.3.0

type RequestMutator func(*http.Request) error

RequestMutator mutates a *http.Request built by NewRequest, in place. Mutators are applied in the order given; the first error returned short-circuits the chain and is returned from NewRequest.

func WithAPIKeyHeader added in v0.3.0

func WithAPIKeyHeader(name, value string) RequestMutator

WithAPIKeyHeader sets an arbitrary named header to value, e.g. a custom API-key header such as X-API-Key.

func WithAPIKeyQuery added in v0.3.0

func WithAPIKeyQuery(name, value string) RequestMutator

WithAPIKeyQuery sets an arbitrary named query parameter to value.

func WithAuthCookie added in v0.3.0

func WithAuthCookie(name, value string) RequestMutator

WithAuthCookie attaches a session/auth cookie with secure defaults (Secure, HttpOnly, SameSite=Strict).

func WithBasicAuth added in v0.3.0

func WithBasicAuth(username, password string) RequestMutator

WithBasicAuth sets HTTP Basic authentication credentials on the request.

func WithBearerToken added in v0.3.0

func WithBearerToken(token string) RequestMutator

WithBearerToken sets the Authorization header to "Bearer <token>".

func WithCookie added in v0.3.0

func WithCookie(cookie *http.Cookie) RequestMutator

WithCookie attaches a cookie to the request.

func WithFormCredential added in v0.3.0

func WithFormCredential(name, value string) RequestMutator

WithFormCredential places name=value as a urlencoded form body field, switching the request method to POST.

func WithHeader added in v0.3.0

func WithHeader(name, value string) RequestMutator

WithHeader sets (overwrites) a single header value.

func WithQuery added in v0.3.0

func WithQuery(name, value string) RequestMutator

WithQuery sets (overwrites) a single URL query parameter.

Jump to

Keyboard shortcuts

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