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

201 lines
3.5 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 {
2019-06-16 17:06:49 +00:00
lk sync.Mutex
2019-06-19 11:51:25 +00:00
nodes map[reflect.Type]*node
2019-06-13 02:23:03 +00:00
}
func NewBus() Bus {
return &bus{
2019-06-19 11:51:25 +00:00
nodes: map[reflect.Type]*node{},
2019-06-13 02:23:03 +00:00
}
}
2019-06-16 19:42:47 +00:00
func (b *bus) withNode(typ reflect.Type, cb func(*node), async func(*node)) error {
2019-06-13 02:23:03 +00:00
b.lk.Lock()
2019-06-19 11:51:25 +00:00
n, ok := b.nodes[typ]
2019-06-13 02:23:03 +00:00
if !ok {
n = newNode(typ)
2019-06-19 11:51:25 +00:00
b.nodes[typ] = n
2019-06-13 02:23:03 +00:00
}
n.lk.Lock()
b.lk.Unlock()
2019-06-16 19:42:47 +00:00
2019-06-13 02:23:03 +00:00
cb(n)
2019-06-19 10:31:36 +00:00
go func() {
defer n.lk.Unlock()
async(n)
}()
2019-06-16 19:42:47 +00:00
2019-06-13 02:23:03 +00:00
return nil
}
func (b *bus) tryDropNode(typ reflect.Type) {
2019-06-13 06:51:54 +00:00
b.lk.Lock()
2019-06-19 11:51:25 +00:00
n, ok := b.nodes[typ]
2019-06-13 06:51:54 +00:00
if !ok { // already dropped
b.lk.Unlock()
return
}
n.lk.Lock()
2019-06-19 10:13:18 +00:00
if atomic.LoadInt32(&n.nEmitters) > 0 || len(n.sinks) > 0 {
2019-06-13 06:51:54 +00:00
n.lk.Unlock()
b.lk.Unlock()
return // still in use
}
n.lk.Unlock()
2019-06-19 11:51:25 +00:00
delete(b.nodes, typ)
2019-06-13 06:51:54 +00:00
b.lk.Unlock()
}
2019-06-16 16:15:35 +00:00
func (b *bus) Subscribe(typedChan interface{}, opts ...SubOption) (c CancelFunc, 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")
}
2019-06-16 17:06:49 +00:00
if typ.ChanDir()&reflect.SendDir == 0 {
return nil, errors.New("channel doesn't allow send")
}
2019-06-16 16:15:35 +00:00
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)
}
typ = settings.forcedType
}
err = b.withNode(typ.Elem(), func(n *node) {
2019-06-16 19:42:47 +00:00
n.sinks = append(n.sinks, refCh)
2019-06-13 06:51:54 +00:00
c = func() {
n.lk.Lock()
for i := 0; i < len(n.sinks); i++ {
if n.sinks[i] == refCh {
n.sinks[i] = n.sinks[len(n.sinks)-1]
n.sinks = n.sinks[:len(n.sinks)-1]
break
}
}
2019-06-19 10:13:18 +00:00
tryDrop := len(n.sinks) == 0 && atomic.LoadInt32(&n.nEmitters) == 0
2019-06-13 06:51:54 +00:00
n.lk.Unlock()
if tryDrop {
b.tryDropNode(typ.Elem())
2019-06-13 06:51:54 +00:00
}
}
2019-06-16 19:42:47 +00:00
}, func(n *node) {
if n.keepLast {
lastVal, ok := n.last.Load().(reflect.Value)
if !ok {
return
}
refCh.Send(lastVal)
}
2019-06-13 02:23:03 +00:00
})
return
}
2019-06-16 19:42:47 +00:00
func (b *bus) Emitter(evtType interface{}, opts ...EmitterOption) (e EmitFunc, c CancelFunc, err error) {
var settings EmitterSettings
for _, opt := range opts {
opt(&settings)
}
typ := reflect.TypeOf(evtType)
if typ.Kind() != reflect.Ptr {
return nil, nil, errors.New("emitter called with non-pointer type")
}
typ = typ.Elem()
err = b.withNode(typ, func(n *node) {
2019-06-13 06:51:54 +00:00
atomic.AddInt32(&n.nEmitters, 1)
closed := false
2019-06-16 19:42:47 +00:00
n.keepLast = n.keepLast || settings.makeStateful
2019-06-13 06:51:54 +00:00
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)
2019-06-13 06:51:54 +00:00
}
2019-06-13 02:23:03 +00:00
}
2019-06-16 19:42:47 +00:00
}, func(_ *node) {})
2019-06-13 02:23:03 +00:00
return
}
///////////////////////
// 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
2019-06-16 17:06:49 +00:00
keepLast bool
2019-06-16 19:42:47 +00:00
last atomic.Value
sinks []reflect.Value
2019-06-13 02:23:03 +00:00
}
func newNode(typ reflect.Type) *node {
return &node{
typ: typ,
}
}
2019-06-13 06:51:54 +00:00
func (n *node) emit(event interface{}) {
eval := reflect.ValueOf(event)
if eval.Type() != n.typ {
panic(fmt.Sprintf("Emit called with wrong type. expected: %s, got: %s", n.typ, eval.Type()))
2019-06-13 02:23:03 +00:00
}
n.lk.RLock()
2019-06-16 19:42:47 +00:00
if n.keepLast {
n.last.Store(eval)
}
2019-06-16 16:15:35 +00:00
// TODO: try using reflect.Select
2019-06-13 02:23:03 +00:00
for _, ch := range n.sinks {
ch.Send(eval)
2019-06-13 02:23:03 +00:00
}
2019-06-13 06:51:54 +00:00
n.lk.RUnlock()
2019-06-13 02:23:03 +00:00
}
///////////////////////
// UTILS
var _ Bus = &bus{}