eventbus

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2022 License: MIT Imports: 2 Imported by: 3

README

~ eventbus ~

A go package to send and receive pub-sub messages using channels.

     

go get -u github.com/zekrotja/eventbus

Intro

This package provides a very simple, generic pub-sub event bus to simplify sending event messages between services using channels.

Why using channels over callbacks?

Callback are - in my opinion - not a very clean and performant way to perform event driven development in Go because, in contrast to languages like JavaScript, Go is not an event driven language. This can lead to blocking publisher routines while waiting for the execution of callbacks on the side of subscribers. Channels, which are well designed to communicate between go routines in the first place, are therefore a way better tool to achieve easy, performant and intuitive communication of events between publishers and subscribers.

Basic Example

package main

import (
	"bufio"
	"fmt"
	"os"

	"github.com/zekrotja/eventbus"
)

func subscriber(name string, bus *eventbus.EventBus[string]) {
	c, _ := bus.Subscribe()
	for msg := range c {
		fmt.Printf("[ -> %s ]: %s\n", name, msg)
	}
}

func main() {
	s := bufio.NewScanner(os.Stdin)
	bus := eventbus.New[string]()

	go subscriber("service1", bus)
	go subscriber("service2", bus)

	bus.SubscribeFunc(func(s string) {
		if s == "exit" {
			os.Exit(0)
		}
	})

	fmt.Println("Publish messages by writing them to the console.\nPress CTRL+C or write 'exit' to exit.")
	for s.Scan() {
		bus.Publish(s.Text())
	}
}

Further examples can be found in the examples directory. If you want to take alook at a practical example, feel free to explore my project Yuri69 which heavily depends on EventBus.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventBus

type EventBus[T any] struct {
	// contains filtered or unexported fields
}

EventBus is an n-to-n pub-sub structure to send and receive event messages between services.

func New

func New[T any](buffSize ...int) *EventBus[T]

New returns a new instance of EventBus with the given type T as message type.

Optionally, you can pass a custon buffer size used for the event subscription channels. It defines the number of messages which can be stored in each subscription channel without blocking the publishing go routine until the message has been picked up by the subscribing go routine. The default value when not passed is 100. When the EventBus might experience high traffic, it is recommendet to allocate larger buffer sized to avoid blocking the publishing go routine.

func (*EventBus[T]) Publish

func (t *EventBus[T]) Publish(v T)

Publish sends the passed message to each subscription to the EventBus.

If no one subscribed to the EventBus, the message will be discarded.

func (*EventBus[T]) Subscribe

func (t *EventBus[T]) Subscribe() (<-chan T, func())

Subscribe returns a channel receiving messages published to the EventBus as well as a function to unsubscribe.

When the unsubscribe function is called, the channel will be closed and the subscription will be removed from the EventBus.

func (*EventBus[T]) SubscribeFunc

func (t *EventBus[T]) SubscribeFunc(f func(T)) func()

SubscribeFunc is a wrapper for Subscribe which calls the given function f when a message has been received passing the received message.

The returned function unsubscribes from the EventBus.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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