connections

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewConnection

func NewConnection(id, userID, tenantID, orgID string, parentCtx context.Context) *conn

NewConnection creates a new connection. The caller must set WriteFn and then call StartWriter before using Send.

Types

type Connection

type Connection interface {
	ID() string
	UserID() string
	TenantID() string
	OrgID() string
	Send(msg *protocol.ServerMessage) bool
	Close()
	Context() context.Context
}

Connection represents an active WebSocket connection.

type Manager

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

Manager tracks all active WebSocket connections using a sharded map for high-concurrency access.

func NewManager

func NewManager(maxConns int64) *Manager

NewManager creates a connection manager with the given max connections limit.

func (*Manager) Deregister

func (m *Manager) Deregister(id string)

Deregister removes a connection.

func (*Manager) Get

func (m *Manager) Get(id string) Connection

Get returns a connection by ID, or nil.

func (*Manager) GetAll

func (m *Manager) GetAll() []Connection

GetAll returns all active connections. Use sparingly — iterates all shards.

func (*Manager) GetByTenant

func (m *Manager) GetByTenant(tenantID string) []Connection

GetByTenant returns all connections for a given tenant ID.

func (*Manager) GetByUser

func (m *Manager) GetByUser(userID string) []Connection

GetByUser returns all connections for a given user ID.

func (*Manager) Register

func (m *Manager) Register(c *conn) bool

Register adds a connection. Returns false if the max connections limit has been reached.

func (*Manager) SetPresenceListener

func (m *Manager) SetPresenceListener(l PresenceListener)

SetPresenceListener registers a listener notified on user presence transitions (0->1 and 1->0 live-connection edges). Not safe to call concurrently with Register/Deregister — set once during setup.

func (*Manager) TenantConnectionCount

func (m *Manager) TenantConnectionCount(tenantID string) int

TenantConnectionCount returns the number of connections for a given tenant.

func (*Manager) Total

func (m *Manager) Total() int64

Total returns the total number of active connections.

func (*Manager) UserConnectionCount

func (m *Manager) UserConnectionCount(userID string) int

UserConnectionCount returns the number of connections for a given user.

type Pool

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

Pool provides limit enforcement for per-user and per-tenant connections, and a bytes.Buffer pool for reducing GC pressure during encoding.

func NewPool

func NewPool(manager *Manager, maxPerUser, maxPerTenant int) *Pool

NewPool creates a new Pool.

func (*Pool) CheckLimits

func (p *Pool) CheckLimits(userID, tenantID string) error

CheckLimits returns an error if the user or tenant has reached their connection limit.

func (*Pool) GetBuffer

func (p *Pool) GetBuffer() *bytes.Buffer

GetBuffer returns a bytes.Buffer from the pool.

func (*Pool) PutBuffer

func (p *Pool) PutBuffer(buf *bytes.Buffer)

PutBuffer returns a bytes.Buffer to the pool.

type PresenceListener

type PresenceListener interface {
	OnPresenceChange(userID string, online bool)
}

PresenceListener is notified when a user transitions between having zero and having at least one live connection. It is invoked exactly on the 0->1 ("came online") and 1->0 ("went offline") edges, never on every individual connect/disconnect — a user with several simultaneous connections (e.g. multiple browser tabs plus mobile) only fires this once per edge.

Jump to

Keyboard shortcuts

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