sse

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrTooManyConnections = errors.New("too many SSE connections")

ErrTooManyConnections is returned when the global SSE connection cap is exceeded.

View Source
var ErrTooManyConnectionsForUser = errors.New("too many SSE connections for user")

ErrTooManyConnectionsForUser is returned when the per-user SSE connection cap is exceeded.

Functions

This section is empty.

Types

type SSELimiter

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

SSELimiter enforces global and per-user SSE connection limits using atomic counters. Acquire returns a release function on success, or an error when the cap is exceeded.

SSE-01: Global connection cap via active atomic counter. SSE-02: Per-user cap via sync.Map keyed by userID. SSE-03: Graceful shutdown is handled by the SSE HTTP handler detecting ctx.Done() and writing a close event frame before returning — the limiter itself only tracks counts and provides the release callback.

func NewSSELimiter

func NewSSELimiter(maxTotal, maxPerUser int) *SSELimiter

NewSSELimiter creates a new SSELimiter with the given global and per-user limits. Callers pass SSEConfig.MaxTotalConnections and SSEConfig.MaxPerUser directly; the limiter does not import config to avoid circular dependencies.

func (*SSELimiter) Acquire

func (l *SSELimiter) Acquire(userID string) (release func(), err error)

Acquire attempts to reserve an SSE connection slot for the given user. Returns a release function that must be called (typically via defer) when the connection ends, and nil error on success. Returns ErrTooManyConnections when the global cap is reached. Returns ErrTooManyConnectionsForUser when the per-user cap is reached.

func (*SSELimiter) ActiveConnections

func (l *SSELimiter) ActiveConnections() int64

ActiveConnections returns the current total number of active SSE connections. Useful for Prometheus metrics.

func (*SSELimiter) ActiveForUser

func (l *SSELimiter) ActiveForUser(userID string) int64

ActiveForUser returns the number of active SSE connections for the given user. Useful for debugging and per-user metrics.

Jump to

Keyboard shortcuts

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