Simple event bus written in go
go get -u github.com/dcarbone/seb/v3
package main
import (
"context"
"fmt"
"github.com/dcarbone/seb/v3"
)
func eventHandler(ev seb.Event) {
fmt.Println("Event received:", ev)
}
func main() {
seb.AttachFunc("", eventHandler)
err := seb.Push(context.Background(), "topic-1", map[string]string{"hello": "dave"})
if err != nil {
panic(err.Error())
}
// and so on.
}
package main
import (
"context"
"fmt"
"github.com/dcarbone/seb/v3"
)
func eventHandler(ev seb.Event) {
fmt.Println("Event received:", ev)
}
func main() {
bus := seb.New(
// apply bus options here...
)
bus.AttachFunc("", eventHandler)
err := bus.Push(context.Background(), "topic-1", map[string]string{"hello": "dave"})
if err != nil {
panic(err.Error())
}
// and so on.
}
Depending on the situation, it may be advantageous to receive messages in a func or in a channel and I wanted to support both without requiring the implementor perform the wrapping
Each recipient will always get events in the order they were sent until the recipient starts to block beyond the defined buffer of 100 delayed messages. If this buffer is breached, events for that specific recipient are dropped.
No recipient will prevent or even delay an event from being pushed to other recipients. This is designed to somewhat force event recipients to be as expedient as possible in its implementation, as the rest of the Bus won't wait for it to handle one event before sending another.
When registering a recipient, you may optionally provide a list of string's of specific topics or *regex.Regexp
instances. Your recipient will then only receive events that pass through those filters.
Only use stdlib modules
If you wish to have a particular event func or channel receive events from subset of topics, you may provide either a string or instance
of *regexp.Regexp to the Filtered
series of handler registration funcs on the Bus. Strings are matched exactly, and regex instances
are matched using MatchString
. First match wins, and exact string matches are always compared first.