eventbus

package
v0.4.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package eventbus provides a simple publish/subscribe event bus. Plugins and components can optionally use this to communicate with each other.

Index

Constants

View Source
const (
	// PluginName identifies this plugin.
	PluginName = "eventbus"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type EventBus

type EventBus interface {
	// Subscribe registers a handler that receives all published messages.
	Subscribe(topic string, handler Handler)

	// Publish sends a message to all subscribers of the topic.
	Publish(topic string, data any)

	// Wait blocks until locally-initiated operations complete. For in-memory
	// implementations, this means all handlers have finished. For distributed
	// implementations, this means messages have been sent to the remote system.
	Wait(ctx context.Context) error
}

EventBus provides publish/subscribe with broadcast semantics. All subscribers receive every published message.

func FromContext

func FromContext(ctx context.Context) EventBus

FromContext retrieves the event bus from a context.

type EventBusPlugin

type EventBusPlugin struct {
	EventBus
}

EventBusPlugin provides access to an event bus for plugins and components to communicate with each other.

func Plugin

func Plugin(eb EventBus) *EventBusPlugin

Plugin registers an eventbus with a Prefab server for use by other plugins to use. The bus can be retrieved from the request context using the FromContext function.

func (*EventBusPlugin) Name

func (p *EventBusPlugin) Name() string

From prefab.Plugin.

func (*EventBusPlugin) ServerOptions

func (p *EventBusPlugin) ServerOptions() []prefab.ServerOption

From prefab.OptionProvider.

func (*EventBusPlugin) Shutdown

func (p *EventBusPlugin) Shutdown(ctx context.Context) error

From prefab.ShutdownPlugin.

type Handler

type Handler func(context.Context, *Message) error

Handler processes messages from the event bus.

type Message

type Message struct {
	ID      string // Unique identifier
	Topic   string // Topic name
	Data    any    // Payload
	Attempt int    // Delivery attempt (1-based)
	// contains filtered or unexported fields
}

Message wraps event data with metadata.

func NewMessage

func NewMessage(id, topic string, data any) *Message

NewMessage creates a message with default no-op ack/nack functions.

func NewMessageWithCallbacks

func NewMessageWithCallbacks(id, topic string, data any, attempt int, ack, nack func()) *Message

NewMessageWithCallbacks creates a message with custom ack/nack callbacks.

func (*Message) Ack

func (m *Message) Ack()

Ack acknowledges successful processing of the message.

func (*Message) Nack

func (m *Message) Nack()

Nack indicates the message failed to process and should be redelivered.

type Shutdownable

type Shutdownable interface {
	Shutdown(ctx context.Context) error
}

Shutdownable is implemented by EventBus implementations that need graceful shutdown.

type Subscriber

type Subscriber func(context.Context, any) error

Subscriber is a function type for event subscribers. Deprecated: Use Handler instead.

Directories

Path Synopsis
Package membus provides an in-memory implementation of eventbus.EventBus.
Package membus provides an in-memory implementation of eventbus.EventBus.

Jump to

Keyboard shortcuts

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