Documentation
¶
Index ¶
- type Broker
- func (broker *Broker) Broadcast(eventType, data string)
- func (broker *Broker) GetClientCount() int
- func (broker *Broker) GetSessionStats() map[string]interface{}
- func (broker *Broker) RegisterClient(w http.ResponseWriter, r *http.Request) (*Client, error)
- func (broker *Broker) SendToClient(sessionID, eventType, data string)
- func (broker *Broker) SendToClients(sessionIDs []string, eventType, data string)
- func (broker *Broker) Stop()
- func (broker *Broker) UnregisterClient(sessionID string)
- type Client
- type ClientSession
- type Event
- type Handler
- func (h *Handler) Broadcast(eventType, data string)
- func (h *Handler) GetStats() map[string]interface{}
- func (h *Handler) SendToClient(sessionID, eventType, data string)
- func (h *Handler) SendToClients(sessionIDs []string, eventType, data string)
- func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Stop()
- type Message
- type SessionConfig
- type SessionManager
- func (sm *SessionManager) BufferEvent(sessionID string, event Event)
- func (sm *SessionManager) CreateSession(metadata SessionMeta) (*ClientSession, error)
- func (sm *SessionManager) DisconnectSession(sessionID string)
- func (sm *SessionManager) GetActiveSessionCount() int
- func (sm *SessionManager) GetSession(sessionID string) (*ClientSession, bool)
- func (sm *SessionManager) GetTotalSessionCount() int
- func (sm *SessionManager) ReconnectSession(sessionID string, lastEventID string) (*ClientSession, []Event, error)
- func (sm *SessionManager) RemoveSession(sessionID string)
- func (sm *SessionManager) Stop()
- func (sm *SessionManager) ValidateSessionOwnership(sessionID string, metadata SessionMeta) bool
- type SessionMeta
- type TestEndpoints
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broker ¶
type Broker struct {
// contains filtered or unexported fields
}
Broker manages SSE connections with reconnection support
func NewBroker ¶
func NewBroker(config SessionConfig) *Broker
NewBroker creates a new SSE broker with reconnection support
func (*Broker) GetClientCount ¶
GetClientCount returns the number of connected clients
func (*Broker) GetSessionStats ¶
GetSessionStats returns statistics about sessions
func (*Broker) RegisterClient ¶
RegisterClient registers a new SSE client
func (*Broker) SendToClient ¶
SendToClient sends an event to a specific client
func (*Broker) SendToClients ¶
SendToClients sends an event to specific clients
func (*Broker) UnregisterClient ¶
UnregisterClient unregisters an SSE client
type Client ¶
type Client struct {
SessionID string // Persistent session identifier
EventChannel chan Event // Channel for sending events to client
Done chan struct{} // Channel to signal client disconnection
Request *http.Request // Original HTTP request
Writer http.ResponseWriter
Flusher http.Flusher
}
Client represents a connected SSE client with session support
type ClientSession ¶
type ClientSession struct {
ID string // Unique session identifier
LastEventID string // Last event ID received by client
LastSeen time.Time // Last time client was connected
Created time.Time // When session was created
EventBuffer *ring.Ring // Circular buffer of missed events
Metadata SessionMeta // Custom session metadata
Reconnections int // Number of times client has reconnected
Active bool // Whether client is currently connected
// contains filtered or unexported fields
}
ClientSession represents a persistent SSE client session
type Event ¶
type Event struct {
ID string // Unique event ID for deduplication
Type string // Event type (e.g., "message", "update", "ping")
Data string // Event payload
Timestamp time.Time // When the event was created
Replayed bool // Whether this is a replayed event
}
Event represents an SSE event with metadata for replay
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler provides HTTP endpoints for SSE with reconnection support
func NewHandler ¶
func NewHandler(config SessionConfig) *Handler
NewHandler creates a new SSE handler with the given configuration
func (*Handler) SendToClient ¶
SendToClient sends an event to a specific client
func (*Handler) SendToClients ¶
SendToClients sends an event to specific clients
type Message ¶
type Message struct {
Event Event // The event to broadcast
TargetIDs []string // Specific session IDs to target (empty = broadcast to all)
}
Message represents a broadcast message with targeting capabilities
type SessionConfig ¶
type SessionConfig struct {
// BufferSize is the maximum number of events to buffer per session
BufferSize int
// BufferTTL is how long to keep a disconnected session alive
BufferTTL time.Duration
// EnableReconnection enables session persistence and event replay
EnableReconnection bool
// CleanupInterval is how often to run the cleanup goroutine
CleanupInterval time.Duration
}
SessionConfig defines configuration for SSE session management
func DefaultSessionConfig ¶
func DefaultSessionConfig() SessionConfig
DefaultSessionConfig returns sensible defaults for session management
type SessionManager ¶
type SessionManager struct {
// contains filtered or unexported fields
}
SessionManager manages all SSE client sessions
func NewSessionManager ¶
func NewSessionManager(config SessionConfig) *SessionManager
NewSessionManager creates a new session manager with the given configuration
func (*SessionManager) BufferEvent ¶
func (sm *SessionManager) BufferEvent(sessionID string, event Event)
BufferEvent adds an event to a session's buffer if disconnected
func (*SessionManager) CreateSession ¶
func (sm *SessionManager) CreateSession(metadata SessionMeta) (*ClientSession, error)
CreateSession creates a new client session
func (*SessionManager) DisconnectSession ¶
func (sm *SessionManager) DisconnectSession(sessionID string)
DisconnectSession marks a session as disconnected
func (*SessionManager) GetActiveSessionCount ¶
func (sm *SessionManager) GetActiveSessionCount() int
GetActiveSessionCount returns the number of active sessions
func (*SessionManager) GetSession ¶
func (sm *SessionManager) GetSession(sessionID string) (*ClientSession, bool)
GetSession retrieves a session by ID
func (*SessionManager) GetTotalSessionCount ¶
func (sm *SessionManager) GetTotalSessionCount() int
GetTotalSessionCount returns the total number of sessions (active and buffered)
func (*SessionManager) ReconnectSession ¶
func (sm *SessionManager) ReconnectSession(sessionID string, lastEventID string) (*ClientSession, []Event, error)
ReconnectSession handles a client reconnection
func (*SessionManager) RemoveSession ¶
func (sm *SessionManager) RemoveSession(sessionID string)
RemoveSession completely removes a session
func (*SessionManager) Stop ¶
func (sm *SessionManager) Stop()
Stop gracefully shuts down the session manager
func (*SessionManager) ValidateSessionOwnership ¶
func (sm *SessionManager) ValidateSessionOwnership(sessionID string, metadata SessionMeta) bool
ValidateSessionOwnership checks if a session can be claimed by a connection This prevents session hijacking by validating metadata
type SessionMeta ¶
type SessionMeta struct {
UserAgent string // Client user agent
RemoteAddr string // Client IP address
Headers map[string]string // Custom headers from client
QueryParams map[string]string // Query parameters from connection
Subscriptions []string // Event types client is subscribed to
ClientVersion string // Client application version
}
SessionMeta holds custom metadata for a session
type TestEndpoints ¶
type TestEndpoints struct {
// contains filtered or unexported fields
}
TestEndpoints provides HTTP endpoints for testing SSE functionality
func NewTestEndpoints ¶
func NewTestEndpoints(handler *Handler) *TestEndpoints
NewTestEndpoints creates test endpoints for SSE
func (*TestEndpoints) RegisterRoutes ¶
func (te *TestEndpoints) RegisterRoutes(mux *http.ServeMux)
RegisterRoutes registers test routes on an HTTP mux