eventbus

package module
v0.0.0-...-8897a62 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2020 License: Apache-2.0 Imports: 0 Imported by: 0

README

Project Status - Archived

I'm considering this project archived for a variety of reasons 1) I haven't touched it in a long time 2) there are much better event bus options out there 3) I think since the original authoring of this package that go idioms have become more clear and it's likely there's better things out there.

Never the less, this code is still available for reference for anyone who wants to look at it.

A concurrent EventBus for Go

build status

EventBus is a simple publish and subscribe tool for go. It contains no built-in filtering mechanism, but it is concurrent in nearly every respect, and won't block any process emitting/publishing a message.

The main intention of this project is to support a Go Expect library.

Example

package main

import "github.com/jamesharr/eventbus"

func main() {
	// Create the bus
	bus := eventbus.CreateEventBus()

	// Register a couple handlers
	handler1 = make(chan eventbus.Message)
	handler2 = make(chan eventbus.Message)
	bus.Register(handler1) // Register/Unregister are GoRoutine safe.
	bus.Register(handler2)

	// Emit a couple events onto the bus (will never block0
	bus.Emit("Hello")
	bus.Emit("World")

	// Normally done in another GoRoutine, but this demonstrates the buffering.
	msg := <-handler1 // msg = "Hello"
	msg = <-handler1  // msg = "World"

	// Close the bus, and flush out any messages.
	bus.Close()

	// More buffering
	msg = <-handler2   // msg = "Hello"
	msg = <-handler2   // msg = "World"
	_, ok = <-handler2 // OK = false, bus is closed, all events have been flushed.

	// Same here
	_, ok := <-handler1 // OK = false, bus is closed.

	// This will cause a panic, since the bus is closed.
	bus.Emit("DOH!")

}

API Documentation

API Documentation

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateShimQueue

func CreateShimQueue(input <-chan Message, output chan<- Message)

Create a queue shim

Types

type EventBus

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

A simplistic pub/sub system.

All methods are thread/GoRoutine safe. See Close() for some related caveats. Slow/laggy handlers should not slow down routines Emit()ing events. All messages will be received in the same order on all handlers.

func CreateEventBus

func CreateEventBus() *EventBus

Create an EventBus and start all related GoRoutines.

func (*EventBus) Close

func (bus *EventBus) Close()

Shutdown the event bus.

This will drain all current messages from the queues before closing the handler channels passed to Register(). After closing an EventBus, any attempt to Emit() a message will cause a panic.

func (*EventBus) Emit

func (bus *EventBus) Emit(msg Message)

Emit a message onto the bus

func (*EventBus) Register

func (bus *EventBus) Register(h Handler)

Register a message handler.

func (*EventBus) Unregister

func (bus *EventBus) Unregister(h Handler)

Unregister an event handler.

type Handler

type Handler chan Message

Receiver of events

type Message

type Message interface{}

Item sent on the eventbus

Jump to

Keyboard shortcuts

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