Help us shape the future of Go! Take the 2026 Go Developer Survey

sse

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2026 License: AGPL-3.0 Imports: 12 Imported by: 0

README

sse

import "github.com/agentstation/starmap/internal/server/sse"

Package sse provides the sole reactive catalog-publication transport.

Index

Constants

const (
    // CatalogPublishedEvent is the stable SSE event name.
    CatalogPublishedEvent = remote.CatalogPublishedEvent
    // DefaultHeartbeatInterval keeps idle streams alive through common proxies.
    DefaultHeartbeatInterval = 20 * time.Second
    // DefaultWriteTimeout bounds each event or heartbeat write and flush. The
    // broadcaster resets the deadline before every frame, so the value bounds
    // one frame and never bounds the stream. Two minutes lets a slow reader on
    // a congested link keep its subscription.
    DefaultWriteTimeout = 2 * time.Minute

    // DefaultMaxClients bounds concurrent subscriber connections. Each
    // connection owns one writer goroutine and one queued publication, so an
    // unbounded client count would let subscribers exhaust publisher memory.
    DefaultMaxClients = 512

    // DefaultAdmissionRetryAfter is the base wait a refused subscriber gets.
    // The served value carries jitter, so a refused fleet does not return at
    // one instant.
    DefaultAdmissionRetryAfter = 30 * time.Second
)

type Broadcaster

Broadcaster delivers publications to HTTP SSE connections. Each connection owns exactly one writer goroutine: its request handler. Publication overload terminates that connection so reconnect catch-up can recover without a silently healthy stream.

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

func NewBroadcaster
func NewBroadcaster(config Config, logger *zerolog.Logger) (*Broadcaster, error)

NewBroadcaster constructs an idle broadcaster. It starts no goroutine.

func (*Broadcaster) ClientCount
func (b *Broadcaster) ClientCount() int

ClientCount returns the number of currently registered SSE connections.

func (*Broadcaster) Close
func (b *Broadcaster) Close()

Close terminates every active connection and rejects new ones.

func (*Broadcaster) Health
func (b *Broadcaster) Health() Health

Health returns the current server-side stream delivery health.

func (*Broadcaster) Publish
func (b *Broadcaster) Publish(publication Publication) error

Publish offers one committed generation to every connected stream. It never blocks the catalog commit path. Publish terminates a connection that cannot accept the generation, which prevents silent data loss.

func (*Broadcaster) ServeHTTP
func (b *Broadcaster) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP serves one heartbeat-enabled SSE connection.

func (*Broadcaster) Stats
func (b *Broadcaster) Stats() DeliveryStats

Stats returns cumulative delivery counters.

type Config

Config controls per-connection SSE liveness and write behavior.

type Config struct {
    HeartbeatInterval time.Duration
    WriteTimeout      time.Duration

    // MaxClients bounds concurrent subscriber connections. Zero selects
    // DefaultMaxClients, and a negative value is invalid.
    MaxClients int

    // AdmissionRetryAfter is the base wait the publisher declares to a refused
    // subscriber. Zero selects DefaultAdmissionRetryAfter.
    AdmissionRetryAfter time.Duration
}

type DeliveryError

DeliveryError classifies the latest stream failure without exposing secrets.

type DeliveryError struct {
    Kind       string    `json:"kind"`
    OccurredAt time.Time `json:"occurred_at"`
}

type DeliveryStats

DeliveryStats is a lock-free snapshot of SSE delivery behavior.

type DeliveryStats struct {
    Published              uint64 `json:"published"`
    Sent                   uint64 `json:"sent"`
    Heartbeats             uint64 `json:"heartbeats"`
    Disconnected           uint64 `json:"disconnected"`
    Refused                uint64 `json:"refused"`
    BackpressureTerminated uint64 `json:"backpressure_terminated"`
    Failed                 uint64 `json:"failed"`
}

type Health

Health reports server-side SSE publication delivery without conflating heartbeat liveness with catalog freshness.

type Health struct {
    State            StreamState    `json:"state"`
    Clients          int            `json:"clients"`
    MaxClients       int            `json:"max_clients"`
    LastHeartbeatAt  time.Time      `json:"last_heartbeat_at"`
    LastEventAt      time.Time      `json:"last_event_at"`
    LastGenerationID string         `json:"last_generation_id,omitempty"`
    LastSequence     uint64         `json:"last_sequence"`
    LastError        *DeliveryError `json:"last_error,omitempty"`
    Delivery         DeliveryStats  `json:"delivery"`
}

type Publication

Publication identifies one committed immutable catalog generation.

type Publication = remote.Publication

type StreamState

StreamState is the server-side publication stream state.

type StreamState string

const (
    // StreamStateIdle means the broadcaster accepts streams but has no clients.
    StreamStateIdle StreamState = "idle"
    // StreamStateStreaming means the broadcaster has active clients.
    StreamStateStreaming StreamState = "streaming"
    // StreamStateStopped means the broadcaster rejects new streams.
    StreamStateStopped StreamState = "stopped"
)

Generated by gomarkdoc

Documentation

Overview

Package sse provides the sole reactive catalog-publication transport.

Index

Constants

View Source
const (
	// CatalogPublishedEvent is the stable SSE event name.
	CatalogPublishedEvent = remote.CatalogPublishedEvent
	// DefaultHeartbeatInterval keeps idle streams alive through common proxies.
	DefaultHeartbeatInterval = 20 * time.Second
	// DefaultWriteTimeout bounds each event or heartbeat write and flush. The
	// broadcaster resets the deadline before every frame, so the value bounds
	// one frame and never bounds the stream. Two minutes lets a slow reader on
	// a congested link keep its subscription.
	DefaultWriteTimeout = 2 * time.Minute

	// DefaultMaxClients bounds concurrent subscriber connections. Each
	// connection owns one writer goroutine and one queued publication, so an
	// unbounded client count would let subscribers exhaust publisher memory.
	DefaultMaxClients = 512

	// DefaultAdmissionRetryAfter is the base wait a refused subscriber gets.
	// The served value carries jitter, so a refused fleet does not return at
	// one instant.
	DefaultAdmissionRetryAfter = 30 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Broadcaster

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

Broadcaster delivers publications to HTTP SSE connections. Each connection owns exactly one writer goroutine: its request handler. Publication overload terminates that connection so reconnect catch-up can recover without a silently healthy stream.

func NewBroadcaster

func NewBroadcaster(config Config, logger *zerolog.Logger) (*Broadcaster, error)

NewBroadcaster constructs an idle broadcaster. It starts no goroutine.

func (*Broadcaster) ClientCount

func (b *Broadcaster) ClientCount() int

ClientCount returns the number of currently registered SSE connections.

func (*Broadcaster) Close added in v0.2.0

func (b *Broadcaster) Close()

Close terminates every active connection and rejects new ones.

func (*Broadcaster) Health added in v0.2.0

func (b *Broadcaster) Health() Health

Health returns the current server-side stream delivery health.

func (*Broadcaster) Publish added in v0.2.0

func (b *Broadcaster) Publish(publication Publication) error

Publish offers one committed generation to every connected stream. It never blocks the catalog commit path. Publish terminates a connection that cannot accept the generation, which prevents silent data loss.

func (*Broadcaster) ServeHTTP

func (b *Broadcaster) ServeHTTP(writer http.ResponseWriter, request *http.Request)

ServeHTTP serves one heartbeat-enabled SSE connection.

func (*Broadcaster) Stats added in v0.2.0

func (b *Broadcaster) Stats() DeliveryStats

Stats returns cumulative delivery counters.

type Config added in v0.2.0

type Config struct {
	HeartbeatInterval time.Duration
	WriteTimeout      time.Duration

	// MaxClients bounds concurrent subscriber connections. Zero selects
	// DefaultMaxClients, and a negative value is invalid.
	MaxClients int

	// AdmissionRetryAfter is the base wait the publisher declares to a refused
	// subscriber. Zero selects DefaultAdmissionRetryAfter.
	AdmissionRetryAfter time.Duration
}

Config controls per-connection SSE liveness and write behavior.

type DeliveryError added in v0.2.0

type DeliveryError struct {
	Kind       string    `json:"kind"`
	OccurredAt time.Time `json:"occurred_at"`
}

DeliveryError classifies the latest stream failure without exposing secrets.

type DeliveryStats added in v0.2.0

type DeliveryStats struct {
	Published              uint64 `json:"published"`
	Sent                   uint64 `json:"sent"`
	Heartbeats             uint64 `json:"heartbeats"`
	Disconnected           uint64 `json:"disconnected"`
	Refused                uint64 `json:"refused"`
	BackpressureTerminated uint64 `json:"backpressure_terminated"`
	Failed                 uint64 `json:"failed"`
}

DeliveryStats is a lock-free snapshot of SSE delivery behavior.

type Health added in v0.2.0

type Health struct {
	State            StreamState    `json:"state"`
	Clients          int            `json:"clients"`
	MaxClients       int            `json:"max_clients"`
	LastHeartbeatAt  time.Time      `json:"last_heartbeat_at"`
	LastEventAt      time.Time      `json:"last_event_at"`
	LastGenerationID string         `json:"last_generation_id,omitempty"`
	LastSequence     uint64         `json:"last_sequence"`
	LastError        *DeliveryError `json:"last_error,omitempty"`
	Delivery         DeliveryStats  `json:"delivery"`
}

Health reports server-side SSE publication delivery without conflating heartbeat liveness with catalog freshness.

type Publication added in v0.2.0

type Publication = remote.Publication

Publication identifies one committed immutable catalog generation.

type StreamState added in v0.2.0

type StreamState string

StreamState is the server-side publication stream state.

const (
	// StreamStateIdle means the broadcaster accepts streams but has no clients.
	StreamStateIdle StreamState = "idle"
	// StreamStateStreaming means the broadcaster has active clients.
	StreamStateStreaming StreamState = "streaming"
	// StreamStateStopped means the broadcaster rejects new streams.
	StreamStateStopped StreamState = "stopped"
)

Jump to

Keyboard shortcuts

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