eventsfx

package
v0.7.3 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

ajan/eventsfx

Overview

eventsfx package provides an event handling and pub/sub system for Go applications. It supports synchronous and asynchronous event handling with configurable timeouts and buffer sizes.

Configuration

Configuration struct for the event bus:

type Config struct {
  DefaultBufferSize int           `conf:"default_buffer_size" default:"100"`
  ReplyTimeout      time.Duration `conf:"reply_timeout"       default:"5s"`
}

Example configuration:

config := &eventsfx.Config{
  DefaultBufferSize: 1000,              // Buffer size for event queue
  ReplyTimeout:      10 * time.Second,  // Timeout for synchronous event replies
}

Features

  • Synchronous and asynchronous event handling
  • Event buffering with configurable queue size
  • Timeout handling for synchronous events
  • Support for multiple subscribers per event
  • Metrics integration with Prometheus
  • Integration with dependency injection

API

EventBus

The main component for event handling:

// Create a new event bus
bus := eventsfx.NewEventBus(config, metricsProvider, logger)
if err := bus.Init(); err != nil {
  panic("unable to initialize event bus")
}

// Subscribe to events
bus.Subscribe("user.created", func(event Event) {
  // Handle event
})

// Publish events asynchronously
bus.Publish(Event{
  Name: "user.created",
  Data: userData,
})

// Publish events synchronously with reply
reply, err := bus.PublishSync(Event{
  Name: "user.validate",
  Data: userData,
})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEventTimeout          = errors.New("event timeout")
	ErrFailedToCreateMetrics = errors.New("failed to create event metrics")
)
View Source
var ErrFailedToBuildEventDispatchesCounter = errors.New(
	"failed to build event dispatches counter",
)

Functions

This section is empty.

Types

type Config

type Config struct {
	DefaultBufferSize int           `conf:"default_buffer_size" default:"100"`
	ReplyTimeout      time.Duration `conf:"reply_timeout"       default:"5s"`
}

type Event

type Event struct {
	Data      any
	ReplyChan chan any
	Name      string
}

type EventBus

type EventBus struct {
	InnerMetrics *Metrics

	Subscribers map[string][]EventHandler
	Queue       chan Event

	Config *Config
	// contains filtered or unexported fields
}

func NewEventBus

func NewEventBus(
	config *Config,
	metricsProvider *metricsfx.MetricsProvider,
	logger *logfx.Logger,
) *EventBus

func (*EventBus) Cleanup

func (bus *EventBus) Cleanup()

func (*EventBus) Dispatch

func (bus *EventBus) Dispatch(event Event)

func (*EventBus) DispatchSync

func (bus *EventBus) DispatchSync(event Event)

func (*EventBus) Init added in v0.7.0

func (bus *EventBus) Init() error

func (*EventBus) Listen

func (bus *EventBus) Listen()

func (*EventBus) Publish

func (bus *EventBus) Publish(event Event)

func (*EventBus) PublishSync

func (bus *EventBus) PublishSync(event Event) (any, error)

func (*EventBus) Subscribe

func (bus *EventBus) Subscribe(eventName string, handler EventHandler)

type EventHandler

type EventHandler func(event Event)

type Metrics

type Metrics struct {
	Provider *metricsfx.MetricsProvider

	EventDispatchesTotal *metricsfx.CounterMetric
}

Metrics holds event-specific metrics using the simplified MetricsBuilder approach.

func NewMetrics

func NewMetrics(provider *metricsfx.MetricsProvider) *Metrics

NewMetrics creates event metrics using the simplified MetricsBuilder.

func (*Metrics) Init added in v0.7.0

func (metrics *Metrics) Init() error

Jump to

Keyboard shortcuts

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