package httptrack

Import Path
	go.pact.im/x/httptrack (on go.dev)

Dependency Relation
	imports 4 packages, and imported by one package

Involved Source Files conn.go Package httptrack provides tools for tracking HTTP connection state transitions. It integrates with [net/http.Server] via the ConnState hook to support: - Graceful shutdowns (via [ConnTracker]) - Connection statistics (via [StatsTracker]) Trackers implement the [Tracker] interface and can be composed using [Compose] function. stats.go tracker.go
Code Examples package main import ( "net/http" "go.pact.im/x/httptrack" ) func main() { server := &http.Server{} var connTracker httptrack.ConnTracker server.ConnState = connTracker.Track // On shutdown: _ = server.Close() connTracker.Wait() // blocks until all connections are done } package main import ( "fmt" "net/http" "go.pact.im/x/httptrack" ) func main() { server := &http.Server{} var statsTracker httptrack.StatsTracker server.ConnState = statsTracker.Track stats := statsTracker.Stats() fmt.Println( "Active:", stats.Active(), "Idle:", stats.Idle(), "Closed:", stats.ClosedTotal(), ) } package main import ( "net/http" "go.pact.im/x/httptrack" ) func main() { server := &http.Server{} var connTracker httptrack.ConnTracker var statsTracker httptrack.StatsTracker httptrack.Wrap(server, httptrack.Compose( &statsTracker, &connTracker, )) }
Package-Level Type Names (total 5, all are exported)
/* sort exporteds by: | */
ConnTracker is a connection tracker for HTTP connections. It can be used to wait for all existing HTTP connections to finish after performing HTTP server shutdown. Note that [http.Server.Close] closes all connections but does not wait for per-connection goroutines to return. If the connection was active, it is up to the running [http.Handler] to handle connection error and eventually return. ConnTracker allows waiting for all in-flight handlers. Track updates the connection counter based on the connection’s state. It should be assigned to the ConnState field of an [http.Server]. Wait blocks until all tracked HTTP connections have completed. It should be called after [http.Server.Serve] has returned. *ConnTracker : Tracker
Stats holds a snapshot of HTTP connection state statistics. Accepted returns the number of connections currently in the [http.StateNew] state. A non-zero value typically indicates that there are accepted connections which have not yet sent a complete HTTP request. This may occur naturally under normal traffic, but a persistently high number of connections in this state can suggest: - Slow or high-latency clients - Clients intentionally delaying requests (e.g. denial-of-service attack) - Server under heavy load or accepting connections faster than it can process them AcceptedTotal returns the total number of connections ever seen in the [http.StateNew] state. This is a monotonically increasing counter that reflects the cumulative number of accepted connections. It includes all connections, regardless of whether they were later closed, hijacked, or reused. Active returns the number of connections currently in the [http.StateActive] state. In HTTP/1.x, this usually means that a request is currently being processed. In HTTP/2, this state indicates that the connection has at least one open stream. However, the connection may briefly enter the Active state after being accepted as part of reading the HTTP/2 connection preface, before transitioning to Idle state. Therefore, a connection in the Active state is not guaranteed to be actively handling a request. ActiveTotal returns the total number of times connections have transitioned into the [http.StateActive] state. This counter increases every time a connection becomes active. It does not represent the number of requests, since a single connection may enter Active state for multiple requests (e.g. HTTP/2 streams). ClosedTotal returns the number of connections that have been closed. A non-zero Closed count indicates connections that have been fully closed by either the server or the client, including normal connection termination and error scenarios. High numbers in Closed are typical over time, but a sudden spike may indicate: - Mass client disconnects - Server-side timeouts or errors - Deployment cycles or load balancer resets HijackedTotal returns the number of connections that have been hijacked via [http.Hijacker]. A hijacked connection is taken over by the application (e.g. for WebSocket upgrade) and is no longer managed by the HTTP server. Idle returns the number of connections currently in the [http.StateIdle] state. In HTTP/1.x, Idle connections are typically those that have completed a request and are being kept alive. A non-zero Idle count is normal in servers that support HTTP keep-alive. In HTTP/2, this state indicates that the connection has no open streams. A newly established HTTP/2 connection enters the Idle state after reading the connection preface. IdleTotal returns the total number of times connections have entered the [http.StateIdle] state. func (*StatsTracker).Stats() Stats
StatsTracker is a [Tracker] implementation that keeps counts of HTTP connections in each state. It can be used for monitoring connections and their lifecycle. Connections returns a snapshot of connection states. Stats returns a snapshot of connection statistics. Track records the transition of a connection’s state. It updates internal statistics and tracks the current state of each connection. The dynamic type of the provided net.Conn must be comparable (i.e. usable as a map key). Most standard connection types (such as tls.Conn pointer) meet this requirement. Passing a non-comparable type will cause a runtime panic. *StatsTracker : Tracker
Tracker is an interface for tracking HTTP connection state transitions. It matches the signature required by [http.Server.ConnState], allowing implementations to observe and respond to connection lifecycle events. ( Tracker) Track(net.Conn, http.ConnState) *ConnTracker *StatsTracker TrackerFunc func Compose(hooks ...Tracker) TrackerFunc func Wrap(s *http.Server, h Tracker) TrackerFunc
TrackerFunc is an adapter to allow the use of ordinary functions as [Tracker] implementations. If f is a function with the appropriate signature, TrackerFunc(f) is a [Tracker] that calls f. Track calls f(conn, state). TrackerFunc : Tracker func Compose(hooks ...Tracker) TrackerFunc func Wrap(s *http.Server, h Tracker) TrackerFunc
Package-Level Functions (total 2, both are exported)
Compose returns a TrackerFunc that invokes multiple [Tracker] hooks. Each call to Track on the returned TrackerFunc will call Track on all provided Tracker implementations, in the order they are passed. When using a [ConnTracker] for graceful shutdown, it should be passed as the last argument to Compose. This ensures that other trackers have completed their processing before the server is allowed to shut down.
Wrap configures an [http.Server] to use the given [Tracker] via server’s ConnState hook. If ConnState is already set, it is preserved and runs before the provided [Tracker], and Wrap returns the old value.
Package-Level Constants (total 2, neither is exported)