session

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package session manages live sessions and their channels.

Package session is the in-memory session and channel registry.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrSessionClosed is returned when using a session that has been closed.
	ErrSessionClosed = errors.New("session: closed")
	// ErrSessionLimit is returned when a user is at their max session count.
	ErrSessionLimit = errors.New("session: per-user limit reached")
	// ErrChannelLimit is returned when a session is at its max channel count.
	ErrChannelLimit = errors.New("session: per-session channel limit reached")
)

Functions

This section is empty.

Types

type ConnectFunc

type ConnectFunc func(ctx context.Context) (plugin.Session, error)

ConnectFunc lazily opens the upstream session on first use.

type Handle

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

Handle is a borrowed reference to a live session. It opens tracked channels and keeps the session marked as recently used.

func (*Handle) Close

func (h *Handle) Close() error

func (*Handle) HealthCheck

func (h *Handle) HealthCheck(ctx context.Context) error

func (*Handle) OpenChannel

func (h *Handle) OpenChannel(ctx context.Context, req plugin.ChannelRequest) (plugin.Channel, error)

OpenChannel opens a tracked upstream stream, enforcing the per-session channel cap. The returned channel decrements the counter exactly once on Close.

func (*Handle) Session

func (h *Handle) Session() plugin.Session

Session returns the underlying plugin session.

func (*Handle) Snapshot

func (h *Handle) Snapshot() Snapshot

Snapshot returns the current registry state for this borrowed session.

func (*Handle) TrackStream

func (h *Handle) TrackStream() func()

TrackStream pins the session while a browser stream is active. Not every plugin stream maps to an upstream Channel, but an open WS still means the session is in use and must not be reclaimed as idle.

type Key

type Key struct {
	ConnectionID string
	OwnerScope   string
}

Key identifies one live session for an actor's scope on a connection.

type Manager

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

Manager owns the session registry and lifecycle.

func New

func New(opts Options) *Manager

New starts a manager and its background janitor.

func (*Manager) Acquire

func (m *Manager) Acquire(ctx context.Context, key Key, userID string, connect ConnectFunc) (*Handle, error)

Acquire returns the live session for key, lazily connecting on first use.

func (*Manager) Close

func (m *Manager) Close(key Key)

Close closes and removes the session for key, if present.

func (*Manager) CloseConnection

func (m *Manager) CloseConnection(connectionID string)

CloseConnection closes and removes every live session for a connection across all owner scopes. Callers use this after connection config changes so cached plugin options cannot outlive the saved connection state.

func (*Manager) IdleTimeout

func (m *Manager) IdleTimeout() time.Duration

IdleTimeout returns the configured idle session timeout.

func (*Manager) Shutdown

func (m *Manager) Shutdown()

Shutdown stops the janitor and closes every live session.

func (*Manager) Stats

func (m *Manager) Stats() Stats

Stats returns the current session/channel counts.

func (*Manager) Status

func (m *Manager) Status(key Key) (Snapshot, bool)

Status returns a snapshot for key without creating or connecting a session.

type Options

type Options struct {
	IdleTimeout           time.Duration
	MaxSessionsPerUser    int
	MaxChannelsPerSession int
	HealthInterval        time.Duration
	FailureRetention      time.Duration
}

Options bound the registry. Zero values fall back to sensible defaults.

type Snapshot

type Snapshot struct {
	Key             Key
	UserID          string
	State           State
	Reason          string
	Channels        int
	Streams         int
	LastUsed        time.Time
	CreatedAt       time.Time
	LastHealthCheck time.Time
}

Snapshot is a point-in-time view of one live registry entry.

type State

type State string

State is the lifecycle state of a registry entry.

const (
	StateConnecting State = "connecting"
	StateConnected  State = "connected"
	StateClosed     State = "closed"
	StateError      State = "error"
)

type Stats

type Stats struct {
	Sessions int
	Channels int
}

Stats is a point-in-time snapshot for telemetry.

Jump to

Keyboard shortcuts

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