Documentation
¶
Overview ¶
Package connectiontracker track connections for statistical purposes - ostensibly for inbound HTTP2 connections - but it is a generic package that should apply to other connections. The goal is to determine occupancy and concurrency on a per-listen-address basis and within a given connection for those connections which support sessions.
connectiontracker presents a reporter interface so its output can be periodically logged.
Typically usage is to create a connectiontracker for a given listen or connect address then call it via the http.Server.ConnState function or moral equivalent, i.e:
ct := connectiontracker.New("Name") s := http.Server{ConnState: func(c net.Conn, state ConnState) { ct.ConnState(c.RemoteAddr().String(), time.Now(), state) } ... time passes and requests occur fmt.Println(ct.Report(true))
If you are running a system in which connections can have multiple sessions such as HTTP2 then you should also call SessionAdd/SessionDone when sessions transition from active to closed. This is normally at the beginning of the request handler, such as:
ct.SessionAdd(http.Request.RemoteAddr) defer ct.SessionDone(http.Request.RemoteAddr)
The connection and session key can be any string you like so long as it is consistent and accurately reflects a unique connection endpoint. Normally it's a remote address/port and by virtue of the fact that a connectiontracker is associated with a server having a unique listen address the remote address/port/listen-address tuple makes the key appropriately unique.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Tracker ¶
type Tracker struct {
// contains filtered or unexported fields
}
func New ¶
New constructs a tracker object - in particular the map used to track each connection key
func (*Tracker) ConnState ¶
ConnState is called when a connection transitions to a new state. The key can be anything so long as it is unique per-connection though normally it will be the net.Conn.RemoteAddr() provided by http. So long as it's unique for a given connection tho, it's all good.
ConnState checks that the new state makes sense for the connection and if it does, the connection is updated and true is returned. If the new state doesn't make sense, the transition and internal state are reconciled and false is returned. Reconciliation favours the current state over the previous to avoid dangling connections.
ConnState does not fastidiously check that all state transitions make sense, it merely checks those which need to be correct for it to perform its function. This is a statistics gathering function after all, not a logic validation monster; besideswhich this function does not really know which transitions are legal in most cases.
func (*Tracker) SessionAdd ¶
SessionAdd increments a session counter within a connection. Not all connections support multiple sessions, but some such as HTTP2, do. Return false if the connection key is not know.
func (*Tracker) SessionDone ¶
SessionDone undoes SessionAdd.