go-eventbus/basic.go

172 lines
2.8 KiB
Go
Raw Permalink Normal View History

2019-06-13 10:23:03 +08:00
package event
import (
"errors"
"fmt"
"reflect"
"sync"
2019-06-13 14:51:54 +08:00
"sync/atomic"
2019-06-13 10:23:03 +08:00
)
///////////////////////
// BUS
type bus struct {
lk sync.Mutex
2019-06-13 10:23:03 +08:00
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-14 04:25:53 +08:00
defer n.lk.Unlock()
2019-06-13 10:23:03 +08:00
cb(n)
return nil
}
2019-06-13 14:51:54 +08: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 || n.sinkLen() > 0 {
2019-06-13 14:51:54 +08:00
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 10:23:03 +08:00
err = b.withNode(evtType, func(n *node) {
2019-06-13 14:51:54 +08:00
out, i := n.sub(0)
s = out
c = func() {
n.lk.Lock()
n.sink.Delete(i)
2019-06-13 14:51:54 +08:00
close(out)
tryDrop := n.sinkLen() == 0 && n.nEmitters == 0
2019-06-13 14:51:54 +08:00
n.lk.Unlock()
if tryDrop {
b.tryDropNode(evtType)
}
}
2019-06-13 10:23:03 +08:00
})
return
}
func (b *bus) Emitter(evtType interface{}, _ ...EmitterOption) (e EmitFunc, c CancelFunc, err error) {
2019-06-13 10:23:03 +08:00
err = b.withNode(evtType, func(n *node) {
2019-06-13 14:51:54 +08: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 10:23:03 +08:00
}
})
return
}
///////////////////////
// NODE
type node struct {
// not under lock
sink sync.Map
2019-06-13 10:23:03 +08:00
// Note: make sure to NEVER lock bus.lk when this lock is held
lk sync.RWMutex
typ reflect.Type
2019-06-14 04:25:53 +08:00
// emitter ref count
2019-06-13 14:51:54 +08:00
nEmitters int32
2019-06-14 04:25:53 +08:00
// sink index counter
sinkC int
2019-06-13 10:23:03 +08:00
}
func newNode(typ reflect.Type) *node {
return &node{
typ: typ,
}
}
func (n *node) sinkLen() int {
ln := 0
n.sink.Range(func(_, _ interface{}) bool {
ln = ln + 1
return true
})
return ln
}
2019-06-13 14:51:54 +08:00
func (n *node) sub(buf int) (chan interface{}, int) {
2019-06-13 10:23:03 +08:00
out := make(chan interface{}, buf)
2019-06-14 04:25:53 +08:00
i := n.sinkC
n.sinkC++
n.sink.Store(i, out)
2019-06-13 14:51:54 +08:00
return out, i
2019-06-13 10:23:03 +08:00
}
2019-06-13 14:51:54 +08:00
func (n *node) emit(event interface{}) {
2019-06-13 10:23:03 +08: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.sink.Range(func(_, ch interface{}) bool {
ch.(chan interface{}) <- event
return true
})
2019-06-13 10:23:03 +08:00
}
///////////////////////
// UTILS
func typePath(t reflect.Type) string {
return t.PkgPath() + "/" + t.String()
}
var _ Bus = &bus{}