connectiontracker

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2021 License: BSD-2-Clause Imports: 4 Imported by: 0

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

func New(name string) *Tracker

New constructs a tracker object - in particular the map used to track each connection key

func (*Tracker) ConnState

func (t *Tracker) ConnState(key string, now time.Time, state http.ConnState) bool

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) Name

func (t *Tracker) Name() string

Name implements the reporter interface

func (*Tracker) Report

func (t *Tracker) Report(resetCounters bool) string

Name Report implements the reporter interface

func (*Tracker) SessionAdd

func (t *Tracker) SessionAdd(key string) bool

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

func (t *Tracker) SessionDone(key string) bool

SessionDone undoes SessionAdd.

Jump to

Keyboard shortcuts

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