httpx

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package httpx is Stampede's HTTP/1.1 and HTTP/2 driver. It builds clients that behave like real users (per-user connection pools, cookie jars, TLS session reuse) and measures every request phase with httptrace.

Index

Constants

View Source
const ErrorSnippet = 512

Do sends req and reads the response. Up to maxBody bytes are kept in Result.Body; the rest is read and counted but discarded so connections can be reused and download time is measured fully. keepBody=false discards the body, except the first ErrorSnippet bytes of an error response (status 400 and above), kept for error examples. ErrorSnippet is how much of an error response's body Do keeps when the caller did not ask for the body.

Variables

This section is empty.

Functions

func ClassifyError

func ClassifyError(err error) string

ClassifyError maps a transport error to a short, bounded label used in reports.

func Dialer

func Dialer(o Options) func(ctx context.Context, network, addr string) (net.Conn, error)

Dialer is the dial function the options describe: the test or emulation dialer if set, else a TCP dialer that uses the DNS cache. Other drivers (gRPC) dial through it so they resolve names the same way.

func NewClient

func NewClient(t http.RoundTripper, o Options) *http.Client

NewClient wraps a transport with a fresh cookie jar and redirect policy.

func NewTransport

func NewTransport(o Options) *http.Transport

NewTransport builds a transport. One per virtual user gives browser-like connection behaviour; one shared transport behaves like a service client.

func ResetCookies

func ResetCookies(c *http.Client)

ResetCookies gives the client an empty cookie jar, as for a new session.

func StatusError

func StatusError(code int) string

StatusError labels an HTTP error status.

func Trace

func Trace(ctx context.Context, r *Result) context.Context

Trace returns a context that records the phases of the HTTP exchange made with it into r. Drivers whose library sends the request itself, such as a WebSocket handshake, use it to get the same phase timings, then call Finish.

Types

type DNSCache

type DNSCache struct {
	TTL      time.Duration
	Resolver *net.Resolver
	// contains filtered or unexported fields
}

DNSCache resolves host names once per TTL and shares the answer between virtual users, the way an operating system's resolver cache does for real clients. Without it every new connection waits on a lookup, which skews connect timing and can stall a load generator during ramp-up.

func NewDNSCache

func NewDNSCache(ttl time.Duration) *DNSCache

NewDNSCache caches answers for ttl (default 30s).

func (*DNSCache) DialContext

func (c *DNSCache) DialContext(d *net.Dialer) func(ctx context.Context, network, addr string) (net.Conn, error)

DialContext returns a dial function that resolves through the cache.

func (*DNSCache) Lookup

func (c *DNSCache) Lookup(ctx context.Context, host string) (netip.Addr, error)

Lookup returns the next address for host, round-robin across answers.

type Options

type Options struct {
	HTTP2 bool
	// H2C speaks HTTP/2 without TLS to http:// URLs, with prior knowledge
	// (no upgrade from HTTP/1.1), as gRPC and many internal services do.
	H2C                bool
	DisableKeepAlive   bool
	InsecureSkipVerify bool
	// MaxRedirects: 0 means the default of 10, negative disables following.
	MaxRedirects int
	// MaxConnsPerHost bounds a shared pool (0 = unlimited).
	MaxConnsPerHost int
	// Dialer allows tests and network emulation to replace dialing.
	DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
	// DNS caches lookups across connections; nil resolves on every dial.
	DNS *DNSCache
	// TLSResumption is where TLS sessions are kept for resumption: "shared"
	// (one cache for the process, the default), "per-transport" (a cache
	// per transport: per virtual user when each has its own) or "off"
	// (every connection makes a full handshake).
	TLSResumption string
}

Options configures a client.

type Result

type Result struct {
	Start    time.Time
	End      time.Time
	Phases   [metrics.NumPhases]time.Duration
	Status   int
	Header   http.Header
	Cookies  []*http.Cookie
	Body     []byte
	BytesIn  int64
	BytesOut int64
	Proto    string
	Reused   bool
	Err      error
	// contains filtered or unexported fields
}

Result is a completed exchange.

func Do

func Do(c *http.Client, req *http.Request, bodyLen int64, keepBody bool, maxBody int64) *Result

func Open

func Open(c *http.Client, req *http.Request, bodyLen int64) (*Result, *http.Response)

Open sends req and returns once the response headers have arrived, for drivers that read a streamed body themselves. When the response is non-nil the caller must read and close its body, then call Finish. On a transport error the response is nil and the Result is complete.

func (*Result) Finish

func (r *Result) Finish(bodyBytes int64, err error)

Finish ends an exchange started by Open: bodyBytes were read from the response body and err is the error that stopped reading, if any.

Jump to

Keyboard shortcuts

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