session

package
v0.0.0-...-b198e9d Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMaxSessionsReached = &SessionError{Message: "maximum sessions limit reached"}
	ErrSessionNotFound    = &SessionError{Message: "session not found"}
)

Errors

Functions

This section is empty.

Types

type Manager

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

Manager handles session lifecycle and storage.

func NewManager

func NewManager(cfg ManagerConfig) *Manager

NewManager creates a new session manager.

func (*Manager) ActiveCount

func (m *Manager) ActiveCount() int

ActiveCount returns the number of active sessions.

func (*Manager) Create

func (m *Manager) Create(ctx context.Context) (*Session, error)

Create creates a new session and returns it.

func (*Manager) Delete

func (m *Manager) Delete(sessionID string)

Delete removes a session.

func (*Manager) Get

func (m *Manager) Get(sessionID string) (*Session, bool)

Get retrieves a session by ID.

func (*Manager) IncrementRequestCount

func (m *Manager) IncrementRequestCount(sessionID string) int

IncrementRequestCount increments the request count for a session.

func (*Manager) List

func (m *Manager) List() []*Session

List returns all active sessions (for debugging/admin).

func (*Manager) Start

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

Start begins the background cleanup goroutine.

func (*Manager) Stop

func (m *Manager) Stop()

Stop shuts down the session manager.

func (*Manager) TotalCreated

func (m *Manager) TotalCreated() int64

TotalCreated returns the total number of sessions created.

type ManagerConfig

type ManagerConfig struct {
	SessionTTL      time.Duration
	CleanupInterval time.Duration
	MaxSessions     int
}

ManagerConfig holds session manager configuration.

func DefaultManagerConfig

func DefaultManagerConfig() ManagerConfig

DefaultManagerConfig returns sensible defaults.

type Session

type Session struct {
	// ID is the unique session identifier
	ID string `json:"id"`

	// CreatedAt is when the session was established
	CreatedAt time.Time `json:"created_at"`

	// LastActivityAt is the time of the last request
	LastActivityAt time.Time `json:"last_activity_at"`

	// RequestCount is the total number of requests in this session
	RequestCount int `json:"request_count"`

	// AgentID is the identifier of the connected agent (from config or AgentFacts)
	AgentID string `json:"agent_id"`

	// AgentName is the human-readable agent name
	AgentName string `json:"agent_name,omitempty"`

	// Capabilities are the agent's granted capabilities
	Capabilities []string `json:"capabilities,omitempty"`

	// IdentityVerified indicates if AgentFacts token was verified
	IdentityVerified bool `json:"identity_verified"`

	// DID is the agent's decentralized identifier (if verified)
	DID string `json:"did,omitempty"`

	// SourceIP is the client's IP address
	SourceIP string `json:"source_ip,omitempty"`

	// UserAgent is the client's user agent string
	UserAgent string `json:"user_agent,omitempty"`

	// MessageChan is used to send SSE messages back to the client
	MessageChan chan []byte `json:"-"`

	// Done is closed when the session is terminated
	Done chan struct{} `json:"-"`
	// contains filtered or unexported fields
}

Session represents an active client connection session.

func NewSession

func NewSession(id string) *Session

NewSession creates a new session with the given ID.

func (*Session) Age

func (s *Session) Age() time.Duration

Age returns how long the session has been active.

func (*Session) Close

func (s *Session) Close()

Close closes the session channels.

func (*Session) Context

func (s *Session) Context() context.Context

Context returns a context that is cancelled when the session is closed.

func (*Session) GetRequestCount

func (s *Session) GetRequestCount() int

GetRequestCount returns the current request count.

func (*Session) IdleTime

func (s *Session) IdleTime() time.Duration

IdleTime returns how long since the last activity.

func (*Session) IncrementRequestCount

func (s *Session) IncrementRequestCount() int

IncrementRequestCount atomically increments the request counter and returns the new value.

func (*Session) IsClosed

func (s *Session) IsClosed() bool

IsClosed returns true if the session has been closed.

func (*Session) SendMessage

func (s *Session) SendMessage(msg []byte) bool

SendMessage sends a message to the client via the message channel. Returns false if the session is closed or the channel is full.

func (*Session) SetAgent

func (s *Session) SetAgent(agentID, agentName string, capabilities []string)

SetAgent sets the agent identity information.

func (*Session) SetClientInfo

func (s *Session) SetClientInfo(sourceIP, userAgent string)

SetClientInfo sets the client connection information.

func (*Session) SetIdentity

func (s *Session) SetIdentity(verified bool, did string)

SetIdentity sets the verified identity information.

type SessionError

type SessionError struct {
	Message string
}

SessionError represents a session-related error.

func (*SessionError) Error

func (e *SessionError) Error() string

Jump to

Keyboard shortcuts

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