Skip to content
🔊Minimalist message bus implementation for internal communication
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/ISSUE_TEMPLATE Add issue templates Apr 30, 2019
.gitignore Init for opensource Apr 27, 2019
.travis.yml Init for opensource Apr 27, 2019
CODE_OF_CONDUCT.md Init for opensource Apr 27, 2019
CONTRIBUTING.md Init for opensource Apr 27, 2019
LICENSE Add the missing content for license Apr 28, 2019
README.md Add benchmark test and output May 17, 2019
benchmark_test.go Add benchmark test and output May 17, 2019
bus.go Init for opensource Apr 27, 2019
bus_test.go Init for opensource Apr 27, 2019
config.go Init for opensource Apr 27, 2019
config_actions.go Improve error messages and related tests Apr 30, 2019
config_actions_test.go Improve error messages and related tests Apr 30, 2019
config_test.go Init for opensource Apr 27, 2019
doc.go Init for opensource Apr 27, 2019
event.go Init for opensource Apr 27, 2019
event_actions.go Improve error messages and related tests Apr 30, 2019
event_actions_test.go Improve error messages and related tests Apr 30, 2019
event_test.go Init for opensource Apr 27, 2019
go.mod Init for opensource Apr 27, 2019
go.sum Init for opensource Apr 27, 2019
handler.go Improvements on var inits Apr 27, 2019
handler_actions.go Improvements on var inits Apr 27, 2019
handler_actions_test.go Fix flaky test Apr 27, 2019
handler_test.go Fix flaky test Apr 27, 2019
topic.go Init for opensource Apr 27, 2019
topic_actions.go
topic_actions_test.go Fix flaky test Apr 27, 2019
topic_test.go Improve formatting Apr 27, 2019

README.md

🔊 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.

Installation

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

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. Here is a sample configuration using monoton id generator:

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

func init() {
    // configure id generator (it doesn't have to be monoton)
    node        := uint(1)
    initialTime := uint(0)
    monoton.Configure(sequencer.NewMillisecond(), node, initialTime)

    // configure bus
    if err := bus.Configure(bus.Config{Next: monoton.Next}); err != nil {
        panic("whoops")
    }
    // ...
}

Register Event Topics

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

func init() {
    // ...
    // register topics
    bus.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(e *Event){
        // do something
        // NOTE: Highly recommended to process the event in an async way
    }),
    Matcher: ".*", // matches all topics
}
bus.RegisterHandler("a unique key for the handler", &handler)

Emit Events

txID  := "some-transaction-id-if-exists" // if it is blank, bus will generate one
topic := "order.received" // event topic name (must be registered before)
order := make(map[string]string) // interface{} data for event
order["orderID"]     = "123456"
order["orderAmount"] = "112.20"
order["currency"]    = "USD"

bus.Emit(topic, order, txID) // emit the event for the topic

Processing Events

When an event is emitted, the topic handlers will receive events with order. It is highly recommended to process events asynchronous. Package leave the decision to the packages/projects to use concurrency abstractions depending on use-cases.

Sample Project

An example simple 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

BenchmarkEmit-8         10000000               158 ns/op              72 B/op          2 allocs/op

Contributing

All contributors should follow Contributing Guidelines before creating pull requests.

Credits

Mustafa Turan

License

Apache License 2.0

Copyright (c) 2019 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.

You can’t perform that action at this time.