Documentation
¶
Overview ¶
Package session provides a session manager with TTL cleanup.
Index ¶
- Constants
- Variables
- type Factory
- type LegacyFactory
- type Manager
- type ProxySession
- func (s *ProxySession) CreatedAt() time.Time
- func (s *ProxySession) DeleteMetadata(key string)
- func (s *ProxySession) GetData() interface{}
- func (s *ProxySession) GetMetadata() map[string]string
- func (s *ProxySession) GetMetadataValue(key string) (string, bool)
- func (s *ProxySession) ID() string
- func (s *ProxySession) SetData(data interface{})
- func (s *ProxySession) SetMetadata(key, value string)
- func (s *ProxySession) Touch()
- func (s *ProxySession) Type() SessionType
- func (s *ProxySession) UpdatedAt() time.Time
- type SSESession
- func (s *SSESession) Disconnect()
- func (s *SSESession) GetClientInfo() *ssecommon.SSEClient
- func (s *SSESession) GetConnectionStatus() bool
- func (s *SSESession) GetCreatedAt() time.Time
- func (s *SSESession) SendMessage(msg string) error
- func (s *SSESession) SetClientInfo(client *ssecommon.SSEClient)
- type Session
- type SessionType
- type StreamableSession
- func (s *StreamableSession) Disconnect()
- func (s *StreamableSession) GetData() interface{}
- func (s *StreamableSession) SendMessage(msg jsonrpc2.Message) error
- func (s *StreamableSession) SendResponse(msg jsonrpc2.Message) error
- func (*StreamableSession) SetData(interface{})
- func (*StreamableSession) Type() SessionType
Constants ¶
const ( // DefaultSessionTTL is the default time-to-live for sessions (2 hours) DefaultSessionTTL = 2 * time.Hour )
Variables ¶
var ( // ErrSessionDisconnected is returned when trying to send to a disconnected session ErrSessionDisconnected = errors.New("session is disconnected") // ErrMessageChannelFull is returned when the message channel is full ErrMessageChannelFull = errors.New("message channel is full") // ErrSessionNotFound is returned when a session cannot be found ErrSessionNotFound = errors.New("session not found") // ErrSessionAlreadyExists is returned when trying to create a session with an existing ID ErrSessionAlreadyExists = errors.New("session already exists") // ErrInvalidSessionType is returned when an invalid session type is provided ErrInvalidSessionType = errors.New("invalid session type") )
Common session errors
Functions ¶
This section is empty.
Types ¶
type Factory ¶
Factory defines a function type for creating new sessions. It now returns the Session interface to support different session types.
type LegacyFactory ¶ added in v0.2.17
type LegacyFactory func(id string) *ProxySession
LegacyFactory is the old factory type for backward compatibility
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager holds sessions with TTL cleanup.
func NewManager ¶
NewManager creates a session manager with TTL and starts cleanup worker. It accepts either the new Factory or the legacy factory for backward compatibility.
func NewTypedManager ¶ added in v0.2.17
func NewTypedManager(ttl time.Duration, sessionType SessionType) *Manager
NewTypedManager creates a session manager for a specific session type.
func (*Manager) AddSession ¶ added in v0.2.17
AddSession adds an existing session to the manager. This is useful when you need to create a session with specific properties.
func (*Manager) AddWithID ¶
AddWithID creates (and adds) a new session with the provided ID. Returns error if ID is empty or already exists.
func (*Manager) Get ¶
Get retrieves a session by ID. Returns (session, true) if found, and also updates its UpdatedAt timestamp.
type ProxySession ¶
type ProxySession struct {
// contains filtered or unexported fields
}
ProxySession implements the Session interface for proxy sessions. It now includes support for session types, metadata, and custom data.
func NewProxySession ¶
func NewProxySession(id string) *ProxySession
NewProxySession creates a new ProxySession with the given ID. It defaults to SessionTypeMCP for backward compatibility.
func NewTypedProxySession ¶ added in v0.2.17
func NewTypedProxySession(id string, sessType SessionType) *ProxySession
NewTypedProxySession creates a new ProxySession with the given ID and type.
func (*ProxySession) CreatedAt ¶
func (s *ProxySession) CreatedAt() time.Time
CreatedAt returns the creation time of the session.
func (*ProxySession) DeleteMetadata ¶ added in v0.2.17
func (s *ProxySession) DeleteMetadata(key string)
DeleteMetadata removes a metadata key.
func (*ProxySession) GetData ¶ added in v0.2.17
func (s *ProxySession) GetData() interface{}
GetData returns the session-specific data.
func (*ProxySession) GetMetadata ¶ added in v0.2.17
func (s *ProxySession) GetMetadata() map[string]string
GetMetadata returns all metadata as a map.
func (*ProxySession) GetMetadataValue ¶ added in v0.2.17
func (s *ProxySession) GetMetadataValue(key string) (string, bool)
GetMetadataValue gets a specific metadata value.
func (*ProxySession) SetData ¶ added in v0.2.17
func (s *ProxySession) SetData(data interface{})
SetData sets the session-specific data.
func (*ProxySession) SetMetadata ¶ added in v0.2.17
func (s *ProxySession) SetMetadata(key, value string)
SetMetadata sets a metadata key-value pair.
func (*ProxySession) Touch ¶
func (s *ProxySession) Touch()
Touch updates the session's last updated time to the current time.
func (*ProxySession) Type ¶ added in v0.2.17
func (s *ProxySession) Type() SessionType
Type returns the session type.
func (*ProxySession) UpdatedAt ¶
func (s *ProxySession) UpdatedAt() time.Time
UpdatedAt returns the last updated time of the session.
type SSESession ¶ added in v0.2.17
type SSESession struct {
*ProxySession
// SSE-specific fields
MessageCh chan string
ClientInfo *ssecommon.SSEClient
IsConnected bool
}
SSESession represents an SSE (Server-Sent Events) session. It embeds ProxySession and adds SSE-specific functionality.
func NewSSESession ¶ added in v0.2.17
func NewSSESession(id string) *SSESession
NewSSESession creates a new SSE session with the given ID.
func NewSSESessionWithClient ¶ added in v0.2.17
func NewSSESessionWithClient(id string, client *ssecommon.SSEClient) *SSESession
NewSSESessionWithClient creates a new SSE session with the given ID and client info.
func (*SSESession) Disconnect ¶ added in v0.2.17
func (s *SSESession) Disconnect()
Disconnect marks the session as disconnected and closes the message channel.
func (*SSESession) GetClientInfo ¶ added in v0.2.17
func (s *SSESession) GetClientInfo() *ssecommon.SSEClient
GetClientInfo returns the SSE client information.
func (*SSESession) GetConnectionStatus ¶ added in v0.2.17
func (s *SSESession) GetConnectionStatus() bool
GetConnectionStatus returns whether the SSE session is connected.
func (*SSESession) GetCreatedAt ¶ added in v0.2.17
func (s *SSESession) GetCreatedAt() time.Time
GetCreatedAt returns when the SSE session was created. This is useful for tracking connection duration.
func (*SSESession) SendMessage ¶ added in v0.2.17
func (s *SSESession) SendMessage(msg string) error
SendMessage sends a message to the SSE client if connected.
func (*SSESession) SetClientInfo ¶ added in v0.2.17
func (s *SSESession) SetClientInfo(client *ssecommon.SSEClient)
SetClientInfo sets the SSE client information.
type Session ¶
Session interface
func NewStreamableSession ¶ added in v0.2.17
NewStreamableSession constructs a new streamable session with buffered channels
type SessionType ¶ added in v0.2.17
type SessionType string
SessionType represents the type of session
const ( // SessionTypeMCP represents a standard MCP session SessionTypeMCP SessionType = "mcp" // SessionTypeSSE represents an SSE (Server-Sent Events) session SessionTypeSSE SessionType = "sse" // SessionTypeStreamable represents a streamable HTTP session SessionTypeStreamable SessionType = "streamable" )
type StreamableSession ¶ added in v0.2.17
type StreamableSession struct {
*ProxySession
MessageCh chan jsonrpc2.Message
ResponseCh chan jsonrpc2.Message
// contains filtered or unexported fields
}
StreamableSession represents a Streamable HTTP session
func (*StreamableSession) Disconnect ¶ added in v0.2.17
func (s *StreamableSession) Disconnect()
Disconnect closes channels and marks session as disconnected
func (*StreamableSession) GetData ¶ added in v0.2.17
func (s *StreamableSession) GetData() interface{}
GetData exposes buffer stats for observability/debugging
func (*StreamableSession) SendMessage ¶ added in v0.2.17
func (s *StreamableSession) SendMessage(msg jsonrpc2.Message) error
SendMessage pushes message to MessageCh
func (*StreamableSession) SendResponse ¶ added in v0.2.17
func (s *StreamableSession) SendResponse(msg jsonrpc2.Message) error
SendResponse pushes message to ResponseCh
func (*StreamableSession) SetData ¶ added in v0.2.17
func (*StreamableSession) SetData(interface{})
SetData is a no-op for StreamableSession; channel stats are exposed via GetData.
func (*StreamableSession) Type ¶ added in v0.2.17
func (*StreamableSession) Type() SessionType
Type identifies this as a streamable session