eventsfx

package
v0.6.30 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

ajan/eventsfx

Overview

The 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)

// 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")

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 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) 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 {
	RequestsTotal *prometheus.CounterVec
	// contains filtered or unexported fields
}

func NewMetrics

func NewMetrics(mp MetricsProvider) *Metrics

type MetricsProvider

type MetricsProvider interface {
	GetRegistry() *prometheus.Registry
}

Jump to

Keyboard shortcuts

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