eventhub

package
v1.1.8 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 11 Imported by: 0

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

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

type EventEnvelope struct {
	Name    string `json:"name"`
	Payload any    `json:"payload"`
}

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

func New(cfg Config) (*Hub, error)

New creates a Hub for the backend specified in cfg.Backend. An empty Backend defaults to "memory" (GoChannel).

func (*Hub) Close

func (h *Hub) Close() error

Close shuts down all backend connections. It is safe to call once.

func (*Hub) Publish

func (h *Hub) Publish(_ context.Context, event string, payload any) error

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

func (h *Hub) Subscribe(ctx context.Context, event string) (<-chan EventEnvelope, func(), error)

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.

Jump to

Keyboard shortcuts

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