session

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package session provides session management for mcpdrill VUs.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSessionExpired = &SessionError{Op: "use", Err: errSessionExpired}
	ErrSessionClosed  = &SessionError{Op: "use", Err: errSessionClosed}
	ErrPoolExhausted  = &SessionError{Op: "acquire", Err: errPoolExhausted}
	ErrPoolTimeout    = &SessionError{Op: "acquire", Err: errPoolTimeout}
	ErrManagerClosed  = &SessionError{Op: "acquire", Err: errManagerClosed}
	ErrInvalidConfig  = &SessionError{Op: "create", Err: errInvalidConfig}
)

Common session errors.

Functions

This section is empty.

Types

type ChurnMode

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

func NewChurnMode

func NewChurnMode(config *SessionConfig) *ChurnMode

func (*ChurnMode) Acquire

func (cm *ChurnMode) Acquire(ctx context.Context, vuID string) (*SessionInfo, error)

func (*ChurnMode) ChurnIntervalOps

func (cm *ChurnMode) ChurnIntervalOps() int64

func (*ChurnMode) Close

func (cm *ChurnMode) Close(ctx context.Context) error

func (*ChurnMode) Invalidate

func (cm *ChurnMode) Invalidate(ctx context.Context, session *SessionInfo) error

func (*ChurnMode) IsOpsBased

func (cm *ChurnMode) IsOpsBased() bool

func (*ChurnMode) Metrics

func (cm *ChurnMode) Metrics() *SessionMetrics

func (*ChurnMode) Release

func (cm *ChurnMode) Release(ctx context.Context, session *SessionInfo) error

type EvictionCallback

type EvictionCallback func(session *SessionInfo, reason EvictionReason)

type EvictionReason

type EvictionReason string
const (
	EvictionTTL  EvictionReason = "ttl"
	EvictionIdle EvictionReason = "idle"
)

type Evictor

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

func NewEvictor

func NewEvictor(ttlMs, maxIdleMs int64, callback EvictionCallback) *Evictor

func (*Evictor) IdleEvictions

func (e *Evictor) IdleEvictions() int64

func (*Evictor) Start

func (e *Evictor) Start(ctx context.Context)

func (*Evictor) Stop

func (e *Evictor) Stop()

func (*Evictor) TTLEvictions

func (e *Evictor) TTLEvictions() int64

func (*Evictor) Track

func (e *Evictor) Track(session *SessionInfo)

func (*Evictor) Untrack

func (e *Evictor) Untrack(sessionID string)

type Manager

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

func NewManager

func NewManager(config *SessionConfig) (*Manager, error)

func (*Manager) Acquire

func (m *Manager) Acquire(ctx context.Context, vuID string) (*SessionInfo, error)

func (*Manager) Close

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

func (*Manager) Config

func (m *Manager) Config() *SessionConfig

func (*Manager) Invalidate

func (m *Manager) Invalidate(ctx context.Context, session *SessionInfo) error

func (*Manager) Metrics

func (m *Manager) Metrics() *SessionMetrics

func (*Manager) Mode

func (m *Manager) Mode() SessionMode

func (*Manager) Release

func (m *Manager) Release(ctx context.Context, session *SessionInfo) error

func (*Manager) Start

func (m *Manager) Start(ctx context.Context)

type ModeHandler

type ModeHandler interface {
	Acquire(ctx context.Context, vuID string) (*SessionInfo, error)
	Release(ctx context.Context, session *SessionInfo) error
	Invalidate(ctx context.Context, session *SessionInfo) error
	Close(ctx context.Context) error
	Metrics() *SessionMetrics
}

type PerRequestMode

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

func NewPerRequestMode

func NewPerRequestMode(config *SessionConfig) *PerRequestMode

func (*PerRequestMode) Acquire

func (pm *PerRequestMode) Acquire(ctx context.Context, vuID string) (*SessionInfo, error)

func (*PerRequestMode) Close

func (pm *PerRequestMode) Close(ctx context.Context) error

func (*PerRequestMode) Invalidate

func (pm *PerRequestMode) Invalidate(ctx context.Context, session *SessionInfo) error

func (*PerRequestMode) Metrics

func (pm *PerRequestMode) Metrics() *SessionMetrics

func (*PerRequestMode) Release

func (pm *PerRequestMode) Release(ctx context.Context, session *SessionInfo) error

type PoolAcquireOptions

type PoolAcquireOptions struct {
	Timeout time.Duration
}

type PoolMode

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

func NewPoolMode

func NewPoolMode(config *SessionConfig) *PoolMode

func (*PoolMode) Acquire

func (pm *PoolMode) Acquire(ctx context.Context, vuID string) (*SessionInfo, error)

func (*PoolMode) Close

func (pm *PoolMode) Close(ctx context.Context) error

func (*PoolMode) Invalidate

func (pm *PoolMode) Invalidate(ctx context.Context, session *SessionInfo) error

func (*PoolMode) Metrics

func (pm *PoolMode) Metrics() *SessionMetrics

func (*PoolMode) Release

func (pm *PoolMode) Release(ctx context.Context, session *SessionInfo) error

func (*PoolMode) Start

func (pm *PoolMode) Start(ctx context.Context)

type ReuseMode

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

func NewReuseMode

func NewReuseMode(config *SessionConfig) *ReuseMode

func (*ReuseMode) Acquire

func (rm *ReuseMode) Acquire(ctx context.Context, vuID string) (*SessionInfo, error)

func (*ReuseMode) Close

func (rm *ReuseMode) Close(ctx context.Context) error

func (*ReuseMode) Invalidate

func (rm *ReuseMode) Invalidate(ctx context.Context, session *SessionInfo) error

func (*ReuseMode) Metrics

func (rm *ReuseMode) Metrics() *SessionMetrics

func (*ReuseMode) Release

func (rm *ReuseMode) Release(ctx context.Context, session *SessionInfo) error

func (*ReuseMode) Start

func (rm *ReuseMode) Start(ctx context.Context)

type SessionConfig

type SessionConfig struct {
	// Mode determines how sessions are managed.
	Mode SessionMode

	// PoolSize is the maximum number of sessions in the pool (for pool mode).
	// Must be > 0 for pool mode.
	PoolSize int

	// TTLMs is the maximum lifetime of a session in milliseconds.
	// 0 means no TTL limit.
	TTLMs int64

	// MaxIdleMs is the maximum idle time before a session is evicted.
	// 0 means no idle limit.
	MaxIdleMs int64

	// ChurnIntervalMs is the interval between session rotations (for churn mode).
	// Only used when Mode is ModeChurn. If 0, uses ChurnIntervalOps instead.
	ChurnIntervalMs int64

	// ChurnIntervalOps is the number of operations after which to churn the session.
	// Only used when Mode is ModeChurn and ChurnIntervalMs is 0.
	// Default is 1 (churn after every operation).
	ChurnIntervalOps int64

	// TransportConfig is the configuration for creating transport connections.
	TransportConfig *transport.TransportConfig

	// Adapter is the transport adapter used to create connections.
	Adapter transport.Adapter

	// ProtocolVersion is the MCP protocol version to use for initialization.
	// Empty string means use default (mcp.DefaultProtocolVersion).
	ProtocolVersion string

	// ProtocolVersionPolicy determines how to handle version negotiation.
	// Empty string means use strict policy.
	ProtocolVersionPolicy mcp.VersionPolicy
}

SessionConfig holds configuration for session management.

func DefaultSessionConfig

func DefaultSessionConfig() *SessionConfig

DefaultSessionConfig returns a default session configuration.

type SessionError

type SessionError struct {
	Op      string // Operation that failed
	Session string // Session ID (if available)
	Err     error  // Underlying error
}

SessionError represents an error from session management.

func (*SessionError) Error

func (e *SessionError) Error() string

func (*SessionError) Unwrap

func (e *SessionError) Unwrap() error

type SessionInfo

type SessionInfo struct {
	// ID is the session identifier (from MCP server).
	ID string

	// VUID is the VU that owns this session (for reuse/per_request modes).
	// Empty for pool mode sessions.
	VUID string

	// State is the current session state.
	State SessionState

	// CreatedAt is when the session was created.
	CreatedAt time.Time

	// LastUsedAt is when the session was last used.
	LastUsedAt time.Time

	// ExpiresAt is when the session will expire (based on TTL).
	// Zero value means no expiration.
	ExpiresAt time.Time

	// IdleExpiresAt is when the session will expire due to idle timeout.
	// Updated on each use.
	IdleExpiresAt time.Time

	// OperationCount is the number of operations performed on this session.
	OperationCount int64

	// Connection is the underlying transport connection.
	Connection transport.Connection
	// contains filtered or unexported fields
}

SessionInfo contains information about a session.

func NewSessionInfo

func NewSessionInfo(id string, conn transport.Connection, ttlMs, maxIdleMs int64) *SessionInfo

NewSessionInfo creates a new SessionInfo.

func (*SessionInfo) GetState

func (s *SessionInfo) GetState() SessionState

GetState returns the current session state.

func (*SessionInfo) IsExpired

func (s *SessionInfo) IsExpired() bool

IsExpired checks if the session has expired (TTL or idle).

func (*SessionInfo) SetState

func (s *SessionInfo) SetState(state SessionState)

SetState updates the session state.

func (*SessionInfo) Touch

func (s *SessionInfo) Touch(maxIdleMs int64)

Touch updates the last used time and resets idle expiration.

type SessionManager

type SessionManager interface {
	// Acquire gets a session for the given VU.
	// For reuse mode, returns the VU's existing session or creates one.
	// For per_request mode, always creates a new session.
	// For pool mode, borrows a session from the pool.
	// For churn mode, may create a new session based on churn interval.
	Acquire(ctx context.Context, vuID string) (*SessionInfo, error)

	// Release returns a session after use.
	// For reuse mode, marks the session as idle.
	// For per_request mode, closes the session.
	// For pool mode, returns the session to the pool.
	// For churn mode, may close the session based on churn policy.
	Release(ctx context.Context, session *SessionInfo) error

	// Invalidate marks a session as invalid (e.g., after 404).
	// The session will be closed and a new one created on next Acquire.
	Invalidate(ctx context.Context, session *SessionInfo) error

	// Close shuts down the session manager and closes all sessions.
	Close(ctx context.Context) error

	// Metrics returns current session metrics.
	Metrics() *SessionMetrics

	// Mode returns the session mode.
	Mode() SessionMode
}

SessionManager defines the interface for session management.

type SessionMetrics

type SessionMetrics struct {
	// ActiveSessions is the current number of active sessions.
	ActiveSessions int64

	// IdleSessions is the current number of idle sessions.
	IdleSessions int64

	// TotalCreated is the total number of sessions created.
	TotalCreated int64

	// TotalEvicted is the total number of sessions evicted.
	TotalEvicted int64

	// TTLEvictions is the number of sessions evicted due to TTL.
	TTLEvictions int64

	// IdleEvictions is the number of sessions evicted due to idle timeout.
	IdleEvictions int64

	// Reconnects is the number of session reconnects (after 404).
	Reconnects int64

	// PoolWaits is the number of times a VU had to wait for a pool slot.
	PoolWaits int64

	// PoolTimeouts is the number of times a VU timed out waiting for a pool slot.
	PoolTimeouts int64
}

SessionMetrics contains metrics about session management.

type SessionMode

type SessionMode string

SessionMode represents the session management mode.

const (
	// ModeReuse keeps one session per VU for entire stage duration.
	// Re-initializes on 404 (session expired on server).
	ModeReuse SessionMode = "reuse"

	// ModePerRequest creates a new session (initialize) before every operation.
	// High overhead, useful for testing session creation paths.
	ModePerRequest SessionMode = "per_request"

	// ModePool shares a connection pool among VUs with session affinity.
	// Sessions are borrowed from pool and returned after use.
	ModePool SessionMode = "pool"

	// ModeChurn deliberately rotates sessions to stress creation/teardown.
	// Sessions are created and destroyed at configurable intervals.
	ModeChurn SessionMode = "churn"
)

type SessionPool

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

func NewSessionPool

func NewSessionPool(maxSize int, ttlMs, maxIdleMs int64) *SessionPool

func (*SessionPool) Acquire

func (p *SessionPool) Acquire(ctx context.Context) (*SessionInfo, bool, error)

func (*SessionPool) AcquireWithTimeout

func (p *SessionPool) AcquireWithTimeout(ctx context.Context, timeout time.Duration) (*SessionInfo, bool, error)

func (*SessionPool) Add

func (p *SessionPool) Add(session *SessionInfo)

func (*SessionPool) Available

func (p *SessionPool) Available() int

func (*SessionPool) CancelReservation

func (p *SessionPool) CancelReservation()

func (*SessionPool) Close

func (p *SessionPool) Close()

func (*SessionPool) IdleEvictions

func (p *SessionPool) IdleEvictions() int64

func (*SessionPool) InUse

func (p *SessionPool) InUse() int

func (*SessionPool) PoolTimeouts

func (p *SessionPool) PoolTimeouts() int64

func (*SessionPool) PoolWaits

func (p *SessionPool) PoolWaits() int64

func (*SessionPool) Release

func (p *SessionPool) Release(session *SessionInfo)

func (*SessionPool) Remove

func (p *SessionPool) Remove(session *SessionInfo)

func (*SessionPool) Size

func (p *SessionPool) Size() int

func (*SessionPool) Start

func (p *SessionPool) Start(ctx context.Context)

func (*SessionPool) TTLEvictions

func (p *SessionPool) TTLEvictions() int64

func (*SessionPool) TotalCreated

func (p *SessionPool) TotalCreated() int64

type SessionState

type SessionState string

SessionState represents the current state of a session.

const (
	// StateCreating indicates the session is being initialized.
	StateCreating SessionState = "creating"

	// StateActive indicates the session is ready for operations.
	StateActive SessionState = "active"

	// StateIdle indicates the session is not currently in use.
	StateIdle SessionState = "idle"

	// StateExpired indicates the session has been evicted (TTL or idle timeout).
	StateExpired SessionState = "expired"

	// StateClosed indicates the session has been explicitly closed.
	StateClosed SessionState = "closed"
)

type SessionTimer

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

func NewSessionTimer

func NewSessionTimer(session *SessionInfo, ttlMs, maxIdleMs int64, callback EvictionCallback) *SessionTimer

func (*SessionTimer) Stop

func (st *SessionTimer) Stop()

func (*SessionTimer) Touch

func (st *SessionTimer) Touch(maxIdleMs int64)

Jump to

Keyboard shortcuts

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