event

package
v2.0.0-beta.6 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package event is the chok v2 business event bus: typed pub/sub with per-subscriber bounded queues. It is layer two of the lifecycle split (SPEC §3.5): asynchronous, no error propagation, no veto — anything that must be able to abort a phase belongs to component Init or the reload callback, never here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Publish

func Publish[T any](ctx context.Context, b *Bus, ev T)

Publish delivers ev to every subscriber registered for T's concrete type. Never returns an error and never panics (subscriber panics are recovered); after Close it is a silent no-op.

func Subscribe

func Subscribe[T any](b *Bus, fn func(context.Context, T), opts ...SubOpt) (cancel func())

Subscribe registers fn for events of T's concrete type and returns an idempotent cancel. Async subscribers (the default) get their own goroutine and bounded queue.

Types

type Bus

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

Bus routes published values to subscribers registered for the value's concrete type. The zero value is not usable; construct with NewBus.

func NewBus

func NewBus(opts ...BusOpt) *Bus

NewBus constructs an empty bus.

func (*Bus) Close

func (b *Bus) Close(ctx context.Context)

Close stops the bus: subsequent Publish calls become silent no-ops, every async subscriber drains its backlog within the ctx budget (mini-SPEC §2: default 5s from the kernel), then the goroutines stop. Called by the kernel after the last component Close so subscribers observe the complete lifecycle stream.

type BusOpt

type BusOpt func(*Bus)

BusOpt configures NewBus.

func WithLogger

func WithLogger(l Logger) BusOpt

WithLogger routes overflow warnings and subscriber panics to l. Without it the bus stays silent (events are still counted).

type Logger

type Logger interface {
	Warn(msg string, keysAndValues ...any)
	Error(msg string, keysAndValues ...any)
}

Logger is the bus's consumer-side logging contract (overflow warns, subscriber panics). The chok log.Logger satisfies it structurally — defined here so this package stays a leaf under the kernel.

type SubOpt

type SubOpt func(*subscription)

SubOpt configures a subscription.

func WithBlock

func WithBlock() SubOpt

WithBlock makes Publish wait for queue space instead of dropping the oldest event. The wait is bounded by the publish context — on ctx.Done the event is dropped (counted), never deadlocked.

func WithDropOldest

func WithDropOldest() SubOpt

WithDropOldest is the explicit spelling of the default policy.

func WithQueueSize

func WithQueueSize(n int) SubOpt

WithQueueSize overrides the bounded queue capacity (min 1).

func WithSync

func WithSync() SubOpt

WithSync delivers in the publisher's goroutine. Ordering only — there is still no error propagation, and a panicking subscriber is recovered and logged exactly like the async path.

Jump to

Keyboard shortcuts

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