edaRR

package module
v0.0.0-...-dfa5485 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2025 License: MIT Imports: 10 Imported by: 0

README

EDA-RR

EDA-RR is a simple event library for Go, extracted from a personal project. It provides basic functionality for organizing communication between application components through events.

Features

  • Publish-Subscribe: Asynchronous event publishing and subscription
  • Routing Patterns: Support for single (*) and multiple (**) wildcards
  • Event Filtering: Subscribe to events with content-based filtering
  • Request-Response: Synchronous communication between components
  • Multiple Publishing: Publishing events to multiple topics
  • Multiple Requests: Parallel request dispatching to multiple recipients

Installation

go get github.com/Akrab/EDA-RR

Quick Start

package main

import (
  "log"
  "os"
  "time"

  "github.com/Akrab/EDA-RR"
)

func main() {
  // Create an event bus
  eventBus := eda.NewEventBus(log.New(os.Stdout, "[EDA] ", log.LstdFlags))

  // Subscribe to events
  ch, unsubscribe := eventBus.Subscribe("user.login", 10)
  defer unsubscribe()

  // Process events
  go func() {
    for event := range ch {
      log.Printf("Received event: %v", event)
    }
  }()

  // Publish an event
  eventBus.Publish("user.login", map[string]string{
    "username": "john_doe",
  })

  time.Sleep(100 * time.Millisecond)
}

Usage Examples

See the examples directory for samples:

Documentation

Core Concepts
  • Event: Contains identifier, type, data, and metadata

  • Subscription: Links an event pattern to a processing channel

  • Pattern: Defines event routing rules

    • user.login - exact match
    • user.* - any event with "user." prefix and one segment
    • user.** - any event with "user." prefix and any number of segments
MIT. No warranties or liabilities.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateID

func GenerateID(eventType string) string

func NewError

func NewError(msg string) error

Types

type Event

type Event struct {
	ID            string                 // Unique event identifier
	Type          string                 // Event type
	CorrelationID string                 // ID for related events (e.g., request-response)
	ReplyTo       string                 // Topic for response event (optional)
	Data          interface{}            // Event payload
	Timestamp     time.Time              // Event creation timestamp
	Metadata      map[string]interface{} // Additional metadata
}

type EventBus

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

func NewEventBus

func NewEventBus(logger *log.Logger) *EventBus

func (*EventBus) DeliverResponse

func (eb *EventBus) DeliverResponse(originalEvent Event, responseData interface{}, err error)

func (*EventBus) Publish

func (eb *EventBus) Publish(eventType string, data interface{}, metadata ...map[string]interface{}) string

func (*EventBus) PublishEvent

func (eb *EventBus) PublishEvent(event Event)

func (*EventBus) PublishMultiple

func (eb *EventBus) PublishMultiple(eventTypes []string, data interface{}, metadata ...map[string]interface{}) []string

func (*EventBus) RequestMultiple

func (eb *EventBus) RequestMultiple(ctx context.Context, requestTypes []string, data interface{}, timeout time.Duration) (map[string]interface{}, []error)

RequestMultiple sends a request to multiple recipients and collects responses

func (*EventBus) RequestReply

func (eb *EventBus) RequestReply(ctx context.Context, requestType string, data interface{}, timeout time.Duration) (interface{}, error)

func (*EventBus) Subscribe

func (eb *EventBus) Subscribe(pattern string, bufferSize int) (<-chan Event, func())

func (*EventBus) SubscribeWithFilter

func (eb *EventBus) SubscribeWithFilter(pattern string, bufferSize int, filter FilterFunc) (<-chan Event, func())

type FilterFunc

type FilterFunc func(Event) bool

type Subscription

type Subscription struct {
	ID      string
	Channel chan Event
	Done    chan struct{} // Channel for signaling completion
}

Directories

Path Synopsis
examples
basic command
patterns command
request-reply command

Jump to

Keyboard shortcuts

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