Documentation
¶
Overview ¶
Package event provides domain event primitives for use inside one bounded context. Concrete dispatchers may be in-memory or backed by external middleware, but the package does not expose broker-specific concepts.
The package is intentionally scoped to domain events inside one bounded context. It is not an integration message bus, broker abstraction, transactional outbox, or reliable delivery mechanism across process restarts.
Dispatch errors report only dispatcher admission or delivery failures. They do not report handler success or failure. Handlers represent follow-up transactions and own their own error policy.
Index ¶
- Variables
- type CloseInterruptedContext
- type Collection
- type Dispatcher
- type Event
- type Handler
- type InMemoryDispatcher
- type Kind
- type Option
- func WithBufferSize(size int) Option
- func WithCloseInterruptedHandler(fn func(CloseInterruptedContext)) Option
- func WithContextFactory(fn func(context.Context, Event) context.Context) Option
- func WithDelayClose(delay time.Duration) Option
- func WithHandlerTimeout(timeout time.Duration) Option
- func WithLogger(logger *slog.Logger) Option
- func WithPanicHandler(fn func(PanicContext)) Option
- func WithUnhandledEventHandler(fn func(UnhandledContext)) Option
- type PanicContext
- type PendingBatch
- type Subscriber
- type UnhandledContext
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrDispatcherClosed = errors.New("domain event dispatcher is closed")
ErrDispatcherClosed reports that a dispatcher cannot accept new events because it is closing or closed.
Functions ¶
This section is empty.
Types ¶
type CloseInterruptedContext ¶
type CloseInterruptedContext struct {
Error error
InFlightBatchID uint64
PendingBatches []PendingBatch
}
CloseInterruptedContext describes accepted work that was not confirmed as handled before Close was interrupted.
type Collection ¶
Collection stores domain events raised by an aggregate until the application layer drains them after persistence succeeds.
func NewCollection ¶
func NewCollection() Collection
NewCollection creates an empty aggregate event collection.
Example ¶
package main
import (
"fmt"
"github.com/go-jimu/components/ddd/event"
)
type exampleDomainEvent struct {
kind event.Kind
}
func (e exampleDomainEvent) Kind() event.Kind {
return e.kind
}
func main() {
events := event.NewCollection()
events.Add(exampleDomainEvent{kind: "order.paid"})
events.Add(exampleDomainEvent{kind: "order.confirmed"})
fmt.Println("pending", events.Len())
drained := events.Drain()
fmt.Println(drained[0].Kind(), drained[1].Kind(), events.Len())
}
Output: pending 2 order.paid order.confirmed 0
type Dispatcher ¶
type Dispatcher interface {
Dispatch(Event) error
DispatchAll([]Event) error
Close(context.Context) error
}
Dispatcher accepts domain event batches for handling.
type Event ¶
type Event interface {
Kind() Kind
}
Event is a domain fact raised inside one bounded context.
type InMemoryDispatcher ¶ added in v0.7.4
type InMemoryDispatcher struct {
// contains filtered or unexported fields
}
func NewDispatcher ¶
func NewDispatcher(opts ...Option) *InMemoryDispatcher
NewDispatcher creates an in-process dispatcher with one background worker.
func (*InMemoryDispatcher) Close ¶ added in v0.7.4
func (d *InMemoryDispatcher) Close(ctx context.Context) error
func (*InMemoryDispatcher) Dispatch ¶ added in v0.7.4
func (d *InMemoryDispatcher) Dispatch(event Event) error
func (*InMemoryDispatcher) DispatchAll ¶ added in v0.7.4
func (d *InMemoryDispatcher) DispatchAll(events []Event) error
func (*InMemoryDispatcher) Subscribe ¶ added in v0.7.4
func (d *InMemoryDispatcher) Subscribe(handler Handler)
type Option ¶
type Option func(*InMemoryDispatcher)
Option configures a Dispatcher during construction.
func WithBufferSize ¶
WithBufferSize sets the maximum number of queued event batches.
func WithCloseInterruptedHandler ¶
func WithCloseInterruptedHandler(fn func(CloseInterruptedContext)) Option
WithCloseInterruptedHandler sets a hook for close interruptions that leave accepted work unconfirmed.
func WithContextFactory ¶
WithContextFactory sets a function to derive a new context for each event dispatch.
func WithDelayClose ¶
WithDelayClose sets the delay before the dispatcher starts rejecting new events during shutdown.
func WithHandlerTimeout ¶
WithHandlerTimeout sets the timeout for each handler invocation.
func WithLogger ¶
WithLogger sets the logger for dispatcher lifecycle and runtime diagnostics.
func WithPanicHandler ¶
func WithPanicHandler(fn func(PanicContext)) Option
WithPanicHandler sets a hook for recovered handler panics.
func WithUnhandledEventHandler ¶
func WithUnhandledEventHandler(fn func(UnhandledContext)) Option
WithUnhandledEventHandler sets a hook for events with no registered handler.
type PanicContext ¶
PanicContext describes a recovered handler panic.
type PendingBatch ¶
PendingBatch describes an accepted event batch that was not started before Close was interrupted.
type Subscriber ¶ added in v0.7.4
type Subscriber interface {
Subscribe(Handler)
}
Subscriber registers handlers for domain events.
type UnhandledContext ¶
UnhandledContext describes an event that has no registered handler.