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

419 lines
8.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
)
// /////////////////////
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 {
lk sync.RWMutex
nodes map[reflect.Type]*node
wildcard *wildcardNode
metricsTracer MetricsTracer
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 {
n *node
w *wildcardNode
typ reflect.Type
closed atomic.Bool
dropper func(reflect.Type)
metricsTracer MetricsTracer
2019-06-19 15:09:42 +00:00
}
2019-07-30 19:51:57 +00:00
func (e *emitter) Emit(evt interface{}) error {
if e.closed.Load() {
2019-07-30 19:51:57 +00:00
return fmt.Errorf("emitter is closed")
2019-06-19 15:09:42 +00:00
}
2019-06-19 15:09:42 +00:00
e.n.emit(evt)
e.w.emit(evt)
if e.metricsTracer != nil {
e.metricsTracer.EventEmitted(e.typ)
}
2019-07-30 19:51:57 +00:00
return nil
2019-06-19 15:09:42 +00:00
}
2019-06-21 16:50:36 +00:00
func (e *emitter) Close() error {
if !e.closed.CompareAndSwap(false, true) {
return fmt.Errorf("closed an emitter more than once")
2019-06-19 15:09:42 +00:00
}
if atomic.AddInt32(&e.n.nEmitters, -1) == 0 {
e.dropper(e.typ)
}
return nil
}
func NewBus(opts ...Option) event.Bus {
bus := &basicBus{
nodes: map[reflect.Type]*node{},
wildcard: &wildcardNode{},
}
for _, opt := range opts {
opt(bus)
2019-06-13 02:23:03 +00:00
}
return bus
2019-06-13 02:23:03 +00:00
}
func (b *basicBus) withNode(typ reflect.Type, cb func(*node), async func(*node)) {
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, b.metricsTracer)
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
if async == nil {
n.lk.Unlock()
} else {
go func() {
defer n.lk.Unlock()
async(n)
}()
}
2019-06-13 02:23:03 +00:00
}
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()
}
type wildcardSub struct {
ch chan interface{}
w *wildcardNode
metricsTracer MetricsTracer
name string
}
func (w *wildcardSub) Out() <-chan interface{} {
return w.ch
}
func (w *wildcardSub) Close() error {
w.w.removeSink(w.ch)
if w.metricsTracer != nil {
w.metricsTracer.RemoveSubscriber(reflect.TypeOf(event.WildcardSubscription))
}
return nil
}
func (w *wildcardSub) Name() string {
return w.name
}
type namedSink struct {
name string
ch chan interface{}
}
2019-06-21 16:50:36 +00:00
type sub struct {
ch chan interface{}
nodes []*node
dropper func(reflect.Type)
metricsTracer MetricsTracer
name string
}
func (s *sub) Name() string {
return s.name
2019-06-21 16:50:36 +00:00
}
func (s *sub) Out() <-chan interface{} {
return s.ch
}
func (s *sub) Close() error {
2019-06-22 10:05:03 +00:00
go func() {
// drain the event channel, will return when closed and drained.
// this is necessary to unblock publishes to this channel.
for range s.ch {
2019-06-22 10:05:03 +00:00
}
}()
2019-06-21 16:50:36 +00:00
for _, n := range s.nodes {
n.lk.Lock()
2019-06-22 10:05:03 +00:00
2019-06-21 16:50:36 +00:00
for i := 0; i < len(n.sinks); i++ {
if n.sinks[i].ch == s.ch {
2019-06-21 16:50:36 +00:00
n.sinks[i], n.sinks[len(n.sinks)-1] = n.sinks[len(n.sinks)-1], nil
n.sinks = n.sinks[:len(n.sinks)-1]
if s.metricsTracer != nil {
s.metricsTracer.RemoveSubscriber(n.typ)
}
2019-06-21 16:50:36 +00:00
break
}
}
2019-06-22 10:05:03 +00:00
2019-06-21 16:50:36 +00:00
tryDrop := len(n.sinks) == 0 && atomic.LoadInt32(&n.nEmitters) == 0
2019-06-22 10:05:03 +00:00
2019-06-21 16:50:36 +00:00
n.lk.Unlock()
2019-06-22 10:05:03 +00:00
2019-06-21 16:50:36 +00:00
if tryDrop {
s.dropper(n.typ)
}
}
close(s.ch)
2019-06-21 16:50:36 +00:00
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) {
settings := newSubSettings()
2019-06-16 16:15:35 +00:00
for _, opt := range opts {
if err := opt(&settings); err != nil {
return nil, err
}
}
if evtTypes == event.WildcardSubscription {
out := &wildcardSub{
ch: make(chan interface{}, settings.buffer),
w: b.wildcard,
metricsTracer: b.metricsTracer,
name: settings.name,
}
b.wildcard.addSink(&namedSink{ch: out.ch, name: out.name})
return out, nil
}
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
if len(types) > 1 {
for _, t := range types {
if t == event.WildcardSubscription {
return nil, fmt.Errorf("wildcard subscriptions must be started separately")
}
}
}
2019-06-21 16:50:36 +00:00
out := &sub{
ch: make(chan interface{}, settings.buffer),
nodes: make([]*node, len(types)),
dropper: b.tryDropNode,
metricsTracer: b.metricsTracer,
name: settings.name,
}
2019-06-22 10:05:03 +00:00
for _, etyp := range types {
if reflect.TypeOf(etyp).Kind() != reflect.Ptr {
2019-06-21 16:50:36 +00:00
return nil, errors.New("subscribe called with non-pointer type")
2019-06-16 16:15:35 +00:00
}
2019-06-22 10:05:03 +00:00
}
for i, etyp := range types {
typ := reflect.TypeOf(etyp)
2019-06-21 16:50:36 +00:00
b.withNode(typ.Elem(), func(n *node) {
n.sinks = append(n.sinks, &namedSink{ch: out.ch, name: out.name})
2019-06-21 16:50:36 +00:00
out.nodes[i] = n
if b.metricsTracer != nil {
b.metricsTracer.AddSubscriber(typ.Elem())
}
2019-06-21 16:50:36 +00:00
}, func(n *node) {
if n.keepLast {
l := n.last
2019-06-21 16:50:36 +00:00
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) {
if evtType == event.WildcardSubscription {
return nil, fmt.Errorf("illegal emitter for wildcard subscription")
}
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()
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
e = &emitter{n: n, typ: typ, dropper: b.tryDropNode, w: b.wildcard, metricsTracer: b.metricsTracer}
}, nil)
2019-06-13 02:23:03 +00:00
return
}
// GetAllEventTypes returns all the event types that this bus has emitters
// or subscribers for.
func (b *basicBus) GetAllEventTypes() []reflect.Type {
b.lk.RLock()
defer b.lk.RUnlock()
types := make([]reflect.Type, 0, len(b.nodes))
for t := range b.nodes {
types = append(types, t)
}
return types
}
// /////////////////////
2019-06-13 02:23:03 +00:00
// NODE
type wildcardNode struct {
sync.RWMutex
nSinks int32
sinks []*namedSink
metricsTracer MetricsTracer
}
func (n *wildcardNode) addSink(sink *namedSink) {
atomic.AddInt32(&n.nSinks, 1) // ok to do outside the lock
n.Lock()
n.sinks = append(n.sinks, sink)
n.Unlock()
if n.metricsTracer != nil {
n.metricsTracer.AddSubscriber(reflect.TypeOf(event.WildcardSubscription))
}
}
func (n *wildcardNode) removeSink(ch chan interface{}) {
atomic.AddInt32(&n.nSinks, -1) // ok to do outside the lock
n.Lock()
for i := 0; i < len(n.sinks); i++ {
if n.sinks[i].ch == 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
}
}
n.Unlock()
}
func (n *wildcardNode) emit(evt interface{}) {
if atomic.LoadInt32(&n.nSinks) == 0 {
return
}
n.RLock()
for _, sink := range n.sinks {
// Sending metrics before sending on channel allows us to
// record channel full events before blocking
sendSubscriberMetrics(n.metricsTracer, sink)
sink.ch <- evt
}
n.RUnlock()
}
2019-06-13 02:23:03 +00:00
type node struct {
2019-06-19 13:00:22 +00:00
// Note: make sure to NEVER lock basicBus.lk when this lock is held
lk sync.Mutex
2019-06-13 02:23:03 +00:00
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
last interface{}
sinks []*namedSink
metricsTracer MetricsTracer
2019-06-13 02:23:03 +00:00
}
func newNode(typ reflect.Type, metricsTracer MetricsTracer) *node {
2019-06-13 02:23:03 +00:00
return &node{
typ: typ,
metricsTracer: metricsTracer,
2019-06-13 02:23:03 +00:00
}
}
func (n *node) emit(evt interface{}) {
typ := reflect.TypeOf(evt)
if typ != n.typ {
panic(fmt.Sprintf("Emit called with wrong type. expected: %s, got: %s", n.typ, typ))
2019-06-13 02:23:03 +00:00
}
n.lk.Lock()
2019-06-16 19:42:47 +00:00
if n.keepLast {
n.last = evt
2019-06-16 19:42:47 +00:00
}
for _, sink := range n.sinks {
// Sending metrics before sending on channel allows us to
// record channel full events before blocking
sendSubscriberMetrics(n.metricsTracer, sink)
sink.ch <- evt
2019-06-13 02:23:03 +00:00
}
n.lk.Unlock()
2019-06-13 02:23:03 +00:00
}
func sendSubscriberMetrics(metricsTracer MetricsTracer, sink *namedSink) {
if metricsTracer != nil {
metricsTracer.SubscriberQueueLength(sink.name, len(sink.ch)+1)
metricsTracer.SubscriberQueueFull(sink.name, len(sink.ch)+1 >= cap(sink.ch))
metricsTracer.SubscriberEventQueued(sink.name)
}
}