gobus

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2019 License: GPL-3.0 Imports: 0 Imported by: 0

README

gobus

PubSub Event bus for Go

Build Status Go Report Card Software License Build Status

Package go-bus is a thread-safe, zero-alloc, pub-sub event bus for embebbed go apps and microservices.

Install

go get github.com/zerjioang/go-bus

TL;DR

func TestExampleCalculationBus(t *testing.T) {
	//define a global bus instance
	bus := mutex.NewBus()

	//register our subscriber: the calculation engine
	bus.Subscribe("calc", func(message gobus.EventMessage) {
		println("new calculation request arrived")
		a := message.Get("a").(int)
		b := message.Get("b").(int)
		println("received a = ", a)
		println("received b = ", b)
		println("the sum is", a+b)
	})

	//register our data publisher
	bus.Send("calc", gobus.EventPayload{
		"a": 5,
		"b": 10,
	})

	bus.Send("calc", gobus.EventPayload{
		"a": 8,
		"b": 12,
	})

	bus.Shutdown()
}
new calculation request arrived
new calculation request arrived
received a =  5
received b =  10
received a =  8
received b =  12
the sum is 15
the sum is 20

Benchmarking

Always do benchmarking with your own data. Here are mine:

For mutex based version

BenchmarkEventBus/instantiation-4                     2000000000	         0.42 ns/op	2394.34 MB/s	       0 B/op	       0 allocs/op
BenchmarkEventBus/instantiation-ptr-                  2000000000	         0.43 ns/op	2310.56 MB/s	       0 B/op	       0 allocs/op
BenchmarkEventBus/str-to-uint32-4                       50000000	        37.6 ns/op	  26.56 MB/s	       0 B/op	       0 allocs/op
BenchmarkEventBus/subscribe-4                            5000000	       274 ns/op	   3.64 MB/s	      43 B/op	       0 allocs/op
BenchmarkEventBus/subscribe-invalid-no-name-4          500000000	         3.38 ns/op	 295.51 MB/s	       0 B/op	       0 allocs/op
BenchmarkEventBus/subscribe-invalid-no-listener-4      300000000	         4.20 ns/op	 238.03 MB/s	       0 B/op	       0 allocs/op
BenchmarkEventBus/publish-no-subscriber-4                3000000	       468 ns/op	   2.13 MB/s	       0 B/op	       0 allocs/op
BenchmarkEventBus/publish-with-subscriber-4              3000000	       476 ns/op	   2.10 MB/s	       0 B/op	       0 allocs/op
BenchmarkEventBus/pub-sub-4                               100000	     79957 ns/op	   0.01 MB/s	      48 B/op	       0 allocs/op
PASS

Special thanks to http://ernestmicklei.com/2014/11/guava-like-eventbus-for-go/ for it's original approach

License

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  • Uses GPL license described below

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventListener

type EventListener func(EventMessage)

EventListener is the signature of functions that can handle an EventMessage.

type EventMessage

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

EventMessage is send on the bus to all subscribed listeners

func NewEventMessage

func NewEventMessage(id string, data map[string]interface{}) EventMessage

func (*EventMessage) Get

func (m *EventMessage) Get(key string) interface{}

type EventPayload

type EventPayload map[string]interface{}

EventPayload represents the context of an event, a simple key-value map.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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