mirror of
https://github.com/libp2p/go-eventbus.git
synced 2026-08-19 14:53:27 +08:00
Compare commits
26 Commits
feat/array
...
v0.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6212a929bf | ||
|
|
f100eac4ef | ||
|
|
c7aefba960 | ||
|
|
fe6d9d1489 | ||
|
|
131418619d | ||
|
|
015ba825e9 | ||
|
|
8b7b645962 | ||
|
|
bd8289f870 | ||
|
|
1ab671b0ca | ||
|
|
a1807fd415 | ||
|
|
2341c42dab | ||
|
|
2ea3b26fbd | ||
|
|
04b7ec33de | ||
|
|
401bb25f47 | ||
|
|
2f028f9607 | ||
|
|
942c134291 | ||
|
|
d23aaa9b5c | ||
|
|
1cb839f3b0 | ||
|
|
3abafaf475 | ||
|
|
71ffb0ebf1 | ||
|
|
8b50ba1149 | ||
|
|
c54e8ebbe9 | ||
|
|
5b845983c2 | ||
|
|
287e2189af | ||
|
|
821aef1f4b | ||
|
|
1c855d2c2d |
30
.travis.yml
Normal file
30
.travis.yml
Normal file
@@ -0,0 +1,30 @@
|
||||
os:
|
||||
- linux
|
||||
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.12.x
|
||||
|
||||
env:
|
||||
global:
|
||||
- GOTFLAGS="-race"
|
||||
matrix:
|
||||
- BUILD_DEPTYPE=gomod
|
||||
|
||||
|
||||
# disable travis install
|
||||
install:
|
||||
- true
|
||||
|
||||
script:
|
||||
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
|
||||
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $GOPATH/pkg/mod
|
||||
- $HOME/.cache/go-build
|
||||
|
||||
notifications:
|
||||
email: false
|
||||
@@ -5,10 +5,10 @@
|
||||
[](http://webchat.freenode.net/?channels=%23libp2p)
|
||||
[](https://godoc.org/github.com/libp2p/go-eventbus)
|
||||
[](https://coveralls.io/github/libp2p/go-eventbus?branch=master)
|
||||
[](https://travis-ci.org/libp2p/go-eventbus)
|
||||
[](https://travis-ci.com/libp2p/go-eventbus)
|
||||
[](https://discuss.libp2p.io)
|
||||
|
||||
> Simple and fast Go event bus
|
||||
> Simple and fast eventbus for type-based local event delivery.
|
||||
|
||||
## Install
|
||||
|
||||
|
||||
238
basic.go
238
basic.go
@@ -1,4 +1,4 @@
|
||||
package event
|
||||
package eventbus
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -6,128 +6,201 @@ import (
|
||||
"reflect"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/libp2p/go-libp2p-core/event"
|
||||
)
|
||||
|
||||
///////////////////////
|
||||
// BUS
|
||||
|
||||
type bus struct {
|
||||
lk sync.Mutex
|
||||
nodes map[string]*node
|
||||
// basicBus is a type-based event delivery system
|
||||
type basicBus struct {
|
||||
lk sync.Mutex
|
||||
nodes map[reflect.Type]*node
|
||||
}
|
||||
|
||||
func NewBus() Bus {
|
||||
return &bus{
|
||||
nodes: map[string]*node{},
|
||||
var _ event.Bus = (*basicBus)(nil)
|
||||
|
||||
type emitter struct {
|
||||
n *node
|
||||
typ reflect.Type
|
||||
closed int32
|
||||
dropper func(reflect.Type)
|
||||
}
|
||||
|
||||
func (e *emitter) Emit(evt interface{}) {
|
||||
if atomic.LoadInt32(&e.closed) != 0 {
|
||||
panic("emitter is closed")
|
||||
}
|
||||
e.n.emit(evt)
|
||||
}
|
||||
|
||||
func (e *emitter) Close() error {
|
||||
if !atomic.CompareAndSwapInt32(&e.closed, 0, 1) {
|
||||
panic("closed an emitter more than once")
|
||||
}
|
||||
if atomic.AddInt32(&e.n.nEmitters, -1) == 0 {
|
||||
e.dropper(e.typ)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewBus() event.Bus {
|
||||
return &basicBus{
|
||||
nodes: map[reflect.Type]*node{},
|
||||
}
|
||||
}
|
||||
|
||||
func (b *bus) withNode(typ reflect.Type, cb func(*node)) error {
|
||||
path := typePath(typ)
|
||||
|
||||
func (b *basicBus) withNode(typ reflect.Type, cb func(*node), async func(*node)) error {
|
||||
b.lk.Lock()
|
||||
|
||||
n, ok := b.nodes[path]
|
||||
n, ok := b.nodes[typ]
|
||||
if !ok {
|
||||
n = newNode(typ)
|
||||
b.nodes[path] = n
|
||||
b.nodes[typ] = n
|
||||
}
|
||||
|
||||
n.lk.Lock()
|
||||
b.lk.Unlock()
|
||||
defer n.lk.Unlock()
|
||||
|
||||
cb(n)
|
||||
|
||||
go func() {
|
||||
defer n.lk.Unlock()
|
||||
async(n)
|
||||
}()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *bus) tryDropNode(typ reflect.Type) {
|
||||
path := typePath(typ)
|
||||
|
||||
func (b *basicBus) tryDropNode(typ reflect.Type) {
|
||||
b.lk.Lock()
|
||||
n, ok := b.nodes[path]
|
||||
n, ok := b.nodes[typ]
|
||||
if !ok { // already dropped
|
||||
b.lk.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
n.lk.Lock()
|
||||
if n.nEmitters > 0 || len(n.sinks) > 0 {
|
||||
if atomic.LoadInt32(&n.nEmitters) > 0 || len(n.sinks) > 0 {
|
||||
n.lk.Unlock()
|
||||
b.lk.Unlock()
|
||||
return // still in use
|
||||
}
|
||||
n.lk.Unlock()
|
||||
|
||||
delete(b.nodes, path)
|
||||
delete(b.nodes, typ)
|
||||
b.lk.Unlock()
|
||||
}
|
||||
|
||||
func (b *bus) Subscribe(typedChan interface{}, opts ...SubOption) (c CancelFunc, err error) {
|
||||
var settings SubSettings
|
||||
type sub struct {
|
||||
ch chan interface{}
|
||||
nodes []*node
|
||||
dropper func(reflect.Type)
|
||||
}
|
||||
|
||||
func (s *sub) Out() <-chan interface{} {
|
||||
return s.ch
|
||||
}
|
||||
|
||||
func (s *sub) Close() error {
|
||||
close(s.ch)
|
||||
for _, n := range s.nodes {
|
||||
n.lk.Lock()
|
||||
for i := 0; i < len(n.sinks); i++ {
|
||||
if n.sinks[i] == s.ch {
|
||||
n.sinks[i], n.sinks[len(n.sinks)-1] = n.sinks[len(n.sinks)-1], nil
|
||||
n.sinks = n.sinks[:len(n.sinks)-1]
|
||||
break
|
||||
}
|
||||
}
|
||||
tryDrop := len(n.sinks) == 0 && atomic.LoadInt32(&n.nEmitters) == 0
|
||||
n.lk.Unlock()
|
||||
if tryDrop {
|
||||
s.dropper(n.typ)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ event.Subscription = (*sub)(nil)
|
||||
|
||||
// Subscribe creates new subscription. Failing to drain the channel will cause
|
||||
// publishers to get blocked. CancelFunc is guaranteed to return after last send
|
||||
// to the channel
|
||||
func (b *basicBus) Subscribe(evtTypes interface{}, opts ...event.SubscriptionOpt) (_ event.Subscription, err error) {
|
||||
var settings subSettings
|
||||
for _, opt := range opts {
|
||||
if err := opt(&settings); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
refCh := reflect.ValueOf(typedChan)
|
||||
typ := refCh.Type()
|
||||
if typ.Kind() != reflect.Chan {
|
||||
return nil, errors.New("expected a channel")
|
||||
}
|
||||
if typ.ChanDir() & reflect.SendDir == 0 {
|
||||
return nil, errors.New("channel doesn't allow send")
|
||||
types, ok := evtTypes.([]interface{})
|
||||
if !ok {
|
||||
types = []interface{}{evtTypes}
|
||||
}
|
||||
|
||||
if settings.forcedType != nil {
|
||||
if settings.forcedType.Elem().AssignableTo(typ) {
|
||||
return nil, fmt.Errorf("forced type %s cannot be sent to chan %s", settings.forcedType, typ)
|
||||
out := &sub{
|
||||
ch: make(chan interface{}, settings.buffer),
|
||||
nodes: make([]*node, len(types)),
|
||||
|
||||
dropper: b.tryDropNode,
|
||||
}
|
||||
|
||||
for i, etyp := range types {
|
||||
typ := reflect.TypeOf(etyp)
|
||||
|
||||
if typ.Kind() != reflect.Ptr {
|
||||
return nil, errors.New("subscribe called with non-pointer type")
|
||||
}
|
||||
typ = settings.forcedType
|
||||
}
|
||||
|
||||
err = b.withNode(typ.Elem(), func(n *node) {
|
||||
// when all subs are waiting on this channel, setting this to 1 doesn't
|
||||
// really affect benchmarks
|
||||
i := n.sub(refCh)
|
||||
c = func() {
|
||||
n.lk.Lock()
|
||||
delete(n.sinks, i)
|
||||
tryDrop := len(n.sinks) == 0 && n.nEmitters == 0
|
||||
n.lk.Unlock()
|
||||
if tryDrop {
|
||||
b.tryDropNode(typ.Elem())
|
||||
err = b.withNode(typ.Elem(), func(n *node) {
|
||||
n.sinks = append(n.sinks, out.ch)
|
||||
out.nodes[i] = n
|
||||
}, func(n *node) {
|
||||
if n.keepLast {
|
||||
l := n.last.Load()
|
||||
if l == nil {
|
||||
return
|
||||
}
|
||||
out.ch <- l
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
})
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (b *bus) Emitter(evtType interface{}, _ ...EmitterOption) (e EmitFunc, c CancelFunc, err error) {
|
||||
// Emitter creates new emitter
|
||||
//
|
||||
// eventType accepts typed nil pointers, and uses the type information to
|
||||
// select output type
|
||||
//
|
||||
// Example:
|
||||
// emit, err := eventbus.Emitter(new(EventT))
|
||||
// defer emit.Close() // MUST call this after being done with the emitter
|
||||
//
|
||||
// emit(EventT{})
|
||||
func (b *basicBus) Emitter(evtType interface{}, opts ...event.EmitterOpt) (e event.Emitter, err error) {
|
||||
var settings emitterSettings
|
||||
for _, opt := range opts {
|
||||
if err := opt(&settings); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
typ := reflect.TypeOf(evtType)
|
||||
if typ.Kind() != reflect.Ptr {
|
||||
return nil, nil, errors.New("emitter called with non-pointer type")
|
||||
return nil, errors.New("emitter called with non-pointer type")
|
||||
}
|
||||
typ = typ.Elem()
|
||||
|
||||
err = b.withNode(typ, func(n *node) {
|
||||
atomic.AddInt32(&n.nEmitters, 1)
|
||||
closed := false
|
||||
|
||||
e = func(event interface{}) {
|
||||
if closed {
|
||||
panic("emitter is closed")
|
||||
}
|
||||
n.emit(event)
|
||||
}
|
||||
|
||||
c = func() {
|
||||
closed = true
|
||||
if atomic.AddInt32(&n.nEmitters, -1) == 0 {
|
||||
b.tryDropNode(typ)
|
||||
}
|
||||
}
|
||||
})
|
||||
n.keepLast = n.keepLast || settings.makeStateful
|
||||
e = &emitter{n: n, typ: typ, dropper: b.tryDropNode}
|
||||
}, func(_ *node) {})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -135,7 +208,7 @@ func (b *bus) Emitter(evtType interface{}, _ ...EmitterOption) (e EmitFunc, c Ca
|
||||
// NODE
|
||||
|
||||
type node struct {
|
||||
// Note: make sure to NEVER lock bus.lk when this lock is held
|
||||
// Note: make sure to NEVER lock basicBus.lk when this lock is held
|
||||
lk sync.RWMutex
|
||||
|
||||
typ reflect.Type
|
||||
@@ -143,29 +216,18 @@ type node struct {
|
||||
// emitter ref count
|
||||
nEmitters int32
|
||||
|
||||
// sink index counter
|
||||
sinkC int
|
||||
keepLast bool
|
||||
last atomic.Value
|
||||
|
||||
// TODO: we could make emit a bit faster by making this into an array, but
|
||||
// it doesn't seem needed for now
|
||||
sinks map[int]reflect.Value
|
||||
sinks []chan interface{}
|
||||
}
|
||||
|
||||
func newNode(typ reflect.Type) *node {
|
||||
return &node{
|
||||
typ: typ,
|
||||
|
||||
sinks: map[int]reflect.Value{},
|
||||
}
|
||||
}
|
||||
|
||||
func (n *node) sub(outChan reflect.Value) int {
|
||||
i := n.sinkC
|
||||
n.sinkC++
|
||||
n.sinks[i] = outChan
|
||||
return i
|
||||
}
|
||||
|
||||
func (n *node) emit(event interface{}) {
|
||||
eval := reflect.ValueOf(event)
|
||||
if eval.Type() != n.typ {
|
||||
@@ -173,18 +235,12 @@ func (n *node) emit(event interface{}) {
|
||||
}
|
||||
|
||||
n.lk.RLock()
|
||||
// TODO: try using reflect.Select
|
||||
if n.keepLast {
|
||||
n.last.Store(event)
|
||||
}
|
||||
|
||||
for _, ch := range n.sinks {
|
||||
ch.Send(eval)
|
||||
ch <- event
|
||||
}
|
||||
n.lk.RUnlock()
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
// UTILS
|
||||
|
||||
func typePath(t reflect.Type) string {
|
||||
return t.PkgPath() + "/" + t.String()
|
||||
}
|
||||
|
||||
var _ Bus = &bus{}
|
||||
|
||||
223
basic_test.go
223
basic_test.go
@@ -1,4 +1,4 @@
|
||||
package event
|
||||
package eventbus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -6,41 +6,49 @@ import (
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/libp2p/go-libp2p-testing/race"
|
||||
)
|
||||
|
||||
type EventA struct{}
|
||||
type EventB int
|
||||
|
||||
func getN() int {
|
||||
n := 50000
|
||||
if race.WithRace() {
|
||||
n = 1000
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (EventA) String() string {
|
||||
return "Oh, Hello"
|
||||
}
|
||||
|
||||
func TestEmit(t *testing.T) {
|
||||
bus := NewBus()
|
||||
events := make(chan EventA)
|
||||
cancel, err := bus.Subscribe(events)
|
||||
sub, err := bus.Subscribe(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer cancel()
|
||||
<-events
|
||||
defer sub.Close()
|
||||
<-sub.Out()
|
||||
}()
|
||||
|
||||
emit, cancel, err := bus.Emitter(new(EventA))
|
||||
em, err := bus.Emitter(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cancel()
|
||||
defer em.Close()
|
||||
|
||||
emit(EventA{})
|
||||
em.Emit(EventA{})
|
||||
}
|
||||
|
||||
func TestSub(t *testing.T) {
|
||||
bus := NewBus()
|
||||
events := make(chan EventB)
|
||||
cancel, err := bus.Subscribe(events)
|
||||
sub, err := bus.Subscribe(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -51,18 +59,18 @@ func TestSub(t *testing.T) {
|
||||
wait.Add(1)
|
||||
|
||||
go func() {
|
||||
defer cancel()
|
||||
event = <-events
|
||||
defer sub.Close()
|
||||
event = (<-sub.Out()).(EventB)
|
||||
wait.Done()
|
||||
}()
|
||||
|
||||
emit, cancel, err := bus.Emitter(new(EventB))
|
||||
em, err := bus.Emitter(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cancel()
|
||||
defer em.Close()
|
||||
|
||||
emit(EventB(7))
|
||||
em.Emit(EventB(7))
|
||||
wait.Wait()
|
||||
|
||||
if event != 7 {
|
||||
@@ -73,23 +81,23 @@ func TestSub(t *testing.T) {
|
||||
func TestEmitNoSubNoBlock(t *testing.T) {
|
||||
bus := NewBus()
|
||||
|
||||
emit, cancel, err := bus.Emitter(new(EventA))
|
||||
em, err := bus.Emitter(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cancel()
|
||||
defer em.Close()
|
||||
|
||||
emit(EventA{})
|
||||
em.Emit(EventA{})
|
||||
}
|
||||
|
||||
func TestEmitOnClosed(t *testing.T) {
|
||||
bus := NewBus()
|
||||
|
||||
emit, cancel, err := bus.Emitter(new(EventA))
|
||||
em, err := bus.Emitter(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cancel()
|
||||
em.Close()
|
||||
|
||||
defer func() {
|
||||
r := recover()
|
||||
@@ -101,12 +109,12 @@ func TestEmitOnClosed(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
emit(EventA{})
|
||||
em.Emit(EventA{})
|
||||
}
|
||||
|
||||
func TestClosingRaces(t *testing.T) {
|
||||
subs := 50000
|
||||
emits := 50000
|
||||
subs := getN()
|
||||
emits := getN()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var lk sync.RWMutex
|
||||
@@ -121,9 +129,9 @@ func TestClosingRaces(t *testing.T) {
|
||||
lk.RLock()
|
||||
defer lk.RUnlock()
|
||||
|
||||
cancel, _ := b.Subscribe(make(chan EventA))
|
||||
sub, _ := b.Subscribe(new(EventA))
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
cancel()
|
||||
sub.Close()
|
||||
|
||||
wg.Done()
|
||||
}()
|
||||
@@ -133,9 +141,9 @@ func TestClosingRaces(t *testing.T) {
|
||||
lk.RLock()
|
||||
defer lk.RUnlock()
|
||||
|
||||
_, cancel, _ := b.Emitter(new(EventA))
|
||||
emit, _ := b.Emitter(new(EventA))
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
cancel()
|
||||
emit.Close()
|
||||
|
||||
wg.Done()
|
||||
}()
|
||||
@@ -146,7 +154,7 @@ func TestClosingRaces(t *testing.T) {
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if len(b.(*bus).nodes) != 0 {
|
||||
if len(b.(*basicBus).nodes) != 0 {
|
||||
t.Error("expected no nodes")
|
||||
}
|
||||
}
|
||||
@@ -156,7 +164,7 @@ func TestSubMany(t *testing.T) {
|
||||
|
||||
var r int32
|
||||
|
||||
n := 50000
|
||||
n := getN()
|
||||
var wait sync.WaitGroup
|
||||
var ready sync.WaitGroup
|
||||
wait.Add(n)
|
||||
@@ -164,39 +172,37 @@ func TestSubMany(t *testing.T) {
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
go func() {
|
||||
events := make(chan EventB)
|
||||
cancel, err := bus.Subscribe(events)
|
||||
sub, err := bus.Subscribe(new(EventB))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer cancel()
|
||||
defer sub.Close()
|
||||
|
||||
ready.Done()
|
||||
atomic.AddInt32(&r, int32(<-events))
|
||||
atomic.AddInt32(&r, int32((<-sub.Out()).(EventB)))
|
||||
wait.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
emit, cancel, err := bus.Emitter(new(EventB))
|
||||
em, err := bus.Emitter(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cancel()
|
||||
defer em.Close()
|
||||
|
||||
ready.Wait()
|
||||
|
||||
emit(EventB(7))
|
||||
em.Emit(EventB(7))
|
||||
wait.Wait()
|
||||
|
||||
if int(r) != 7 * n {
|
||||
if int(r) != 7*n {
|
||||
t.Error("got wrong result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubType(t *testing.T) {
|
||||
bus := NewBus()
|
||||
events := make(chan fmt.Stringer)
|
||||
cancel, err := bus.Subscribe(events, ForceSubType(new(EventA)))
|
||||
sub, err := bus.Subscribe([]interface{}{new(EventA), new(EventB)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -207,18 +213,18 @@ func TestSubType(t *testing.T) {
|
||||
wait.Add(1)
|
||||
|
||||
go func() {
|
||||
defer cancel()
|
||||
event = <-events
|
||||
defer sub.Close()
|
||||
event = (<-sub.Out()).(EventA)
|
||||
wait.Done()
|
||||
}()
|
||||
|
||||
emit, cancel, err := bus.Emitter(new(EventA))
|
||||
em, err := bus.Emitter(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cancel()
|
||||
defer em.Close()
|
||||
|
||||
emit(EventA{})
|
||||
em.Emit(EventA{})
|
||||
wait.Wait()
|
||||
|
||||
if event.String() != "Oh, Hello" {
|
||||
@@ -226,7 +232,76 @@ func TestSubType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func testMany(t testing.TB, subs, emits, msgs int) {
|
||||
func TestNonStateful(t *testing.T) {
|
||||
bus := NewBus()
|
||||
em, err := bus.Emitter(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer em.Close()
|
||||
|
||||
sub1, err := bus.Subscribe(new(EventB), BufSize(1))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer sub1.Close()
|
||||
|
||||
select {
|
||||
case <-sub1.Out():
|
||||
t.Fatal("didn't expect to get an event")
|
||||
default:
|
||||
}
|
||||
|
||||
em.Emit(EventB(1))
|
||||
|
||||
select {
|
||||
case e := <-sub1.Out():
|
||||
if e.(EventB) != 1 {
|
||||
t.Fatal("got wrong event")
|
||||
}
|
||||
default:
|
||||
t.Fatal("expected to get an event")
|
||||
}
|
||||
|
||||
sub2, err := bus.Subscribe(new(EventB), BufSize(1))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer sub2.Close()
|
||||
|
||||
select {
|
||||
case <-sub2.Out():
|
||||
t.Fatal("didn't expect to get an event")
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func TestStateful(t *testing.T) {
|
||||
bus := NewBus()
|
||||
em, err := bus.Emitter(new(EventB), Stateful)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer em.Close()
|
||||
|
||||
em.Emit(EventB(2))
|
||||
|
||||
sub, err := bus.Subscribe(new(EventB), BufSize(1))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer sub.Close()
|
||||
|
||||
if (<-sub.Out()).(EventB) != 2 {
|
||||
t.Fatal("got wrong event")
|
||||
}
|
||||
}
|
||||
|
||||
func testMany(t testing.TB, subs, emits, msgs int, stateful bool) {
|
||||
if race.WithRace() && subs+emits > 5000 {
|
||||
t.SkipNow()
|
||||
}
|
||||
|
||||
bus := NewBus()
|
||||
|
||||
var r int64
|
||||
@@ -238,16 +313,19 @@ func testMany(t testing.TB, subs, emits, msgs int) {
|
||||
|
||||
for i := 0; i < subs; i++ {
|
||||
go func() {
|
||||
events := make(chan EventB)
|
||||
cancel, err := bus.Subscribe(events)
|
||||
sub, err := bus.Subscribe(new(EventB))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer cancel()
|
||||
defer sub.Close()
|
||||
|
||||
ready.Done()
|
||||
for i := 0; i < emits * msgs; i++ {
|
||||
atomic.AddInt64(&r, int64(<-events))
|
||||
for i := 0; i < emits*msgs; i++ {
|
||||
e, ok := <-sub.Out()
|
||||
if !ok {
|
||||
panic("wat")
|
||||
}
|
||||
atomic.AddInt64(&r, int64(e.(EventB)))
|
||||
}
|
||||
wait.Done()
|
||||
}()
|
||||
@@ -255,16 +333,19 @@ func testMany(t testing.TB, subs, emits, msgs int) {
|
||||
|
||||
for i := 0; i < emits; i++ {
|
||||
go func() {
|
||||
emit, cancel, err := bus.Emitter(new(EventB))
|
||||
em, err := bus.Emitter(new(EventB), func(settings interface{}) error {
|
||||
settings.(*emitterSettings).makeStateful = stateful
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer cancel()
|
||||
defer em.Close()
|
||||
|
||||
ready.Wait()
|
||||
|
||||
for i := 0; i < msgs; i++ {
|
||||
emit(EventB(97))
|
||||
em.Emit(EventB(97))
|
||||
}
|
||||
|
||||
wait.Done()
|
||||
@@ -273,66 +354,78 @@ func testMany(t testing.TB, subs, emits, msgs int) {
|
||||
|
||||
wait.Wait()
|
||||
|
||||
if int(r) != 97 * subs * emits * msgs {
|
||||
if int(r) != 97*subs*emits*msgs {
|
||||
t.Fatal("got wrong result")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBothMany(t *testing.T) {
|
||||
testMany(t, 10000, 100, 10)
|
||||
testMany(t, 10000, 100, 10, false)
|
||||
}
|
||||
|
||||
func BenchmarkSubs(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, b.N, 100, 100)
|
||||
testMany(b, b.N, 100, 100, false)
|
||||
}
|
||||
|
||||
func BenchmarkEmits(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, 100, b.N, 100)
|
||||
testMany(b, 100, b.N, 100, false)
|
||||
}
|
||||
|
||||
func BenchmarkMsgs(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, 100, 100, b.N)
|
||||
testMany(b, 100, 100, b.N, false)
|
||||
}
|
||||
|
||||
func BenchmarkOneToMany(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, b.N, 1, 100)
|
||||
testMany(b, b.N, 1, 100, false)
|
||||
}
|
||||
|
||||
func BenchmarkManyToOne(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, 1, b.N, 100)
|
||||
testMany(b, 1, b.N, 100, false)
|
||||
}
|
||||
|
||||
func BenchmarkMs1e2m4(b *testing.B) {
|
||||
b.N = 1000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 10, 100, 10000)
|
||||
testMany(b, 10, 100, 10000, false)
|
||||
}
|
||||
|
||||
func BenchmarkMs1e0m6(b *testing.B) {
|
||||
b.N = 10000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 10, 1, 1000000)
|
||||
testMany(b, 10, 1, 1000000, false)
|
||||
}
|
||||
|
||||
func BenchmarkMs0e0m6(b *testing.B) {
|
||||
b.N = 1000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 1, 1, 1000000)
|
||||
testMany(b, 1, 1, 1000000, false)
|
||||
}
|
||||
|
||||
func BenchmarkStatefulMs1e0m6(b *testing.B) {
|
||||
b.N = 10000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 10, 1, 1000000, true)
|
||||
}
|
||||
|
||||
func BenchmarkStatefulMs0e0m6(b *testing.B) {
|
||||
b.N = 1000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 1, 1, 1000000, true)
|
||||
}
|
||||
|
||||
func BenchmarkMs0e6m0(b *testing.B) {
|
||||
b.N = 1000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 1, 1000000, 1)
|
||||
testMany(b, 1, 1000000, 1, false)
|
||||
}
|
||||
|
||||
func BenchmarkMs6e0m0(b *testing.B) {
|
||||
b.N = 1000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 1000000, 1, 1)
|
||||
testMany(b, 1000000, 1, 1, false)
|
||||
}
|
||||
|
||||
3
codecov.yml
Normal file
3
codecov.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
coverage:
|
||||
range: "50...100"
|
||||
comment: off
|
||||
5
go.mod
5
go.mod
@@ -1,3 +1,8 @@
|
||||
module github.com/libp2p/go-eventbus
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/libp2p/go-libp2p-core v0.0.6
|
||||
github.com/libp2p/go-libp2p-testing v0.0.4
|
||||
)
|
||||
|
||||
114
go.sum
Normal file
114
go.sum
Normal file
@@ -0,0 +1,114 @@
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
|
||||
github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32/go.mod h1:DrZx5ec/dmnfpw9KyYoQyYo7d0KEvTkk/5M/vbZjAr8=
|
||||
github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c h1:aEbSeNALREWXk0G7UdNhR3ayBV7tZ4M2PNmnrCAph6Q=
|
||||
github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI=
|
||||
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
|
||||
github.com/btcsuite/btcutil v0.0.0-20190207003914-4c204d697803/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
|
||||
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
|
||||
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
|
||||
github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY=
|
||||
github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
|
||||
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
|
||||
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
|
||||
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495 h1:6IyqGr3fnd0tM3YxipK27TUskaOVUjU2nG45yzwcQKY=
|
||||
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
|
||||
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/ipfs/go-cid v0.0.1/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
|
||||
github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
|
||||
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
|
||||
github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8/go.mod h1:Ly/wlsjFq/qrU3Rar62tu1gASgGw6chQbSh/XgIIXCY=
|
||||
github.com/jbenet/goprocess v0.1.3 h1:YKyIEECS/XvcfHtBzxtjBBbWK+MbvA6dG8ASiqwvr10=
|
||||
github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
|
||||
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
|
||||
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
|
||||
github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8=
|
||||
github.com/libp2p/go-libp2p-core v0.0.1/go.mod h1:g/VxnTZ/1ygHxH3dKok7Vno1VfpvGcGip57wjTU4fco=
|
||||
github.com/libp2p/go-libp2p-core v0.0.6 h1:SsYhfWJ47vLP1Rd9/0hqEm/W/PlFbC/3YLZyLCcvo1w=
|
||||
github.com/libp2p/go-libp2p-core v0.0.6/go.mod h1:0d9xmaYAVY5qmbp/fcgxHT3ZJsLjYeYPMJAUKpaCHrE=
|
||||
github.com/libp2p/go-libp2p-testing v0.0.4 h1:Qev57UR47GcLPXWjrunv5aLIQGO4n9mhI/8/EIrEEFc=
|
||||
github.com/libp2p/go-libp2p-testing v0.0.4/go.mod h1:gvchhf3FQOtBdr+eFUABet5a4MBLK8jM3V4Zghvmi+E=
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
|
||||
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
||||
github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
||||
github.com/minio/sha256-simd v0.1.0 h1:U41/2erhAKcmSI14xh/ZTUdBPOzDOIfS93ibzUSl8KM=
|
||||
github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
||||
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
||||
github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
||||
github.com/mr-tron/base58 v1.1.2 h1:ZEw4I2EgPKDJ2iEw0cNmLB3ROrEmkOtXIkaG7wZg+78=
|
||||
github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
|
||||
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
|
||||
github.com/multiformats/go-multiaddr v0.0.2/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44=
|
||||
github.com/multiformats/go-multiaddr v0.0.4 h1:WgMSI84/eRLdbptXMkMWDXPjPq7SPLIgGUVm2eroyU4=
|
||||
github.com/multiformats/go-multiaddr v0.0.4/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44=
|
||||
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
|
||||
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
|
||||
github.com/multiformats/go-multihash v0.0.5 h1:1wxmCvTXAifAepIMyF39vZinRw5sbqjPs/UIi93+uik=
|
||||
github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a h1:/eS3yfGjQKG+9kayBkj0ip1BGhq6zJ3eaVksphxAaek=
|
||||
github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0=
|
||||
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU=
|
||||
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc=
|
||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443 h1:IcSOAf4PyMp3U3XbIEj1/xJ2BjNN2jWv7JoyOsMxXUU=
|
||||
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
50
interface.go
50
interface.go
@@ -1,50 +0,0 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type SubSettings struct {
|
||||
forcedType reflect.Type
|
||||
}
|
||||
type SubOption func(*SubSettings) error
|
||||
|
||||
func ForceSubType(evtType interface{}) SubOption {
|
||||
return func(s *SubSettings) error {
|
||||
typ := reflect.TypeOf(evtType)
|
||||
if typ.Kind() != reflect.Ptr {
|
||||
return errors.New("ForceSubType called with non-pointer type")
|
||||
}
|
||||
s.forcedType = typ
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type EmitterSettings struct {}
|
||||
type EmitterOption func(*EmitterSettings)
|
||||
|
||||
type Bus interface {
|
||||
// Subscribe creates new subscription. Failing to drain the incoming channel
|
||||
// will cause publishers to get blocked
|
||||
//
|
||||
// evtTypes only accepts typed nil pointers, and uses the type information to
|
||||
// select output type
|
||||
//
|
||||
// Example:
|
||||
// sub, cancel, err := eventbus.Subscribe(new(os.Signal))
|
||||
// defer cancel()
|
||||
//
|
||||
// evt := (<-sub).(os.Signal) // guaranteed to be safe
|
||||
Subscribe(typedChan interface{}, opts ...SubOption) (CancelFunc, error)
|
||||
|
||||
Emitter(eventType interface{}, opts ...EmitterOption) (EmitFunc, CancelFunc, error)
|
||||
}
|
||||
|
||||
// EmitFunc emits events. If any channel subscribed to the topic is blocked,
|
||||
// calls to EmitFunc will block
|
||||
//
|
||||
// Calling this function with wrong event type will cause a panic
|
||||
type EmitFunc func(event interface{})
|
||||
|
||||
type CancelFunc func()
|
||||
28
opts.go
Normal file
28
opts.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package eventbus
|
||||
|
||||
type subSettings struct {
|
||||
buffer int
|
||||
}
|
||||
|
||||
func BufSize(n int) func(interface{}) error {
|
||||
return func(s interface{}) error {
|
||||
s.(*subSettings).buffer = n
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type emitterSettings struct {
|
||||
makeStateful bool
|
||||
}
|
||||
|
||||
// Stateful is an Emitter option which makes makes the eventbus channel
|
||||
// 'remember' last event sent, and when a new subscriber joins the
|
||||
// bus, the remembered event is immediately sent to the subscription
|
||||
// channel.
|
||||
//
|
||||
// This allows to provide state tracking for dynamic systems, and/or
|
||||
// allows new subscribers to verify that there are Emitters on the channel
|
||||
func Stateful(s interface{}) error {
|
||||
s.(*emitterSettings).makeStateful = true
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user