connmgr

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package connmgr provides connection management for server scenarios.

This package implements connection limiting, tracking, and lifecycle management for high-concurrency server deployments.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrConnectionLimit is returned when the connection limit is reached.
	ErrConnectionLimit = errors.New("connection limit reached")
	// ErrManagerClosed is returned when the manager is closed.
	ErrManagerClosed = errors.New("connection manager closed")
)

Errors returned by the connection manager.

Functions

This section is empty.

Types

type Config

type Config struct {
	// MaxConnections is the maximum number of concurrent connections.
	// 0 means unlimited.
	MaxConnections int

	// ConnectionTimeout is the idle timeout for connections.
	// 0 means no timeout.
	ConnectionTimeout time.Duration

	// AcceptTimeout is the timeout for accepting new connections.
	AcceptTimeout time.Duration
}

Config holds the configuration for the connection manager.

type ConnInfo

type ConnInfo struct {
	ID         string
	RemoteAddr string
	StartTime  time.Time
	// contains filtered or unexported fields
}

ConnInfo holds information about a connection.

func (*ConnInfo) AddBytesIn

func (c *ConnInfo) AddBytesIn(n int64)

AddBytesIn adds to the bytes received counter.

func (*ConnInfo) AddBytesOut

func (c *ConnInfo) AddBytesOut(n int64)

AddBytesOut adds to the bytes sent counter.

func (*ConnInfo) BytesIn

func (c *ConnInfo) BytesIn() int64

BytesIn returns the total bytes received.

func (*ConnInfo) BytesOut

func (c *ConnInfo) BytesOut() int64

BytesOut returns the total bytes sent.

func (*ConnInfo) Close

func (c *ConnInfo) Close()

Close marks the connection as closed.

func (*ConnInfo) Duration

func (c *ConnInfo) Duration() time.Duration

Duration returns how long the connection has been active.

func (*ConnInfo) IsClosed

func (c *ConnInfo) IsClosed() bool

IsClosed returns whether the connection is closed.

func (*ConnInfo) LastActive

func (c *ConnInfo) LastActive() time.Time

LastActive returns the last active time.

func (*ConnInfo) UpdateActive

func (c *ConnInfo) UpdateActive()

UpdateActive updates the last active time to now.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages connections for server scenarios.

func NewManager

func NewManager(cfg Config) *Manager

NewManager creates a new connection manager.

func (*Manager) Accept

func (m *Manager) Accept(conn net.Conn) (*ConnInfo, error)

Accept processes a new connection. Returns ErrConnectionLimit if the connection limit is reached.

func (*Manager) Active

func (m *Manager) Active() int64

Active returns the number of active connections.

func (*Manager) CloseIdle

func (m *Manager) CloseIdle(timeout time.Duration) []string

CloseIdle closes connections that have been idle for longer than the timeout.

func (*Manager) ForEach

func (m *Manager) ForEach(fn func(id string, info *ConnInfo) bool)

ForEach iterates over all active connections.

func (*Manager) GetInfo

func (m *Manager) GetInfo(id string) (*ConnInfo, bool)

GetInfo returns the connection info for the given ID.

func (*Manager) GetStats

func (m *Manager) GetStats() Stats

GetStats returns current statistics.

func (*Manager) Rejected

func (m *Manager) Rejected() int64

Rejected returns the number of rejected connections.

func (*Manager) Release

func (m *Manager) Release(info *ConnInfo)

Release releases a connection.

func (*Manager) Shutdown

func (m *Manager) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the manager.

func (*Manager) Total

func (m *Manager) Total() int64

Total returns the total number of connections accepted.

func (*Manager) WrapConn

func (m *Manager) WrapConn(conn net.Conn, info *ConnInfo) *WrappedConn

WrapConn wraps a connection with management tracking.

type Stats

type Stats struct {
	Active      int64
	Total       int64
	Rejected    int64
	Limit       int
	Utilization float64
}

Stats returns statistics about the connection manager.

type WrappedConn

type WrappedConn struct {
	net.Conn
	// contains filtered or unexported fields
}

WrappedConn wraps a net.Conn with connection management.

func (*WrappedConn) Close

func (w *WrappedConn) Close() error

Close closes the connection and releases resources.

func (*WrappedConn) Info

func (w *WrappedConn) Info() *ConnInfo

Info returns the connection info.

func (*WrappedConn) Read

func (w *WrappedConn) Read(b []byte) (n int, err error)

Read reads from the connection and updates tracking.

func (*WrappedConn) Write

func (w *WrappedConn) Write(b []byte) (n int, err error)

Write writes to the connection and updates tracking.

Jump to

Keyboard shortcuts

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