bus

package module
Version: v3.0.3 Latest Latest
Warning

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

Go to latest
Published: May 11, 2021 License: Apache-2.0 Imports: 5 Imported by: 9

README

🔊 Bus

GoDoc Build Status Coverage Status Go Report Card GitHub license

Bus is a minimalist event/message bus implementation for internal communication. It is heavily inspired from my event_bus package for Elixir language.

API

The method names and arities/args are stable now. No change should be expected on the package for the version 3.x.x except any bug fixes.

Installation

Via go packages: go get github.com/mustafaturan/bus/v3

Usage

Configure

The package requires a unique id generator to assign ids to events. You can write your own function to generate unique ids or use a package that provides unique id generation functionality.

The bus package respect to software design choice of the packages/projects. It supports both singleton and dependency injection to init a bus instance.

Hint: Check the demo project for the singleton configuration.

Here is a sample initilization using monoton id generator:

import (
    "github.com/mustafaturan/bus/v3"
    "github.com/mustafaturan/monoton/v2"
    "github.com/mustafaturan/monoton/v2/sequencer"
)

func NewBus() *bus.Bus {
    // configure id generator (it doesn't have to be monoton)
    node        := uint64(1)
    initialTime := uint64(1577865600000) // set 2020-01-01 PST as initial time
    m, err := monoton.New(sequencer.NewMillisecond(), node, initialTime)
    if err != nil {
        panic(err)
    }

    // init an id generator
    var idGenerator bus.Next = m.Next

    // create a new bus instance
    b, err := bus.NewBus(idGenerator)
    if err != nil {
        panic(err)
    }

    // maybe register topics in here
    b.RegisterTopics("order.received", "order.fulfilled")

    return b
}
Register Event Topics

To emit events to the topics, topic names need to be registered first:

// register topics
b.RegisterTopics("order.received", "order.fulfilled")
Register Event Handlers

To receive topic events you need to register handlers; A handler basically requires two vals which are a Handle function and topic Matcher regex pattern.

handler := bus.Handler{
    Handle: func(ctx context.Context, e bus.Event) {
        // do something
        // NOTE: Highly recommended to process the event in an async way
    },
    Matcher: ".*", // matches all topics
}
b.RegisterHandler("a unique key for the handler", handler)
Emit Events
// if txID val is blank, bus package generates one using the id generator
ctx := context.Background()
ctx = context.WithValue(ctx, bus.CtxKeyTxID, "some-transaction-id-if-exists")
// with optional source
ctx = context.WithValue(ctx, bus.CtxKeySource, "source-of-the-event")

// event topic name (must be registered before)
topic := "order.received"

// interface{} data for event
order := make(map[string]string)
order["orderID"]     = "123456"
order["orderAmount"] = "112.20"
order["currency"]    = "USD"

// emit the event
err := b.Emit(ctx, topic, order)

if err != nil {
    // report the err
    fmt.Println(err)
}

// emit the event with options
err := b.EmitWithOpts(ctx, topic, order, bus.WithTxID("some-tx-id"))

if err != nil {
    // report the err
    fmt.Println(err)
}
Processing Events

When an event is emitted, the topic handlers receive the event synchronously. It is highly recommended to process events asynchronous. Package leave the decision to the packages/projects to use concurrency abstractions depending on use-cases. Each handlers receive the same event as ref of bus.Event struct:

// Event data structure
type Event struct {
    ID         string      // identifier
    TxID       string      // transaction identifier
    Topic      string      // topic name
    Source     string      // source of the event
    OccurredAt time.Time   // creation time in nanoseconds
    Data       interface{} // actual event data
}
Sample Project

A demo project with three consumers which increments a counter for each event topic, printer consumer which prints all events and lastly calculator consumer which sums amounts.

Benchmarks

Command:

go test -benchtime 10000000x -benchmem -run=^$ -bench=. github.com/mustafaturan/bus/v3

Results:

goos: darwin
goarch: amd64
pkg: github.com/mustafaturan/bus/v3
cpu: Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
BenchmarkEmit-4                      	10000000	       180.5 ns/op	       8 B/op	       0 allocs/op
BenchmarkEmitWithoutTxID-4           	10000000	       244.6 ns/op	      72 B/op	       2 allocs/op
BenchmarkEmitWithOpts-4              	10000000	       280.8 ns/op	     112 B/op	       4 allocs/op
BenchmarkEmitWithOptsUnspecified-4   	10000000	       169.4 ns/op	       8 B/op	       0 allocs/op
PASS
ok  	github.com/mustafaturan/bus/v3	8.884s

Contributing

All contributors should follow Contributing Guidelines before creating pull requests.

Credits

Mustafa Turan

License

Apache License 2.0

Copyright (c) 2021 Mustafa Turan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package bus is a minimalist event/message bus implementation for internal communication

The package requires a unique id generator to assign ids to events. You can write your own function to generate unique ids or use a package that provides unique id generation functionality.

The `bus` package respect to software design choice of the packages/projects. It supports both singleton and dependency injection to init a `bus` instance.

Here is a sample initilization using `monoton` id generator:

Example code for configuration:

import (
	"github.com/mustafaturan/bus/v2"
	"github.com/mustafaturan/monoton/v2"
	"github.com/mustafaturan/monoton/v2/sequencer"
)

func NewBus() *bus.Bus {
	// configure id generator (it doesn't have to be monoton)
	node        := uint64(1)
	initialTime := uint64(1577865600000) // set 2020-01-01 PST as start
	m, err := monoton.New(sequencer.NewMillisecond(), node, initialTime)
	if err != nil {
		panic(err)
	}

	// init an id generator
	var idGenerator bus.Next = m.Next

	// create a new bus instance
	b, err := bus.NewBus(idGenerator)
	if err != nil {
		panic(err)
	}

	// maybe register topics in here
	b.RegisterTopics("order.received", "order.fulfilled")

	return b
}

Register Topics

To emit events to the topics, topic names should be registered first:

Example code:

// register topics
b.RegisterTopics("order.received", "order.fulfilled")
// ...

Register Handlers

To receive topic events you need to register handlers; A handler basically requires two vals which are a `Handle` function and topic `Matcher` regex pattern.

Example code:

handler := bus.Handler{
	Handle: func(ctx context.Context, e Event) {
		// do something
		// NOTE: Highly recommended to process the event in an async way
	},
	Matcher: ".*", // matches all topics
}
b.RegisterHandler("a unique key for the handler", handler)

Emit Event

Example code:

// if txID val is blank, bus package generates one using the id generator
ctx := context.Background()
ctx = context.WithValue(ctx, bus.CtxKeyTxID, "a-transaction-id")

// event topic name (must be registered before)
topic := "order.received"

// interface{} data for event
order := make(map[string]string)
order["orderID"]     = "123456"
order["orderAmount"] = "112.20"
order["currency"]    = "USD"

// emit the event
err := b.Emit(ctx, topic, order)

if err != nil {
	// report the err
	fmt.Println(err)
}

// emit an event with opts
err := b.EmitWithOpts(ctx, topic, order, bus.WithTxID("tx-id-val"))
if err != nil {
	// report the err
	fmt.Println(err)
}

Processing Events

When an event is emitted, the topic handlers receive the event synchronously. It is highly recommended to process events asynchronous. Package leave the decision to the packages/projects to use concurrency abstractions depending on use-cases. Each handlers receive the same event as ref of `bus.Event` struct.

Index

Constants

View Source
const (
	// CtxKeyTxID tx id context key
	CtxKeyTxID = ctxKey(116)

	// CtxKeySource source context key
	CtxKeySource = ctxKey(117)

	// Version syncs with package version
	Version = "3.0.3"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bus

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

Bus is a message bus

func NewBus

func NewBus(g IDGenerator) (*Bus, error)

NewBus inits a new bus

func (*Bus) DeregisterHandler

func (b *Bus) DeregisterHandler(key string)

DeregisterHandler deletes handler from the registry

func (*Bus) DeregisterTopics

func (b *Bus) DeregisterTopics(topics ...string)

DeregisterTopics deletes topic

func (*Bus) Emit

func (b *Bus) Emit(ctx context.Context, topic string, data interface{}) error

Emit inits a new event and delivers to the interested in handlers with sync safety

func (*Bus) EmitWithOpts

func (b *Bus) EmitWithOpts(ctx context.Context, topic string, data interface{}, opts ...EventOption) error

EmitWithOpts inits a new event and delivers to the interested in handlers with sync safety and options

func (*Bus) HandlerKeys

func (b *Bus) HandlerKeys() []string

HandlerKeys returns list of registered handler keys

func (*Bus) HandlerTopicSubscriptions

func (b *Bus) HandlerTopicSubscriptions(handlerKey string) []string

HandlerTopicSubscriptions returns all topic subscriptions of the handler

func (*Bus) RegisterHandler

func (b *Bus) RegisterHandler(key string, h Handler)

RegisterHandler re/register the handler to the registry

func (*Bus) RegisterTopics

func (b *Bus) RegisterTopics(topics ...string)

RegisterTopics registers topics and fullfills handlers

func (*Bus) TopicHandlerKeys

func (b *Bus) TopicHandlerKeys(topic string) []string

TopicHandlerKeys returns all handlers for the topic

func (*Bus) Topics

func (b *Bus) Topics() []string

Topics lists the all registered topics

type Event

type Event struct {
	ID         string      // identifier
	TxID       string      // transaction identifier
	Topic      string      // topic name
	Source     string      // source of the event
	OccurredAt time.Time   // creation time in nanoseconds
	Data       interface{} // actual event data
}

Event is data structure for any logs

type EventOption

type EventOption = func(Event) Event

EventOption is a function type to mutate event fields

func WithID

func WithID(id string) EventOption

WithID returns an option to set event's id field

func WithOccurredAt

func WithOccurredAt(time time.Time) EventOption

WithOccurredAt returns an option to set event's occurredAt field

func WithSource

func WithSource(source string) EventOption

WithSource returns an option to set event's source field

func WithTxID

func WithTxID(txID string) EventOption

WithTxID returns an option to set event's txID field

type Handler

type Handler struct {

	// handler func to process events
	Handle func(ctx context.Context, e Event)

	// topic matcher as regex pattern
	Matcher string
	// contains filtered or unexported fields
}

Handler is a receiver for event reference with the given regex pattern

type IDGenerator

type IDGenerator interface {
	Generate() string
}

IDGenerator is a sequential unique id generator interface

type Next

type Next func() string

Next is a sequential unique id generator func type

func (Next) Generate

func (n Next) Generate() string

Generate is an implementation of IDGenerator for bus.Next fn type

Source Files

Jump to

Keyboard shortcuts

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