sse

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: AGPL-3.0 Imports: 11 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.
    DefaultWriteTimeout = 10 * 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. A connection that cannot accept the publication is terminated instead of silently losing it.

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
}

type DeliveryError

DeliveryError is a secret-free classification of the latest stream failure.

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"`
    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"`
    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 at least one SSE client is connected.
    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.
	DefaultWriteTimeout = 10 * 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. A connection that cannot accept the publication is terminated instead of silently losing it.

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
}

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 is a secret-free classification of the latest stream failure.

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"`
	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"`
	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 at least one SSE client is connected.
	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