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

247 lines
4.6 KiB
Go
Raw Normal View History

2019-06-19 15:55:25 +00:00
package eventbus
2019-06-13 02:23:03 +00:00
import (
"errors"
"fmt"
"reflect"
"sync"
2019-06-13 06:51:54 +00:00
"sync/atomic"
2019-06-19 15:09:42 +00:00
"github.com/libp2p/go-libp2p-core/event"
2019-06-13 02:23:03 +00:00
)
///////////////////////
// BUS
2019-06-19 13:36:31 +00:00
// basicBus is a type-based event delivery system
2019-06-19 13:00:22 +00:00
type basicBus 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
}
2019-06-19 15:09:42 +00:00
var _ event.Bus = (*basicBus)(nil)
2019-06-21 16:50:36 +00:00
type emitter struct {
2019-06-19 15:09:42 +00:00
n *node
typ reflect.Type
closed int32
dropper func(reflect.Type)
}
2019-06-21 16:50:36 +00:00
func (e *emitter) Emit(evt interface{}) {
2019-06-19 15:09:42 +00:00
if atomic.LoadInt32(&e.closed) != 0 {
panic("emitter is closed")
}
e.n.emit(evt)
}
2019-06-21 16:50:36 +00:00
func (e *emitter) Close() error {
2019-06-19 15:09:42 +00:00
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 {
2019-06-19 13:00:22 +00:00
return &basicBus{
2019-06-19 11:51:25 +00:00
nodes: map[reflect.Type]*node{},
2019-06-13 02:23:03 +00:00
}
}
2019-06-19 13:00:22 +00:00
func (b *basicBus) 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
}
2019-06-19 13:00:22 +00:00
func (b *basicBus) 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-21 16:50:36 +00:00
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)
2019-06-19 13:36:31 +00:00
// 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
2019-06-21 16:50:36 +00:00
func (b *basicBus) Subscribe(evtTypes interface{}, opts ...event.SubscriptionOpt) (_ event.Subscription, err error) {
2019-06-19 12:22:10 +00:00
var settings subSettings
2019-06-16 16:15:35 +00:00
for _, opt := range opts {
if err := opt(&settings); err != nil {
return nil, err
}
}
2019-06-21 16:50:36 +00:00
types, ok := evtTypes.([]interface{})
if !ok {
types = []interface{}{evtTypes}
}
2019-06-21 16:50:36 +00:00
out := &sub{
ch: make(chan interface{}, settings.buffer),
nodes: make([]*node, len(types)),
dropper: b.tryDropNode,
}
2019-06-21 16:50:36 +00:00
for i, etyp := range types {
typ := reflect.TypeOf(etyp)
if typ.Kind() != reflect.Ptr {
return nil, errors.New("subscribe called with non-pointer type")
2019-06-16 16:15:35 +00:00
}
2019-06-21 16:50:36 +00:00
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
}
2019-06-21 16:50:36 +00:00
out.ch <- l
}
2019-06-21 16:50:36 +00:00
})
}
2019-06-16 19:42:47 +00:00
2019-06-21 16:50:36 +00:00
return out, nil
2019-06-13 02:23:03 +00:00
}
2019-06-19 13:36:31 +00:00
// 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{})
2019-06-19 15:12:29 +00:00
func (b *basicBus) Emitter(evtType interface{}, opts ...event.EmitterOpt) (e event.Emitter, err error) {
2019-06-19 12:22:10 +00:00
var settings emitterSettings
2019-06-16 19:42:47 +00:00
for _, opt := range opts {
2019-06-19 15:09:42 +00:00
if err := opt(&settings); err != nil {
return nil, err
}
2019-06-16 19:42:47 +00:00
}
typ := reflect.TypeOf(evtType)
if typ.Kind() != reflect.Ptr {
2019-06-19 12:27:37 +00:00
return 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)
2019-06-16 19:42:47 +00:00
n.keepLast = n.keepLast || settings.makeStateful
2019-06-21 16:50:36 +00:00
e = &emitter{n: n, typ: typ, dropper: b.tryDropNode}
2019-06-16 19:42:47 +00:00
}, func(_ *node) {})
2019-06-13 02:23:03 +00:00
return
}
///////////////////////
// NODE
type node struct {
2019-06-19 13:00:22 +00:00
// Note: make sure to NEVER lock basicBus.lk when this lock is held
2019-06-13 02:23:03 +00:00
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
2019-06-21 16:50:36 +00:00
sinks []chan interface{}
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 {
2019-06-21 16:50:36 +00:00
n.last.Store(event)
2019-06-16 19:42:47 +00:00
}
2019-06-13 02:23:03 +00:00
for _, ch := range n.sinks {
2019-06-21 16:50:36 +00:00
ch <- event
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
}