Documentation
¶
Overview ¶
Package eventhub provides the event hub abstraction used by the duckflux runner to implement emit and wait.event across all supported backends.
The Hub wraps a Watermill Publisher/Subscriber pair. Three backends are supported: "memory" (GoChannel, default), "nats" (NATS JetStream), and "redis" (Redis Streams). Backend is selected via Config.Backend.
Index ¶
- type Config
- type EventEnvelope
- type Hub
- func (h *Hub) Close() error
- func (h *Hub) Publish(_ context.Context, event string, payload any) error
- func (h *Hub) PublishAndWaitAck(ctx context.Context, event string, payload any, timeout time.Duration) error
- func (h *Hub) Subscribe(ctx context.Context, event string) (<-chan EventEnvelope, func(), error)
- type NATSConfig
- type RedisConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Backend selects the pub/sub implementation: "memory" (default), "nats", or "redis".
Backend string
NATS NATSConfig
Redis RedisConfig
}
Config holds the event hub backend selection and per-backend options.
type EventEnvelope ¶
EventEnvelope is the wire format for events sent over the pub/sub layer. It is JSON-marshaled into the Watermill message payload.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub is the central event hub. It owns the Watermill lifecycle and exposes typed Publish/Subscribe methods used by the engine and participants.
Hub is safe for concurrent use.
func New ¶
New creates a Hub for the backend specified in cfg.Backend. An empty Backend defaults to "memory" (GoChannel).
func (*Hub) Publish ¶
Publish marshals payload into an EventEnvelope and publishes it to the topic equal to event. It is safe to call concurrently.
func (*Hub) PublishAndWaitAck ¶
func (h *Hub) PublishAndWaitAck(ctx context.Context, event string, payload any, timeout time.Duration) error
PublishAndWaitAck publishes an event and waits at most timeout for the publish to be confirmed. For the GoChannel backend this is inherent (synchronous channel delivery). For NATS JetStream and Redis Streams a successful publish call means the broker persisted the message.
If timeout elapses before the publish completes, a non-nil error wrapping context.DeadlineExceeded is returned.
func (*Hub) Subscribe ¶
Subscribe subscribes to the given event topic and returns a channel that delivers decoded EventEnvelopes. The returned cancel function must be called when the subscription is no longer needed to release resources.
The channel is closed when cancel is called or ctx is cancelled.
type NATSConfig ¶
type NATSConfig struct {
// URL is the NATS server URL (e.g. "nats://localhost:4222").
URL string
// StreamName is the JetStream stream name (default: "duckflux-events").
StreamName string
}
NATSConfig holds connection parameters for the NATS JetStream backend.
type RedisConfig ¶
type RedisConfig struct {
// Addr is the Redis server address (default: "localhost:6379").
Addr string
// DB is the Redis database number (default: 0).
DB int
// ConsumerGroup is the Redis consumer group name.
// Defaults to "duckflux" when empty.
ConsumerGroup string
}
RedisConfig holds connection parameters for the Redis Streams backend.