broadcaster

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package broadcaster provides a unified interface for pub/sub implementations. It defines common interfaces that both broadcast and feed packages can implement.

Architecture:

  • BroadcastHub: Factory that creates Publishers and Subscribables
  • Publisher: For sending messages to all subscribers
  • Subscribable: Read-only view bound to a single lag policy
  • Subscription: For receiving messages from a subscription

Type parameter T is the message type being published.

Lag handling: when the underlying implementation can detect that a subscriber fell behind (broadcast/ring), the lag policy supplied at NewSubscribable time decides what to do — log, count, panic, etc. Each consumer asks the hub for its own Subscribable with its own policy, so the same hub can serve subscribers with different lag responses simultaneously.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LagPolicyDrop

func LagPolicyDrop[T any](seq iter.Seq[ring.EventOrLag[T]]) iter.Seq[T]

LagPolicyDrop yields events and silently drops lag notifications. Matches feed.Feed's behavior for slow subscribers; use only when the caller has no recovery to do and lag is truly tolerable.

Types

type BlockByNumberReader

type BlockByNumberReader interface {
	BlockByNumber(number uint64) (*core.Block, error)
}

BlockByNumberReader recovers a block by its height, so a lagged subscriber can be caught up from durable storage. blockchain.Reader satisfies this directly.

type BroadcastAdapter

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

BroadcastAdapter adapts broadcast.Broadcast[T] to BroadcastHub[T]. The lag policy lives on each Subscribable, so different consumers of the same broadcast can hold different responses to lag (log, count, drop, ...).

func NewBroadcastAdapter

func NewBroadcastAdapter[T any](b *broadcast.Broadcast[T]) *BroadcastAdapter[T]

func (*BroadcastAdapter[T]) NewPublisher

func (a *BroadcastAdapter[T]) NewPublisher() Publisher[T]

func (*BroadcastAdapter[T]) NewSubscribable

func (a *BroadcastAdapter[T]) NewSubscribable(lagPolicy LagPolicy[T]) Subscribable[T]

type BroadcastHub

type BroadcastHub[T any] interface {
	SubscribableSource[T]

	// NewPublisher creates a new Publisher for sending messages.
	NewPublisher() Publisher[T]
}

BroadcastHub is a factory that creates Publishers and Subscribables.

func New

func New[T any](opts ...Option) BroadcastHub[T]

New is the single access point: callers wire the kind from a flag and get back the same BroadcastHub[T] interface regardless of the choice.

type FeedAdapter

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

FeedAdapter adapts feed.Feed to implement BroadcastHub[T].

func NewFeedAdapter

func NewFeedAdapter[T any](feed *feed.Feed[T]) *FeedAdapter[T]

func (*FeedAdapter[T]) NewPublisher

func (a *FeedAdapter[T]) NewPublisher() Publisher[T]

func (*FeedAdapter[T]) NewSubscribable

func (a *FeedAdapter[T]) NewSubscribable(_ LagPolicy[T]) Subscribable[T]

NewSubscribable returns a view over the producer feed itself; every Subscribe is a direct keep-last subscription, so drops are per client and nothing runs in between.

lagPolicy is ignored: feed.Feed has no lag-or-event envelope to transform. The parameter exists only to satisfy the unified BroadcastHub interface so callers wired against KindBroadcast can be repointed at KindFeed without code changes.

type Kind

type Kind int

Kind selects the underlying pub/sub implementation a hub will use.

const (
	// KindFeed routes through feed.Feed: channel-based fan-out, drops on slow subscribers.
	KindFeed Kind = iota
	// KindBroadcast routes through the ring-buffer Broadcast: bounded, surfaces lag explicitly.
	KindBroadcast
)

type LagPolicy

type LagPolicy[T any] func(iter.Seq[ring.EventOrLag[T]]) iter.Seq[T]

LagPolicy converts the ring-buffered EventOrLag stream into a plain T stream, deciding what to do with lag notifications (log, count, drop, ...). Each subscriber supplies its own at Subscribe time so different consumers of the same hub can apply different policies.

func LagPolicyBlockReplay

func LagPolicyBlockReplay(
	reader BlockByNumberReader, logger log.StructuredLogger,
) LagPolicy[*core.WithBloom[*core.Block]]

LagPolicyBlockReplay forwards live blocks and, on a lag notification, recovers the dropped blocks from durable storage, rebuilding each one's event bloom.

Stateful per subscription. Lag is mapped onto block numbers from the last delivered block; a lag before any event is held and replayed in front of the first block, whose number then anchors the range.

Reorgs: the live path is unaffected, since the next block re-anchors the mapping and the reorg itself is announced on the sync's reorg feed. A lag before the first block replays the canonical chain from the database and is reorg-safe; its range is clamped at genesis because the missed count exceeds the height gap by the reorg depth. A lag after the anchor maps missed sequences onto numbers above the old anchor: numbers past the new head are logged and skipped, and the replacement blocks between the fork point and the anchor are not re-sent.

func LagPolicyLog

func LagPolicyLog[T any](logger log.StructuredLogger) LagPolicy[T]

LagPolicyLog yields events and logs each lag notification at warn level with the missed and next-available sequence numbers.

func WithLagObserver

func WithLagObserver[T any](
	observe func(missedSeq, nextSeq uint64), policy LagPolicy[T],
) LagPolicy[T]

WithLagObserver taps lag notifications for observation (metrics, logging, ...), then delegates to policy. It composes with any policy — e.g. WithLagObserver(observe, LagPolicyBlockReplay(reader, logger)) records lag and still recovers. observe is called with the lag envelope's sequence numbers; the gap size is nextSeq-missedSeq. broadcaster stays free of any metrics dependency: callers pass a plain callback.

Note: only KindBroadcast surfaces lag, so observe only fires there; KindFeed drops silently and ignores lag policies entirely.

type Option

type Option func(*options)

Option configures New.

func WithCapacity

func WithCapacity(capacity uint64) Option

WithCapacity sets the ring capacity; only consulted for KindBroadcast.

func WithKind

func WithKind(kind Kind) Option

WithKind selects the backend. Defaults to KindFeed.

type Publisher

type Publisher[T any] interface {
	// Send publishes a message to all subscribers.
	Send(msg T)
}

Publisher defines the interface for sending messages to subscribers.

T is passed to Send by value. Callers use pointer or interface element types (e.g. *core.Block, core.Transaction), so this is a single-word copy — there is no *T and thus no pointer-to-pointer to allocate.

type Subscribable

type Subscribable[T any] interface {
	// Subscribe creates a new subscription that delivers T values.
	Subscribe() Subscription[T]
}

Subscribable defines a read-only interface for creating subscriptions.

type SubscribableSource

type SubscribableSource[T any] interface {
	// NewSubscribable creates a read-only view for creating subscriptions.
	// The lagPolicy transforms the underlying lag-aware stream into plain T values
	// and is the consumer's hook for handling lag (log, count, panic, ...).
	// Implementations that cannot detect lag (e.g. feed) ignore lagPolicy; the ring
	// backend requires it, so every consumer states its lag response explicitly.
	NewSubscribable(lagPolicy LagPolicy[T]) Subscribable[T]
}

SubscribableSource mints Subscribables with per-consumer lag policies. Producers hand this out to consumers that must be able to subscribe but not publish.

type Subscription

type Subscription[T any] interface {
	// Recv returns the channel for receiving messages.
	// The channel is closed when the subscription is terminated.
	Recv() <-chan T

	// Unsubscribe terminates the subscription and closes the channel. Idempotent.
	Unsubscribe()
}

Subscription defines the interface for receiving messages from a subscription.

Directories

Path Synopsis
Package broadcast implements a fan-out event stream with overwrite-on-full semantics.
Package broadcast implements a fan-out event stream with overwrite-on-full semantics.
ring
Package ring implements the MPMC overwrite-on-full ring buffer that backs the broadcast package; see RingBuffer for the sequence and slot protocol.
Package ring implements the MPMC overwrite-on-full ring buffer that backs the broadcast package; see RingBuffer for the sequence and slot protocol.

Jump to

Keyboard shortcuts

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