eventbus

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package eventbus defines a small publish/subscribe abstraction.

Index

Constants

View Source
const (
	HeaderTraceparent  = "traceparent"
	HeaderTracestate   = "tracestate"
	HeaderBaggage      = "baggage"
	HeaderTraceID      = logger.TraceIDKey
	HeaderSpanID       = logger.SpanIDKey
	HeaderTrackID      = tracking.Header
	HeaderRequestID    = requestid.Header
	HeaderConnectionID = "connection_id"
	HeaderSessionID    = "session_id"
)
View Source
const KeyEventBus runtime.Key = "eventbus"

Variables

View Source
var (
	ErrDefaultNotInitialized = errors.New("eventbus: default not initialized")
	ErrClosed                = errors.New("eventbus: closed")
	ErrUnsupported           = errors.New("eventbus: unsupported capability")
	ErrBusNotFound           = errors.New("eventbus: bus not found")
	ErrNilHandler            = errors.New("eventbus: nil handler")
	ErrEmptyTopic            = errors.New("eventbus: empty topic")
)

Functions

func Bind

func Bind(app *runtime.App, bus Service) error

func BindWithDriver

func BindWithDriver(app *runtime.App, bus Service, driver string) error

func CheckPublishCapability

func CheckPublishCapability(options PublishOptions, capability Capability) error

func CheckSubscribeCapability

func CheckSubscribeCapability(options SubscribeOptions, capability Capability) error

func CloseDefault

func CloseDefault() error

func ContextWithEvent

func ContextWithEvent(ctx context.Context, event *Event) context.Context

func DefaultDriver

func DefaultDriver() string

func EventFlowHeaderKeys

func EventFlowHeaderKeys() []string

EventFlowHeaderKeys returns the correlation headers recognized by EventFlow.

EventBus does not own these field semantics. Trace/span/request/track values are injected and extracted through core/tracecontext, core/tracking and core/requestid helpers. Connection/session values are carried only as headers and are intentionally not modeled as Event top-level fields.

func Publish

func Publish(ctx context.Context, event *Event) error

func Register

func Register(name string, bus Service) error

func SafeHandle

func SafeHandle(ctx context.Context, event *Event, handler Handler, log *zap.Logger) (err error)

func SetDefault

func SetDefault(bus Service)

func SetDefaultWithDriver

func SetDefaultWithDriver(bus Service, driver string)

Types

type Bus

type Bus interface {
	Publish(ctx context.Context, event *Event) error
	Subscribe(ctx context.Context, topic string, handler Handler) (Subscription, error)
	Close() error
	Capability() Capability
}

type Capability

type Capability struct {
	Fanout      bool
	Wildcard    bool
	Persistent  bool
	Delay       bool
	Retry       bool
	ConsumerGrp bool
}

type Codec

type Codec interface {
	Marshal(v any) ([]byte, error)
	Unmarshal(data []byte, v any) error
}

type Event

type Event struct {
	ID        string            `json:"id"`
	Topic     string            `json:"topic"`
	Headers   map[string]string `json:"headers,omitempty"`
	Payload   []byte            `json:"payload,omitempty"`
	Timestamp time.Time         `json:"timestamp"`
}

func NewEvent

func NewEvent(ctx context.Context, topic string, payload []byte, headers map[string]string) (*Event, error)

func NewJSONEvent

func NewJSONEvent(ctx context.Context, topic string, payload any, headers map[string]string) (*Event, error)

type EventBus

type EventBus = Bus

type Handler

type Handler func(ctx context.Context, event *Event) error

func Chain

func Chain(handler Handler, middlewares ...Middleware) Handler

type JSONCodec

type JSONCodec struct{}

func (JSONCodec) Marshal

func (JSONCodec) Marshal(v any) ([]byte, error)

func (JSONCodec) Unmarshal

func (JSONCodec) Unmarshal(data []byte, v any) error

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

func NewManager

func NewManager() *Manager

func (*Manager) Close

func (m *Manager) Close() error

func (*Manager) Get

func (m *Manager) Get(name string) (Service, error)

func (*Manager) MustGet

func (m *Manager) MustGet(name string) Service

func (*Manager) Register

func (m *Manager) Register(name string, bus Service) error

type Middleware

type Middleware func(Handler) Handler

func Logger

func Logger(log *zap.Logger) Middleware

func Recover

func Recover(log *zap.Logger) Middleware

func Tracing

func Tracing() Middleware

type PublishOption

type PublishOption func(*PublishOptions)

func WithDelay

func WithDelay(delay time.Duration) PublishOption

func WithHeader

func WithHeader(key, value string) PublishOption

func WithHeaders

func WithHeaders(headers map[string]string) PublishOption

func WithName

func WithName(name string) PublishOption

func WithPersistent

func WithPersistent(persistent bool) PublishOption

func WithRetry

func WithRetry(retry int) PublishOption

func WithTTL

func WithTTL(ttl time.Duration) PublishOption

func WithTraceID

func WithTraceID(traceID string) PublishOption

type PublishOptions

type PublishOptions struct {
	Name       string
	Headers    map[string]string
	TraceID    string
	Delay      time.Duration
	TTL        time.Duration
	Retry      int
	Persistent bool
}

func ApplyPublishOptions

func ApplyPublishOptions(opts ...PublishOption) PublishOptions

type Service

type Service interface {
	Bus
}

func Default

func Default() Service

func DefaultWithDriver

func DefaultWithDriver() (Service, string)

func From

func From(app *runtime.App) Service

func Get

func Get(name string) (Service, error)

func MustGet

func MustGet(name string) Service

type SubscribeOption

type SubscribeOption func(*SubscribeOptions)

func WithConcurrency

func WithConcurrency(concurrency int) SubscribeOption

func WithDurable

func WithDurable(durable bool) SubscribeOption

func WithGroup

func WithGroup(group string) SubscribeOption

type SubscribeOptions

type SubscribeOptions struct {
	Group       string
	Durable     bool
	Concurrency int
}

func ApplySubscribeOptions

func ApplySubscribeOptions(opts ...SubscribeOption) SubscribeOptions

type Subscription

type Subscription interface {
	Close() error
}

func Subscribe

func Subscribe(ctx context.Context, topic string, handler Handler) (Subscription, error)

type SubscriptionFunc

type SubscriptionFunc func() error

func (SubscriptionFunc) Close

func (fn SubscriptionFunc) Close() error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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