httpkit

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package httpkit provides shared HTTP client construction and utilities for all outbound HTTP calls in Thane. It enforces consistent timeouts, connection management, and good-citizen defaults across all packages.

Issue #53: Go's net.Dial intermittently fails on macOS with "no route to host" for LAN targets. The shared transport here sets explicit dial and TLS timeouts, limits idle connections, and provides a foundation for future diagnostics (GODEBUG=netdns=2, custom DialContext hooks, etc).

Index

Constants

View Source
const (
	// DefaultDialTimeout is the maximum time to establish a TCP connection.
	DefaultDialTimeout = 10 * time.Second

	// DefaultKeepAlive is the interval between TCP keep-alive probes.
	DefaultKeepAlive = 30 * time.Second

	// DefaultTLSHandshakeTimeout is the maximum time for the TLS handshake.
	DefaultTLSHandshakeTimeout = 10 * time.Second

	// DefaultResponseHeader is the maximum time to wait for response headers
	// after a request is fully written.
	DefaultResponseHeader = 15 * time.Second

	// DefaultIdleConnTimeout is how long idle connections stay in the pool.
	DefaultIdleConnTimeout = 90 * time.Second

	// DefaultMaxIdleConns is the total number of idle connections across all hosts.
	DefaultMaxIdleConns = 20

	// DefaultMaxIdleConnsPerHost is the per-host idle connection limit.
	DefaultMaxIdleConnsPerHost = 5
)

Default timeouts and connection pool limits for the shared transport.

Variables

This section is empty.

Functions

func DrainAndClose

func DrainAndClose(rc io.ReadCloser, limit int64)

DrainAndClose reads up to limit bytes from rc and closes it. Use to ensure HTTP connections are returned to the pool.

func NewClient

func NewClient(opts ...ClientOption) *http.Client

NewClient builds an *http.Client with the shared transport and good-citizen defaults (timeouts, User-Agent, connection limits).

func NewTransport

func NewTransport() *http.Transport

NewTransport creates an http.Transport with sensible defaults. This is the foundation for all outbound connections.

func ReadErrorBody

func ReadErrorBody(rc io.ReadCloser, limit int64) string

ReadErrorBody reads up to limit bytes from rc for error messages, then drains and closes the remainder to allow connection reuse. Returns an empty string if rc is nil.

Types

type ClientOption

type ClientOption func(*clientConfig)

ClientOption configures a Client built by NewClient.

func WithDisableKeepAlives

func WithDisableKeepAlives() ClientOption

WithDisableKeepAlives disables HTTP keep-alives on the transport.

func WithLogger

func WithLogger(l *slog.Logger) ClientOption

WithLogger sets a logger for retry diagnostics.

func WithRetry

func WithRetry(count int, delay time.Duration) ClientOption

WithRetry enables automatic retry on transient connection errors (e.g., EHOSTUNREACH, ENETUNREACH, connection refused). Retries are only attempted when the request body can be rewound, but this does not guarantee that the server has not already received the prior attempt. In practice, the retryable error set (dial/connect failures) occurs before any bytes reach the server. Designed to handle macOS ARP table race conditions (issue #53).

func WithTLSInsecureSkipVerify

func WithTLSInsecureSkipVerify() ClientOption

WithTLSInsecureSkipVerify skips TLS certificate verification. Use only for local/development targets.

func WithTimeout

func WithTimeout(d time.Duration) ClientOption

WithTimeout sets the overall request timeout on the http.Client. A zero value disables the timeout (useful for streaming responses).

func WithTransport

func WithTransport(t *http.Transport) ClientOption

WithTransport overrides the default shared transport. Use sparingly — the shared transport handles connection pooling.

func WithUserAgent

func WithUserAgent(ua string) ClientOption

WithUserAgent overrides the default User-Agent header.

func WithoutUserAgent

func WithoutUserAgent() ClientOption

WithoutUserAgent disables the automatic User-Agent roundtripper.

Jump to

Keyboard shortcuts

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