go-libp2p/p2p/host/eventbus/basic.go

193 lines
3.4 KiB
Go
Raw Normal View History

2019-06-13 02:23:03 +00:00
package event
import (
"errors"
"fmt"
"reflect"
"sync"
2019-06-13 06:51:54 +00:00
"sync/atomic"
2019-06-13 02:23:03 +00:00
)
///////////////////////
// BUS
type bus struct {
lk sync.Mutex
nodes map[string]*node
}
func NewBus() Bus {
return &bus{
nodes: map[string]*node{},
}
}
func (b *bus) withNode(evtType interface{}, cb func(*node)) error {
typ := reflect.TypeOf(evtType)
if typ.Kind() != reflect.Ptr {
return errors.New("subscribe called with non-pointer type")
}
typ = typ.Elem()
path := typePath(typ)
b.lk.Lock()
n, ok := b.nodes[path]
if !ok {
n = newNode(typ)
b.nodes[path] = n
}
n.lk.Lock()
b.lk.Unlock()
2019-06-13 20:25:53 +00:00
defer n.lk.Unlock()
2019-06-13 02:23:03 +00:00
cb(n)
return nil
}
2019-06-13 06:51:54 +00:00
func (b *bus) tryDropNode(evtType interface{}) {
path := typePath(reflect.TypeOf(evtType).Elem())
b.lk.Lock()
n, ok := b.nodes[path]
if !ok { // already dropped
b.lk.Unlock()
return
}
n.lk.Lock()
if n.nEmitters > 0 || len(n.sinks) > 0 {
n.lk.Unlock()
b.lk.Unlock()
return // still in use
}
n.lk.Unlock()
delete(b.nodes, path)
b.lk.Unlock()
}
func (b *bus) Subscribe(evtType interface{}, _ ...SubOption) (s <-chan interface{}, c CancelFunc, err error) {
2019-06-13 02:23:03 +00:00
err = b.withNode(evtType, func(n *node) {
2019-06-14 16:57:21 +00:00
// when all subs are waiting on this channel, setting this to 1 doesn't
// really affect benchmarks
2019-06-13 06:51:54 +00:00
out, i := n.sub(0)
s = out
c = func() {
n.lk.Lock()
delete(n.sinks, i)
close(out)
tryDrop := len(n.sinks) == 0 && n.nEmitters == 0
n.lk.Unlock()
if tryDrop {
b.tryDropNode(evtType)
}
}
2019-06-13 02:23:03 +00:00
})
return
}
func (b *bus) Emitter(evtType interface{}, _ ...EmitterOption) (e EmitFunc, c CancelFunc, err error) {
2019-06-13 02:23:03 +00:00
err = b.withNode(evtType, func(n *node) {
2019-06-13 06:51:54 +00:00
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(evtType)
}
2019-06-13 02:23:03 +00:00
}
})
return
}
2019-06-14 16:57:21 +00:00
func (b *bus) SendTo(typedChan interface{}) (CancelFunc, error) {
typ := reflect.TypeOf(typedChan)
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")
}
etype := reflect.New(typ.Elem())
sub, cf, err := b.Subscribe(etype.Interface())
if err != nil {
return nil, err
}
go func() {
tcv := reflect.ValueOf(typedChan)
for event := range sub {
tcv.Send(reflect.ValueOf(event))
}
}()
return cf, nil
}
2019-06-13 02:23:03 +00:00
///////////////////////
// NODE
type node struct {
// Note: make sure to NEVER lock bus.lk when this lock is held
lk sync.RWMutex
typ reflect.Type
2019-06-13 20:25:53 +00:00
// emitter ref count
2019-06-13 06:51:54 +00:00
nEmitters int32
2019-06-13 20:25:53 +00:00
// sink index counter
sinkC int
// TODO: we could make emit a bit faster by making this into an array, but
// it doesn't seem needed for now
2019-06-13 06:51:54 +00:00
sinks map[int]chan interface{}
2019-06-13 02:23:03 +00:00
}
func newNode(typ reflect.Type) *node {
return &node{
typ: typ,
sinks: map[int]chan interface{}{},
}
}
2019-06-13 06:51:54 +00:00
func (n *node) sub(buf int) (chan interface{}, int) {
2019-06-13 02:23:03 +00:00
out := make(chan interface{}, buf)
2019-06-13 20:25:53 +00:00
i := n.sinkC
n.sinkC++
2019-06-13 06:51:54 +00:00
n.sinks[i] = out
return out, i
2019-06-13 02:23:03 +00:00
}
2019-06-13 06:51:54 +00:00
func (n *node) emit(event interface{}) {
2019-06-13 02:23:03 +00:00
etype := reflect.TypeOf(event)
if etype != n.typ {
panic(fmt.Sprintf("Emit called with wrong type. expected: %s, got: %s", n.typ, etype))
}
n.lk.RLock()
for _, ch := range n.sinks {
ch <- event
}
2019-06-13 06:51:54 +00:00
n.lk.RUnlock()
2019-06-13 02:23:03 +00:00
}
///////////////////////
// UTILS
func typePath(t reflect.Type) string {
return t.PkgPath() + "/" + t.String()
}
var _ Bus = &bus{}