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 ¶
- Variables
- func Publish[T any](ctx context.Context, b *Bus, topic Topic[T], payload T) error
- func PublishAsync[T any](ctx context.Context, b *Bus, topic Topic[T], payload T)
- func Subscribe[T any](b *Bus, topic Topic[T], handler func(ctx context.Context, payload T) error)
- type Bus
- type Eventdeprecated
- type Handlerdeprecated
- type RedisRelay
- func (r *RedisRelay) Channel(signal Signal) string
- func (r *RedisRelay) Close() error
- func (r *RedisRelay) ForwardToBus(ctx context.Context, signal Signal, bus *Bus) error
- func (r *RedisRelay) Publish(ctx context.Context, event Event) error
- func (r *RedisRelay) Subscribe(ctx context.Context, signal Signal, handler Handler) error
- type RedisRelayConfig
- type Signaldeprecated
- type Topic
Constants ¶
This section is empty.
Variables ¶
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") )
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
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
PublishAsync emits a typed event without waiting for its handlers.
Types ¶
type Bus ¶
type Bus struct {
// contains filtered or unexported fields
}
Bus manages signal handlers and dispatches events.
func (*Bus) Clear ¶
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
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
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
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
type Event
deprecated
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
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 ¶
type RedisRelayConfig ¶
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.