pubsub

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package pubsub provides a lightweight in-process broker for fan-out event delivery between services and the UI.

Delivery semantics:

  • Broker.Publish is best-effort and lossy under contention. If a subscriber's channel is full, the event is dropped for that subscriber, a warning is logged, and a counter is incremented. This is the right choice for high-frequency intermediate updates (e.g. streaming token deltas) where only the latest state matters.

  • Broker.PublishMustDeliver is bounded-blocking. For each subscriber it first tries a non-blocking send, then falls back to a per-subscriber blocking send with a hard timeout. On timeout the event is dropped for that subscriber, an error is logged, and the must-deliver drop counter is incremented. The publisher never blocks indefinitely. This is the right choice for terminal events (finish, tool result, error, cancel) that must not be silently coalesced away.

Drop counters (Broker.DropCount, Broker.MustDeliverDropCount) are exposed so callers can surface saturation in telemetry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Broker

type Broker[T any] struct {
	// contains filtered or unexported fields
}

func NewBroker

func NewBroker[T any]() *Broker[T]

func NewBrokerWithOptions

func NewBrokerWithOptions[T any](channelBufferSize int) *Broker[T]

func (*Broker[T]) DropCount

func (b *Broker[T]) DropCount() uint64

DropCount returns the cumulative number of events dropped by Broker.Publish because a subscriber's channel was full.

func (*Broker[T]) GetSubscriberCount

func (b *Broker[T]) GetSubscriberCount() int

func (*Broker[T]) MustDeliverDropCount

func (b *Broker[T]) MustDeliverDropCount() uint64

MustDeliverDropCount returns the cumulative number of events dropped by Broker.PublishMustDeliver after the per-subscriber timeout expired.

func (*Broker[T]) Publish

func (b *Broker[T]) Publish(t EventType, payload T)

Publish delivers an event to every active subscriber.

Delivery is non-blocking and lossy: if a subscriber's channel is full the event is dropped for that subscriber, a warning is logged, and Broker.DropCount is incremented. Use Broker.PublishMustDeliver for events that must not be silently dropped.

func (*Broker[T]) PublishMustDeliver

func (b *Broker[T]) PublishMustDeliver(ctx context.Context, t EventType, payload T)

PublishMustDeliver delivers an event with bounded-blocking semantics. For each subscriber it first attempts a non-blocking send, then falls back to a blocking send bounded by a per-subscriber timeout (default [defaultMustDeliverTimeout]). On timeout the event is dropped for that subscriber, Broker.MustDeliverDropCount is incremented, and an error is logged. The publisher never blocks indefinitely.

Use this for terminal events that must reach subscribers (finish, tool result, error, cancel). Callers must still tolerate rare drops after timeout — recovery is the subscriber's responsibility (e.g. a re-fetch on the next session-visible event).

func (*Broker[T]) SetMustDeliverTimeout

func (b *Broker[T]) SetMustDeliverTimeout(d time.Duration)

SetMustDeliverTimeout overrides the per-subscriber timeout used by Broker.PublishMustDeliver. A zero or negative value resets to the default. Intended primarily for tests.

func (*Broker[T]) Shutdown

func (b *Broker[T]) Shutdown()

func (*Broker[T]) Subscribe

func (b *Broker[T]) Subscribe(ctx context.Context) <-chan Event[T]

type Event

type Event[T any] struct {
	Type    EventType `json:"type"`
	Payload T         `json:"payload"`
}

Event represents an event in the lifecycle of a resource.

type EventType

type EventType string

EventType identifies the type of event.

const (
	CreatedEvent EventType = "created"
	UpdatedEvent EventType = "updated"
	DeletedEvent EventType = "deleted"
)

type Payload

type Payload struct {
	Type    PayloadType     `json:"type"`
	Payload json.RawMessage `json:"payload"`
}

Payload wraps a discriminated JSON payload with a type tag.

type PayloadType

type PayloadType = string

PayloadType identifies the type of event payload for discriminated deserialization over JSON.

const (
	PayloadTypeLSPEvent               PayloadType = "lsp_event"
	PayloadTypeMCPEvent               PayloadType = "mcp_event"
	PayloadTypePermissionRequest      PayloadType = "permission_request"
	PayloadTypePermissionNotification PayloadType = "permission_notification"
	PayloadTypeMessage                PayloadType = "message"
	PayloadTypeSession                PayloadType = "session"
	PayloadTypeFile                   PayloadType = "file"
	PayloadTypeAgentEvent             PayloadType = "agent_event"
	PayloadTypeConfigChanged          PayloadType = "config_changed"
	PayloadTypeSkillsEvent            PayloadType = "skills_event"
	PayloadTypeRunComplete            PayloadType = "run_complete"
)

type Publisher

type Publisher[T any] interface {
	Publish(EventType, T)
	PublishMustDeliver(context.Context, EventType, T)
}

Publisher can publish events of type T.

Publish is best-effort and lossy under back-pressure; PublishMustDeliver applies the bounded-blocking semantics used for terminal events that must reach subscribers (finish, tool result, error, cancel, RunComplete). See Broker.Publish and Broker.PublishMustDeliver.

type Subscriber

type Subscriber[T any] interface {
	Subscribe(context.Context) <-chan Event[T]
}

Subscriber can subscribe to events of type T.

Jump to

Keyboard shortcuts

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