grove

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2024 License: MIT Imports: 3 Imported by: 1

README

Grove: An Event-Driven Message Broker

Grove is a lightweight, efficient, and extensible event-driven message broker written in Go. Inspired by systems like Kafka and RabbitMQ, it's designed to be simple yet powerful, providing foundational event handling mechanisms with scope for expansion.

Features

  • Event Prioritization: Decide the order in which your handlers get executed based on set priorities.
  • Wildcard Subscriptions: Subscribe to multiple events using wildcard patterns.
  • Handler Chaining: Handlers can pass modified data down a chain for sequential processing.
  • Event Grouping: Group events together and handle them using common handlers.
  • Middleware Integration: Process events with middlewares before reaching the actual handlers.

Installation

go get github.com/jhumel/grove

Sample Usage

Basic Setup
import "github.com/jhumel/grove"

tree := grove.New()
Event Handling
handler := func(data interface{}) interface{} {
    // process data
    return modifiedData
}

tree.Attach("eventName", handler)
tree.Emit("eventName", eventData)
Wildcard Subscriptions
tree.Attach("user.*", userHandler)  // Will handle events like user.created, user.deleted, etc.
Event Grouping

Grouping is done inherently through the use of wildcards, as shown above.

Handler Chaining

Handlers can pass data to the next handler:

handler1 := func(data interface{}) interface{} {
    return "Processed by Handler 1"
}

handler2 := func(data interface{}) interface{} {
    // This will receive "Processed by Handler 1"
    return nil
}

tree.Attach("eventName", handler1)
tree.Attach("eventName", handler2)
Middleware
middleware := func(data *interface{}) bool {
    // Modify data
    return true  // Continue the chain
}

tree.Use(middleware)

Roadmap

  • Replay Events: Replay old events for new handlers.
  • Time-to-live (TTL) for handlers.
  • Rate limiting for event emissions.
  • Advanced matching patterns for wildcards.

Contributing

We welcome contributions! Please check the CONTRIBUTING.md file for guidelines.

License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogger added in v1.0.4

func SetLogger(logger *logrus.Logger)

Types

type Event

type Event struct {
	Name string
	Data interface{}
}

type Grove

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

func New

func New() *Grove

func (*Grove) Attach

func (g *Grove) Attach(event string, handler Handler, priority ...int)

Attach subscribes a handler to a specific event with an optional priority (higher value = higher priority).

func (*Grove) AttachWithReplay

func (g *Grove) AttachWithReplay(eventName string, handler Handler)

func (*Grove) CountHandlers

func (g *Grove) CountHandlers(event string) int

CountHandlers returns the number of handlers for the given event.

func (*Grove) Emit

func (g *Grove) Emit(event string, data interface{}) bool

func (*Grove) GetSessionID added in v1.0.3

func (g *Grove) GetSessionID() string

func (*Grove) HasHandler

func (g *Grove) HasHandler(event string) bool

HasHandler checks if there are any handlers for the given event.

func (*Grove) Once

func (g *Grove) Once(event string, handler Handler, priority ...int)

Once attaches a handler that will only be triggered once.

func (*Grove) Prune

func (g *Grove) Prune(event string)

Prune removes all handlers from an event.

func (*Grove) PruneAll added in v1.0.3

func (g *Grove) PruneAll()

PruneAll removes all handlers from all events.

func (*Grove) Use

func (g *Grove) Use(mw Middleware)

Middleware can modify the event data or prevent it from propagating.

type Handler

type Handler func(data interface{}) interface{}

type Middleware

type Middleware func(data *interface{}) bool

type Store

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

func NewStore

func NewStore(grove *Grove) *Store

func (*Store) Get

func (s *Store) Get(key string) (interface{}, bool)

func (*Store) Set

func (s *Store) Set(key string, value interface{})

func (*Store) Watch

func (s *Store) Watch(key string, handler Handler)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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