signals

package
v1.36.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 12 Imported by: 3 modules, 1 package

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 signals provides a synchronous and asynchronous event bus for the Nucleus framework. It implements a pattern similar to Django signals, allowing decoupled components to react to model lifecycle events (pre/post save, delete, etc.).

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRedisURLRequired = errors.New("signals: redis url is required")
	ErrSignalRequired   = errors.New("signals: signal is required")
	ErrNilHandler       = errors.New("signals: handler is required")
	ErrNilBus           = errors.New("signals: bus is nil")
	ErrNilRelay         = errors.New("signals: relay is nil")
)
View Source
var ErrHandlerPanic = errors.New("signals: handler panicked")

ErrHandlerPanic is what a panicking handler becomes. It is a distinct error so a caller can tell "the subscriber refused this operation" from "the subscriber is broken".

Functions

func Publish added in v1.30.0

func Publish[T any](ctx context.Context, b *Bus, topic Topic[T], payload T) error

Publish emits a typed event synchronously, returning what the handlers returned — the same semantics as Emit, which aborts the caller's operation when a subscriber refuses it.

func PublishAsync added in v1.30.0

func PublishAsync[T any](ctx context.Context, b *Bus, topic Topic[T], payload T)

PublishAsync emits a typed event without waiting for its handlers.

func Subscribe added in v1.30.0

func Subscribe[T any](b *Bus, topic Topic[T], handler func(ctx context.Context, payload T) error)

Subscribe registers a typed handler.

The handler receives the context the event was published with, so work it starts belongs to the request or the job that caused it — the untyped API carries the context inside the event and most handlers ignore it.

Types

type Bus

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

Bus manages signal handlers and dispatches events.

func NewBus

func NewBus(logger *slog.Logger) *Bus

NewBus creates a new signal bus. The logger is used for async error reporting.

func (*Bus) Clear

func (b *Bus) Clear(signals ...Signal)

Clear removes all handlers for the given signal, or all handlers if no signal is specified. Useful for testing.

func (*Bus) Dropped added in v1.30.0

func (b *Bus) Dropped() int64

Dropped reports how many asynchronous dispatches were dropped because too many were already in flight. A bus that silently drops is worse than one that blocks; this is how an application notices.

func (*Bus) Emit deprecated

func (b *Bus) Emit(event Event) error

Emit dispatches an event synchronously. All registered handlers for the event's signal are called in order. If any handler returns an error, execution stops and the error is returned.

A handler that PANICS is turned into that same error rather than unwinding into the caller. The asynchronous path has recovered since NU-35; this one did not, so a subscriber could take down the model save or the request that emitted the event — two paths of one bus with different guarantees, and the dangerous one was the path that runs inside the user's operation (NU-79).

Deprecated: use Publish with a Topic. See DEP-2026-025. Scheduled for removal in v2.0.0, no earlier than 2027-01-17.

func (*Bus) EmitAsync deprecated

func (b *Bus) EmitAsync(event Event)

EmitAsync dispatches an event asynchronously. Each handler runs in its own goroutine. Errors are logged but do not propagate.

It does NOT block the caller. It used to: the concurrency slot was taken on the caller's goroutine, so once the ceiling was full the emitter waited for a handler to finish — and the emitter is usually a model save inside a request, so the slowest subscriber became latency for the user (NU-78). The slot is now taken inside the goroutine.

That leaves the question the old shape answered by accident: what happens when handlers cannot keep up. The answer is written down rather than inherited — a bounded number of dispatches may be in flight, and beyond that an event is DROPPED with a log. Dropping is the honest failure for a bus whose contract already says errors do not propagate; blocking the emitter is not, because it turns a subscriber's problem into the user's.

Deprecated: use PublishAsync with a Topic. See DEP-2026-025. Scheduled for removal in v2.0.0, no earlier than 2027-01-17.

func (*Bus) On deprecated

func (b *Bus) On(signal Signal, handler Handler)

On registers a handler for the given signal. Handlers are called in registration order when the signal is emitted.

Deprecated: use Subscribe with a Topic. See DEP-2026-025. Scheduled for removal in v2.0.0, no earlier than 2027-01-17.

type Event deprecated

type Event struct {
	Signal    Signal
	ModelName string
	Payload   any
	Ctx       context.Context
}

Event carries the data associated with a signal emission.

Deprecated: Payload is `any`, so every handler asserts the type it hopes for and a producer that changes it breaks its subscribers at run time. A typed Subscribe receives the payload as the type its Topic declares, and the context the event was published with. See DEP-2026-025. Scheduled for removal in v2.0.0, no earlier than 2027-01-17.

type Handler deprecated

type Handler func(Event) error

Handler is a function that processes an event. Returning an error from a synchronous handler aborts the operation that triggered the signal.

Deprecated: a typed handler, func(ctx context.Context, payload T) error, is what Subscribe takes. See DEP-2026-025. Scheduled for removal in v2.0.0, no earlier than 2027-01-17.

type RedisRelay

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

func NewRedisRelay

func NewRedisRelay(cfg RedisRelayConfig, logger *slog.Logger) (*RedisRelay, error)

func (*RedisRelay) Channel

func (r *RedisRelay) Channel(signal Signal) string

func (*RedisRelay) Close

func (r *RedisRelay) Close() error

func (*RedisRelay) ForwardToBus

func (r *RedisRelay) ForwardToBus(ctx context.Context, signal Signal, bus *Bus) error

func (*RedisRelay) Publish

func (r *RedisRelay) Publish(ctx context.Context, event Event) error

func (*RedisRelay) Subscribe

func (r *RedisRelay) Subscribe(ctx context.Context, signal Signal, handler Handler) error

type RedisRelayConfig

type RedisRelayConfig struct {
	RedisURL      string
	ChannelPrefix string
}

type Signal deprecated

type Signal string

Signal identifies the type of event being emitted.

Deprecated: the untyped door of the bus leaves with the major. Name an event with Define and read a topic's name with Topic.String; the typed Subscribe, Publish and PublishAsync carry the payload with its type. See DEP-2026-025. Scheduled for removal in v2.0.0, no earlier than 2027-01-17.

const (
	PreCreate  Signal = "pre_create"
	PostCreate Signal = "post_create"
	PreSave    Signal = "pre_save"
	PostSave   Signal = "post_save"
	PreDelete  Signal = "pre_delete"
	PostDelete Signal = "post_delete"
	PreUpdate  Signal = "pre_update"
	PostUpdate Signal = "post_update"
)

Built-in signals for model lifecycle events.

Deprecated: pkg/model emits them, and pkg/model leaves the public API with the major (DEP-2026-024), so these names leave with it. A subscriber to a model's lifecycle moves to Quark's hooks; an application's own events are named with Define. See DEP-2026-025. Scheduled for removal in v2.0.0, no earlier than 2027-01-17.

type Topic added in v1.30.0

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

Topic is a named event with a payload type.

It exists because the untyped bus moves `any`: every handler asserts the type it hopes for, and a producer that changes the payload breaks its subscribers at run time, in production, one event at a time. A Topic makes the payload part of the name, so the compiler is the one that notices.

var InvoicePaid = signals.Define[InvoicePayload]("invoice.paid")

signals.Subscribe(bus, InvoicePaid, func(ctx context.Context, p InvoicePayload) error {
        return mailer.Send(ctx, p.CustomerEmail)
})

_ = signals.Publish(ctx, bus, InvoicePaid, InvoicePayload{...})

It is delivered ALONGSIDE the untyped API, not instead of it: Signal, Event and Handler are published, and QADR-0010 holds breaking changes until the major at the close of A12. A typed publish reaches untyped subscribers of the same signal and the other way round — it is one bus, with two doors.

func Define added in v1.30.0

func Define[T any](name string) Topic[T]

Define names a topic and fixes its payload type.

func (Topic[T]) Name deprecated added in v1.30.0

func (t Topic[T]) Name() Signal

Name returns the underlying signal, which is what untyped subscribers use.

Deprecated: it returns a Signal, which leaves with the untyped door; use String. See DEP-2026-025. Scheduled for removal in v2.0.0, no earlier than 2027-01-17.

func (Topic[T]) String added in v1.33.0

func (t Topic[T]) String() string

String returns the topic's name: the string Define was given.

Jump to

Keyboard shortcuts

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