mirror of
https://github.com/libp2p/go-eventbus.git
synced 2026-08-21 15:13:27 +08:00
Compare commits
63 Commits
feat/inter
...
fix-panic-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f33a3e22f | ||
|
|
eef4f37eb1 | ||
|
|
29a4be08ae | ||
|
|
ae6fa05fd0 | ||
|
|
5a668e2b67 | ||
|
|
850de4ee5c | ||
|
|
7a51cd32c5 | ||
|
|
f255214b63 | ||
|
|
b6aa9fbf8e | ||
|
|
a4ba8048f4 | ||
|
|
b5241a9053 | ||
|
|
8d129b2f9c | ||
|
|
94baf4d573 | ||
|
|
4f385d2fea | ||
|
|
ccaa483f77 | ||
|
|
6b6139f7ce | ||
|
|
9c15729013 | ||
|
|
5e53d8f064 | ||
|
|
258b78fa88 | ||
|
|
ff9925e51f | ||
|
|
0e3045bb10 | ||
|
|
8ae18d1356 | ||
|
|
11999efa2d | ||
|
|
e358464c5b | ||
|
|
0a84ebcf30 | ||
|
|
0f2b217368 | ||
|
|
2a09bc5311 | ||
|
|
d34a18eba2 | ||
|
|
7c7260c6f3 | ||
|
|
40251732b1 | ||
|
|
217da8e826 | ||
|
|
b1629519a0 | ||
|
|
4afad1f620 | ||
|
|
d88ec38bc6 | ||
|
|
25d54bbbec | ||
|
|
04058af20a | ||
|
|
a77f09c820 | ||
|
|
7b280b5c1b | ||
|
|
df5be7d7dd | ||
|
|
0c299185af | ||
|
|
12782426ed | ||
|
|
9c3fe0580a | ||
|
|
0976d4b358 | ||
|
|
61257f90a2 | ||
|
|
85cd6aa7c7 | ||
|
|
fc8fa53d99 | ||
|
|
facb8f2fed | ||
|
|
454cbe5497 | ||
|
|
d2cb2a10b6 | ||
|
|
3fd76c7eac | ||
|
|
525a0e67fe | ||
|
|
6212a929bf | ||
|
|
f100eac4ef | ||
|
|
c7aefba960 | ||
|
|
fe6d9d1489 | ||
|
|
131418619d | ||
|
|
015ba825e9 | ||
|
|
8b7b645962 | ||
|
|
bd8289f870 | ||
|
|
1ab671b0ca | ||
|
|
a1807fd415 | ||
|
|
2341c42dab | ||
|
|
3abafaf475 |
11
.github/workflows/automerge.yml
vendored
Normal file
11
.github/workflows/automerge.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
# File managed by web3-bot. DO NOT EDIT.
|
||||
# See https://github.com/protocol/.github/ for details.
|
||||
|
||||
name: Automerge
|
||||
on: [ pull_request ]
|
||||
|
||||
jobs:
|
||||
automerge:
|
||||
uses: protocol/.github/.github/workflows/automerge.yml@master
|
||||
with:
|
||||
job: 'automerge'
|
||||
73
.github/workflows/go-check.yml
vendored
Normal file
73
.github/workflows/go-check.yml
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
# File managed by web3-bot. DO NOT EDIT.
|
||||
# See https://github.com/protocol/.github/ for details.
|
||||
|
||||
on: [push, pull_request]
|
||||
name: Go Checks
|
||||
|
||||
jobs:
|
||||
unit:
|
||||
runs-on: ubuntu-latest
|
||||
name: All
|
||||
env:
|
||||
RUNGOGENERATE: false
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
submodules: recursive
|
||||
- uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: "1.18.x"
|
||||
- name: Run repo-specific setup
|
||||
uses: ./.github/actions/go-check-setup
|
||||
if: hashFiles('./.github/actions/go-check-setup') != ''
|
||||
- name: Read config
|
||||
if: hashFiles('./.github/workflows/go-check-config.json') != ''
|
||||
run: |
|
||||
if jq -re .gogenerate ./.github/workflows/go-check-config.json; then
|
||||
echo "RUNGOGENERATE=true" >> $GITHUB_ENV
|
||||
fi
|
||||
- name: Install staticcheck
|
||||
run: go install honnef.co/go/tools/cmd/staticcheck@d7e217c1ff411395475b2971c0824e1e7cc1af98 # 2022.1 (v0.3.0)
|
||||
- name: Check that go.mod is tidy
|
||||
uses: protocol/multiple-go-modules@v1.2
|
||||
with:
|
||||
run: |
|
||||
go mod tidy
|
||||
if [[ -n $(git ls-files --other --exclude-standard --directory -- go.sum) ]]; then
|
||||
echo "go.sum was added by go mod tidy"
|
||||
exit 1
|
||||
fi
|
||||
git diff --exit-code -- go.sum go.mod
|
||||
- name: gofmt
|
||||
if: ${{ success() || failure() }} # run this step even if the previous one failed
|
||||
run: |
|
||||
out=$(gofmt -s -l .)
|
||||
if [[ -n "$out" ]]; then
|
||||
echo $out | awk '{print "::error file=" $0 ",line=0,col=0::File is not gofmt-ed."}'
|
||||
exit 1
|
||||
fi
|
||||
- name: go vet
|
||||
if: ${{ success() || failure() }} # run this step even if the previous one failed
|
||||
uses: protocol/multiple-go-modules@v1.2
|
||||
with:
|
||||
run: go vet ./...
|
||||
- name: staticcheck
|
||||
if: ${{ success() || failure() }} # run this step even if the previous one failed
|
||||
uses: protocol/multiple-go-modules@v1.2
|
||||
with:
|
||||
run: |
|
||||
set -o pipefail
|
||||
staticcheck ./... | sed -e 's@\(.*\)\.go@./\1.go@g'
|
||||
- name: go generate
|
||||
uses: protocol/multiple-go-modules@v1.2
|
||||
if: (success() || failure()) && env.RUNGOGENERATE == 'true'
|
||||
with:
|
||||
run: |
|
||||
git clean -fd # make sure there aren't untracked files / directories
|
||||
go generate ./...
|
||||
# check if go generate modified or added any files
|
||||
if ! $(git add . && git diff-index HEAD --exit-code --quiet); then
|
||||
echo "go generated caused changes to the repository:"
|
||||
git status --short
|
||||
exit 1
|
||||
fi
|
||||
68
.github/workflows/go-test.yml
vendored
Normal file
68
.github/workflows/go-test.yml
vendored
Normal file
@@ -0,0 +1,68 @@
|
||||
# File managed by web3-bot. DO NOT EDIT.
|
||||
# See https://github.com/protocol/.github/ for details.
|
||||
|
||||
on: [push, pull_request]
|
||||
name: Go Test
|
||||
|
||||
jobs:
|
||||
unit:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
os: [ "ubuntu", "windows", "macos" ]
|
||||
go: [ "1.17.x", "1.18.x" ]
|
||||
env:
|
||||
COVERAGES: ""
|
||||
runs-on: ${{ format('{0}-latest', matrix.os) }}
|
||||
name: ${{ matrix.os }} (go ${{ matrix.go }})
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
submodules: recursive
|
||||
- uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: ${{ matrix.go }}
|
||||
- name: Go information
|
||||
run: |
|
||||
go version
|
||||
go env
|
||||
- name: Use msys2 on windows
|
||||
if: ${{ matrix.os == 'windows' }}
|
||||
shell: bash
|
||||
# The executable for msys2 is also called bash.cmd
|
||||
# https://github.com/actions/virtual-environments/blob/main/images/win/Windows2019-Readme.md#shells
|
||||
# If we prepend its location to the PATH
|
||||
# subsequent 'shell: bash' steps will use msys2 instead of gitbash
|
||||
run: echo "C:/msys64/usr/bin" >> $GITHUB_PATH
|
||||
- name: Run repo-specific setup
|
||||
uses: ./.github/actions/go-test-setup
|
||||
if: hashFiles('./.github/actions/go-test-setup') != ''
|
||||
- name: Run tests
|
||||
uses: protocol/multiple-go-modules@v1.2
|
||||
with:
|
||||
# Use -coverpkg=./..., so that we include cross-package coverage.
|
||||
# If package ./A imports ./B, and ./A's tests also cover ./B,
|
||||
# this means ./B's coverage will be significantly higher than 0%.
|
||||
run: go test -v -coverprofile=module-coverage.txt -coverpkg=./... ./...
|
||||
- name: Run tests (32 bit)
|
||||
if: ${{ matrix.os != 'macos' }} # can't run 32 bit tests on OSX.
|
||||
uses: protocol/multiple-go-modules@v1.2
|
||||
env:
|
||||
GOARCH: 386
|
||||
with:
|
||||
run: |
|
||||
export "PATH=${{ env.PATH_386 }}:$PATH"
|
||||
go test -v ./...
|
||||
- name: Run tests with race detector
|
||||
if: ${{ matrix.os == 'ubuntu' }} # speed things up. Windows and OSX VMs are slow
|
||||
uses: protocol/multiple-go-modules@v1.2
|
||||
with:
|
||||
run: go test -v -race ./...
|
||||
- name: Collect coverage files
|
||||
shell: bash
|
||||
run: echo "COVERAGES=$(find . -type f -name 'module-coverage.txt' | tr -s '\n' ',' | sed 's/,$//')" >> $GITHUB_ENV
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@f32b3a3741e1053eb607407145bc9619351dc93b # v2.1.0
|
||||
with:
|
||||
files: '${{ env.COVERAGES }}'
|
||||
env_vars: OS=${{ matrix.os }}, GO=${{ matrix.go }}
|
||||
11
.github/workflows/release-check.yml
vendored
Normal file
11
.github/workflows/release-check.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
# File managed by web3-bot. DO NOT EDIT.
|
||||
# See https://github.com/protocol/.github/ for details.
|
||||
|
||||
name: Release Checker
|
||||
on:
|
||||
pull_request:
|
||||
paths: [ 'version.json' ]
|
||||
|
||||
jobs:
|
||||
release-check:
|
||||
uses: protocol/.github/.github/workflows/release-check.yml@master
|
||||
11
.github/workflows/releaser.yml
vendored
Normal file
11
.github/workflows/releaser.yml
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
# File managed by web3-bot. DO NOT EDIT.
|
||||
# See https://github.com/protocol/.github/ for details.
|
||||
|
||||
name: Releaser
|
||||
on:
|
||||
push:
|
||||
paths: [ 'version.json' ]
|
||||
|
||||
jobs:
|
||||
releaser:
|
||||
uses: protocol/.github/.github/workflows/releaser.yml@master
|
||||
12
.github/workflows/tagpush.yml
vendored
Normal file
12
.github/workflows/tagpush.yml
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
# File managed by web3-bot. DO NOT EDIT.
|
||||
# See https://github.com/protocol/.github/ for details.
|
||||
|
||||
name: Tag Push Checker
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- v*
|
||||
|
||||
jobs:
|
||||
releaser:
|
||||
uses: protocol/.github/.github/workflows/tagpush.yml@master
|
||||
@@ -5,10 +5,10 @@
|
||||
[](http://webchat.freenode.net/?channels=%23libp2p)
|
||||
[](https://godoc.org/github.com/libp2p/go-eventbus)
|
||||
[](https://coveralls.io/github/libp2p/go-eventbus?branch=master)
|
||||
[](https://travis-ci.org/libp2p/go-eventbus)
|
||||
[](https://travis-ci.com/libp2p/go-eventbus)
|
||||
[](https://discuss.libp2p.io)
|
||||
|
||||
> Simple and fast Go event bus
|
||||
> Simple and fast eventbus for type-based local event delivery.
|
||||
|
||||
## Install
|
||||
|
||||
|
||||
328
basic.go
328
basic.go
@@ -1,4 +1,4 @@
|
||||
package event
|
||||
package eventbus
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -6,23 +6,58 @@ import (
|
||||
"reflect"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/libp2p/go-libp2p-core/event"
|
||||
)
|
||||
|
||||
///////////////////////
|
||||
// BUS
|
||||
|
||||
// basicBus is a type-based event delivery system
|
||||
type basicBus struct {
|
||||
lk sync.Mutex
|
||||
nodes map[reflect.Type]*node
|
||||
lk sync.RWMutex
|
||||
nodes map[reflect.Type]*node
|
||||
wildcard *wildcardNode
|
||||
}
|
||||
|
||||
func NewBus() *basicBus {
|
||||
var _ event.Bus = (*basicBus)(nil)
|
||||
|
||||
type emitter struct {
|
||||
n *node
|
||||
w *wildcardNode
|
||||
typ reflect.Type
|
||||
closed int32
|
||||
dropper func(reflect.Type)
|
||||
}
|
||||
|
||||
func (e *emitter) Emit(evt interface{}) error {
|
||||
if atomic.LoadInt32(&e.closed) != 0 {
|
||||
return fmt.Errorf("emitter is closed")
|
||||
}
|
||||
e.n.emit(evt)
|
||||
e.w.emit(evt)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *emitter) Close() error {
|
||||
if !atomic.CompareAndSwapInt32(&e.closed, 0, 1) {
|
||||
return fmt.Errorf("closed an emitter more than once")
|
||||
}
|
||||
if atomic.AddInt32(&e.n.nEmitters, -1) == 0 {
|
||||
e.dropper(e.typ)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewBus() event.Bus {
|
||||
return &basicBus{
|
||||
nodes: map[reflect.Type]*node{},
|
||||
nodes: map[reflect.Type]*node{},
|
||||
wildcard: new(wildcardNode),
|
||||
}
|
||||
}
|
||||
|
||||
func (b *basicBus) withNode(typ reflect.Type, cb func(*node), async func(*node)) error {
|
||||
func (b *basicBus) withNode(typ reflect.Type, cb func(*node), async func(*node)) {
|
||||
b.lk.Lock()
|
||||
|
||||
n, ok := b.nodes[typ]
|
||||
@@ -36,12 +71,14 @@ func (b *basicBus) withNode(typ reflect.Type, cb func(*node), async func(*node))
|
||||
|
||||
cb(n)
|
||||
|
||||
go func() {
|
||||
defer n.lk.Unlock()
|
||||
async(n)
|
||||
}()
|
||||
|
||||
return nil
|
||||
if async == nil {
|
||||
n.lk.Unlock()
|
||||
} else {
|
||||
go func() {
|
||||
defer n.lk.Unlock()
|
||||
async(n)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
func (b *basicBus) tryDropNode(typ reflect.Type) {
|
||||
@@ -64,64 +101,149 @@ func (b *basicBus) tryDropNode(typ reflect.Type) {
|
||||
b.lk.Unlock()
|
||||
}
|
||||
|
||||
func (b *basicBus) Subscribe(typedChan interface{}, opts ...SubOption) (c CancelFunc, err error) {
|
||||
var settings subSettings
|
||||
type wildcardSub struct {
|
||||
ch chan interface{}
|
||||
w *wildcardNode
|
||||
}
|
||||
|
||||
func (w *wildcardSub) Out() <-chan interface{} {
|
||||
return w.ch
|
||||
}
|
||||
|
||||
func (w *wildcardSub) Close() error {
|
||||
w.w.removeSink(w.ch)
|
||||
return nil
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
}
|
||||
}()
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
close(s.ch)
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ event.Subscription = (*sub)(nil)
|
||||
|
||||
// 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
|
||||
func (b *basicBus) Subscribe(evtTypes interface{}, opts ...event.SubscriptionOpt) (_ event.Subscription, err error) {
|
||||
settings := subSettingsDefault
|
||||
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")
|
||||
}
|
||||
if typ.ChanDir()&reflect.SendDir == 0 {
|
||||
return nil, errors.New("channel doesn't allow send")
|
||||
}
|
||||
|
||||
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)
|
||||
if evtTypes == event.WildcardSubscription {
|
||||
out := &wildcardSub{
|
||||
ch: make(chan interface{}, settings.buffer),
|
||||
w: b.wildcard,
|
||||
}
|
||||
typ = settings.forcedType
|
||||
b.wildcard.addSink(out.ch)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
err = b.withNode(typ.Elem(), func(n *node) {
|
||||
n.sinks = append(n.sinks, refCh)
|
||||
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[len(n.sinks)-1], reflect.Value{}
|
||||
n.sinks = n.sinks[:len(n.sinks)-1]
|
||||
break
|
||||
types, ok := evtTypes.([]interface{})
|
||||
if !ok {
|
||||
types = []interface{}{evtTypes}
|
||||
}
|
||||
|
||||
if len(types) > 1 {
|
||||
for _, t := range types {
|
||||
if t == event.WildcardSubscription {
|
||||
return nil, fmt.Errorf("wildcard subscriptions must be started separately")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out := &sub{
|
||||
ch: make(chan interface{}, settings.buffer),
|
||||
nodes: make([]*node, len(types)),
|
||||
|
||||
dropper: b.tryDropNode,
|
||||
}
|
||||
|
||||
for _, etyp := range types {
|
||||
if reflect.TypeOf(etyp).Kind() != reflect.Ptr {
|
||||
return nil, errors.New("subscribe called with non-pointer type")
|
||||
}
|
||||
}
|
||||
|
||||
for i, etyp := range types {
|
||||
typ := reflect.TypeOf(etyp)
|
||||
|
||||
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
|
||||
if l == nil {
|
||||
return
|
||||
}
|
||||
out.ch <- l
|
||||
}
|
||||
tryDrop := len(n.sinks) == 0 && atomic.LoadInt32(&n.nEmitters) == 0
|
||||
n.lk.Unlock()
|
||||
if tryDrop {
|
||||
b.tryDropNode(typ.Elem())
|
||||
}
|
||||
}
|
||||
}, func(n *node) {
|
||||
if n.keepLast {
|
||||
lastVal, ok := n.last.Load().(reflect.Value)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
refCh.Send(lastVal)
|
||||
}
|
||||
})
|
||||
return
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (b *basicBus) Emitter(evtType interface{}, opts ...EmitterOption) (e EmitFunc, err error) {
|
||||
// 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{})
|
||||
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
|
||||
for _, opt := range opts {
|
||||
opt(&settings)
|
||||
if err := opt(&settings); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
typ := reflect.TypeOf(evtType)
|
||||
@@ -130,34 +252,71 @@ func (b *basicBus) Emitter(evtType interface{}, opts ...EmitterOption) (e EmitFu
|
||||
}
|
||||
typ = typ.Elem()
|
||||
|
||||
err = b.withNode(typ, func(n *node) {
|
||||
b.withNode(typ, func(n *node) {
|
||||
atomic.AddInt32(&n.nEmitters, 1)
|
||||
closed := false
|
||||
n.keepLast = n.keepLast || settings.makeStateful
|
||||
|
||||
e = func(event interface{}) {
|
||||
if closed {
|
||||
panic("emitter is closed")
|
||||
}
|
||||
if event == closeEmit {
|
||||
closed = true
|
||||
if atomic.AddInt32(&n.nEmitters, -1) == 0 {
|
||||
b.tryDropNode(typ)
|
||||
}
|
||||
return
|
||||
}
|
||||
n.emit(event)
|
||||
}
|
||||
}, func(_ *node) {})
|
||||
e = &emitter{n: n, typ: typ, dropper: b.tryDropNode, w: b.wildcard}
|
||||
}, nil)
|
||||
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
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
// NODE
|
||||
|
||||
type wildcardNode struct {
|
||||
sync.RWMutex
|
||||
nSinks int32
|
||||
sinks []chan interface{}
|
||||
}
|
||||
|
||||
func (n *wildcardNode) addSink(ch chan interface{}) {
|
||||
atomic.AddInt32(&n.nSinks, 1) // ok to do outside the lock
|
||||
n.Lock()
|
||||
n.sinks = append(n.sinks, ch)
|
||||
n.Unlock()
|
||||
}
|
||||
|
||||
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 {
|
||||
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 _, ch := range n.sinks {
|
||||
ch <- evt
|
||||
}
|
||||
n.RUnlock()
|
||||
}
|
||||
|
||||
type node struct {
|
||||
// Note: make sure to NEVER lock basicBus.lk when this lock is held
|
||||
lk sync.RWMutex
|
||||
lk sync.Mutex
|
||||
|
||||
typ reflect.Type
|
||||
|
||||
@@ -165,9 +324,9 @@ type node struct {
|
||||
nEmitters int32
|
||||
|
||||
keepLast bool
|
||||
last atomic.Value
|
||||
last interface{}
|
||||
|
||||
sinks []reflect.Value
|
||||
sinks []chan interface{}
|
||||
}
|
||||
|
||||
func newNode(typ reflect.Type) *node {
|
||||
@@ -176,24 +335,19 @@ func newNode(typ reflect.Type) *node {
|
||||
}
|
||||
}
|
||||
|
||||
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()))
|
||||
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))
|
||||
}
|
||||
|
||||
n.lk.RLock()
|
||||
n.lk.Lock()
|
||||
if n.keepLast {
|
||||
n.last.Store(eval)
|
||||
n.last = evt
|
||||
}
|
||||
|
||||
for _, ch := range n.sinks {
|
||||
ch.Send(eval)
|
||||
ch <- evt
|
||||
}
|
||||
n.lk.RUnlock()
|
||||
n.lk.Unlock()
|
||||
}
|
||||
|
||||
///////////////////////
|
||||
// UTILS
|
||||
|
||||
var _ Bus = &basicBus{}
|
||||
|
||||
489
basic_test.go
489
basic_test.go
@@ -1,13 +1,18 @@
|
||||
package event
|
||||
package eventbus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jbenet/go-detect-race"
|
||||
"github.com/libp2p/go-libp2p-core/event"
|
||||
"github.com/libp2p/go-libp2p-testing/race"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type EventA struct{}
|
||||
@@ -15,7 +20,7 @@ type EventB int
|
||||
|
||||
func getN() int {
|
||||
n := 50000
|
||||
if detectrace.WithRace() {
|
||||
if race.WithRace() {
|
||||
n = 1000
|
||||
}
|
||||
return n
|
||||
@@ -25,32 +30,41 @@ func (EventA) String() string {
|
||||
return "Oh, Hello"
|
||||
}
|
||||
|
||||
func TestDefaultSubIsBuffered(t *testing.T) {
|
||||
bus := NewBus()
|
||||
s, err := bus.Subscribe(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cap(s.(*sub).ch) == 0 {
|
||||
t.Fatalf("without any options subscribe should be buffered. was %d", cap(s.(*sub).ch))
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmit(t *testing.T) {
|
||||
bus := NewBus()
|
||||
events := make(chan EventA)
|
||||
cancel, err := bus.Subscribe(events)
|
||||
sub, err := bus.Subscribe(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer cancel()
|
||||
<-events
|
||||
defer sub.Close()
|
||||
<-sub.Out()
|
||||
}()
|
||||
|
||||
emit, err := bus.Emitter(new(EventA))
|
||||
em, err := bus.Emitter(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer emit.Close()
|
||||
defer em.Close()
|
||||
|
||||
emit(EventA{})
|
||||
em.Emit(EventA{})
|
||||
}
|
||||
|
||||
func TestSub(t *testing.T) {
|
||||
bus := NewBus()
|
||||
events := make(chan EventB)
|
||||
cancel, err := bus.Subscribe(events)
|
||||
sub, err := bus.Subscribe(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -61,18 +75,18 @@ func TestSub(t *testing.T) {
|
||||
wait.Add(1)
|
||||
|
||||
go func() {
|
||||
defer cancel()
|
||||
event = <-events
|
||||
defer sub.Close()
|
||||
event = (<-sub.Out()).(EventB)
|
||||
wait.Done()
|
||||
}()
|
||||
|
||||
emit, err := bus.Emitter(new(EventB))
|
||||
em, err := bus.Emitter(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer emit.Close()
|
||||
defer em.Close()
|
||||
|
||||
emit(EventB(7))
|
||||
em.Emit(EventB(7))
|
||||
wait.Wait()
|
||||
|
||||
if event != 7 {
|
||||
@@ -80,38 +94,57 @@ func TestSub(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllEventTypes(t *testing.T) {
|
||||
bus := NewBus()
|
||||
require.Empty(t, bus.GetAllEventTypes())
|
||||
|
||||
// the wildcard subscription should be returned.
|
||||
_, err := bus.Subscribe(event.WildcardSubscription)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = bus.Subscribe(new(EventB))
|
||||
require.NoError(t, err)
|
||||
|
||||
evts := bus.GetAllEventTypes()
|
||||
require.Len(t, evts, 1)
|
||||
require.Equal(t, reflect.TypeOf((*EventB)(nil)).Elem(), evts[0])
|
||||
|
||||
_, err = bus.Emitter(new(EventA))
|
||||
require.NoError(t, err)
|
||||
|
||||
evts = bus.GetAllEventTypes()
|
||||
require.Len(t, evts, 2)
|
||||
require.Contains(t, evts, reflect.TypeOf((*EventB)(nil)).Elem())
|
||||
require.Contains(t, evts, reflect.TypeOf((*EventA)(nil)).Elem())
|
||||
}
|
||||
|
||||
func TestEmitNoSubNoBlock(t *testing.T) {
|
||||
bus := NewBus()
|
||||
|
||||
emit, err := bus.Emitter(new(EventA))
|
||||
em, err := bus.Emitter(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer emit.Close()
|
||||
defer em.Close()
|
||||
|
||||
emit(EventA{})
|
||||
em.Emit(EventA{})
|
||||
}
|
||||
|
||||
func TestEmitOnClosed(t *testing.T) {
|
||||
bus := NewBus()
|
||||
|
||||
emit, err := bus.Emitter(new(EventA))
|
||||
em, err := bus.Emitter(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
emit.Close()
|
||||
|
||||
defer func() {
|
||||
r := recover()
|
||||
if r == nil {
|
||||
t.Errorf("expected panic")
|
||||
}
|
||||
if r.(string) != "emitter is closed" {
|
||||
t.Error("unexpected message")
|
||||
}
|
||||
}()
|
||||
|
||||
emit(EventA{})
|
||||
em.Close()
|
||||
err = em.Emit(EventA{})
|
||||
if err == nil {
|
||||
t.Errorf("expected error")
|
||||
}
|
||||
if err.Error() != "emitter is closed" {
|
||||
t.Error("unexpected message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClosingRaces(t *testing.T) {
|
||||
@@ -131,9 +164,9 @@ func TestClosingRaces(t *testing.T) {
|
||||
lk.RLock()
|
||||
defer lk.RUnlock()
|
||||
|
||||
cancel, _ := b.Subscribe(make(chan EventA))
|
||||
sub, _ := b.Subscribe(new(EventA))
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
cancel()
|
||||
sub.Close()
|
||||
|
||||
wg.Done()
|
||||
}()
|
||||
@@ -156,7 +189,7 @@ func TestClosingRaces(t *testing.T) {
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if len(b.nodes) != 0 {
|
||||
if len(b.(*basicBus).nodes) != 0 {
|
||||
t.Error("expected no nodes")
|
||||
}
|
||||
}
|
||||
@@ -174,28 +207,27 @@ func TestSubMany(t *testing.T) {
|
||||
|
||||
for i := 0; i < n; i++ {
|
||||
go func() {
|
||||
events := make(chan EventB)
|
||||
cancel, err := bus.Subscribe(events)
|
||||
sub, err := bus.Subscribe(new(EventB))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer cancel()
|
||||
defer sub.Close()
|
||||
|
||||
ready.Done()
|
||||
atomic.AddInt32(&r, int32(<-events))
|
||||
atomic.AddInt32(&r, int32((<-sub.Out()).(EventB)))
|
||||
wait.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
emit, err := bus.Emitter(new(EventB))
|
||||
em, err := bus.Emitter(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer emit.Close()
|
||||
defer em.Close()
|
||||
|
||||
ready.Wait()
|
||||
|
||||
emit(EventB(7))
|
||||
em.Emit(EventB(7))
|
||||
wait.Wait()
|
||||
|
||||
if int(r) != 7*n {
|
||||
@@ -203,10 +235,111 @@ func TestSubMany(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWildcardSubscription(t *testing.T) {
|
||||
bus := NewBus()
|
||||
sub, err := bus.Subscribe(event.WildcardSubscription)
|
||||
require.NoError(t, err)
|
||||
defer sub.Close()
|
||||
|
||||
em1, err := bus.Emitter(new(EventA))
|
||||
require.NoError(t, err)
|
||||
defer em1.Close()
|
||||
|
||||
em2, err := bus.Emitter(new(EventB))
|
||||
require.NoError(t, err)
|
||||
defer em2.Close()
|
||||
|
||||
require.NoError(t, em1.Emit(EventA{}))
|
||||
require.NoError(t, em2.Emit(EventB(1)))
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
var evts []interface{}
|
||||
|
||||
LOOP:
|
||||
for {
|
||||
select {
|
||||
case evt := <-sub.Out():
|
||||
if evta, ok := evt.(EventA); ok {
|
||||
evts = append(evts, evta)
|
||||
}
|
||||
|
||||
if evtb, ok := evt.(EventB); ok {
|
||||
evts = append(evts, evtb)
|
||||
}
|
||||
|
||||
if len(evts) == 2 {
|
||||
break LOOP
|
||||
}
|
||||
|
||||
case <-ctx.Done():
|
||||
t.Fatalf("did not receive events")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestManyWildcardSubscriptions(t *testing.T) {
|
||||
bus := NewBus()
|
||||
var subs []event.Subscription
|
||||
for i := 0; i < 10; i++ {
|
||||
sub, err := bus.Subscribe(event.WildcardSubscription)
|
||||
require.NoError(t, err)
|
||||
subs = append(subs, sub)
|
||||
}
|
||||
|
||||
em1, err := bus.Emitter(new(EventA))
|
||||
require.NoError(t, err)
|
||||
defer em1.Close()
|
||||
|
||||
em2, err := bus.Emitter(new(EventB))
|
||||
require.NoError(t, err)
|
||||
defer em2.Close()
|
||||
|
||||
require.NoError(t, em1.Emit(EventA{}))
|
||||
require.NoError(t, em2.Emit(EventB(1)))
|
||||
|
||||
// all 10 subscriptions received all 2 events.
|
||||
for _, s := range subs {
|
||||
require.Len(t, s.Out(), 2)
|
||||
}
|
||||
|
||||
// close the first five subscriptions.
|
||||
for _, s := range subs[:5] {
|
||||
require.NoError(t, s.Close())
|
||||
}
|
||||
|
||||
// emit another 2 events.
|
||||
require.NoError(t, em1.Emit(EventA{}))
|
||||
require.NoError(t, em2.Emit(EventB(1)))
|
||||
|
||||
// the first five still have 2 events, while the other five have 4 events.
|
||||
for _, s := range subs[:5] {
|
||||
require.Len(t, s.Out(), 2)
|
||||
}
|
||||
|
||||
for _, s := range subs[5:] {
|
||||
require.Len(t, s.Out(), 4)
|
||||
}
|
||||
|
||||
// close them all, the first five will be closed twice (asserts idempotency).
|
||||
for _, s := range subs {
|
||||
require.NoError(t, s.Close())
|
||||
}
|
||||
}
|
||||
|
||||
func TestWildcardValidations(t *testing.T) {
|
||||
bus := NewBus()
|
||||
|
||||
_, err := bus.Subscribe([]interface{}{event.WildcardSubscription, new(EventA), new(EventB)})
|
||||
require.Error(t, err)
|
||||
|
||||
_, err = bus.Emitter(event.WildcardSubscription)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestSubType(t *testing.T) {
|
||||
bus := NewBus()
|
||||
events := make(chan fmt.Stringer)
|
||||
cancel, err := bus.Subscribe(events, ForceSubType(new(EventA)))
|
||||
sub, err := bus.Subscribe([]interface{}{new(EventA), new(EventB)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -217,18 +350,18 @@ func TestSubType(t *testing.T) {
|
||||
wait.Add(1)
|
||||
|
||||
go func() {
|
||||
defer cancel()
|
||||
event = <-events
|
||||
defer sub.Close()
|
||||
event = (<-sub.Out()).(EventA)
|
||||
wait.Done()
|
||||
}()
|
||||
|
||||
emit, err := bus.Emitter(new(EventA))
|
||||
em, err := bus.Emitter(new(EventA))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer emit.Close()
|
||||
defer em.Close()
|
||||
|
||||
emit(EventA{})
|
||||
em.Emit(EventA{})
|
||||
wait.Wait()
|
||||
|
||||
if event.String() != "Oh, Hello" {
|
||||
@@ -238,45 +371,43 @@ func TestSubType(t *testing.T) {
|
||||
|
||||
func TestNonStateful(t *testing.T) {
|
||||
bus := NewBus()
|
||||
emit, err := bus.Emitter(new(EventB))
|
||||
em, err := bus.Emitter(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer emit.Close()
|
||||
defer em.Close()
|
||||
|
||||
eventsA := make(chan EventB, 1)
|
||||
cancelS, err := bus.Subscribe(eventsA)
|
||||
sub1, err := bus.Subscribe(new(EventB), BufSize(1))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cancelS()
|
||||
defer sub1.Close()
|
||||
|
||||
select {
|
||||
case <-eventsA:
|
||||
case <-sub1.Out():
|
||||
t.Fatal("didn't expect to get an event")
|
||||
default:
|
||||
}
|
||||
|
||||
emit(EventB(1))
|
||||
em.Emit(EventB(1))
|
||||
|
||||
select {
|
||||
case e := <-eventsA:
|
||||
if e != 1 {
|
||||
case e := <-sub1.Out():
|
||||
if e.(EventB) != 1 {
|
||||
t.Fatal("got wrong event")
|
||||
}
|
||||
default:
|
||||
t.Fatal("expected to get an event")
|
||||
}
|
||||
|
||||
eventsB := make(chan EventB, 1)
|
||||
cancelS2, err := bus.Subscribe(eventsB)
|
||||
sub2, err := bus.Subscribe(new(EventB), BufSize(1))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cancelS2()
|
||||
defer sub2.Close()
|
||||
|
||||
select {
|
||||
case <-eventsA:
|
||||
case <-sub2.Out():
|
||||
t.Fatal("didn't expect to get an event")
|
||||
default:
|
||||
}
|
||||
@@ -284,28 +415,73 @@ func TestNonStateful(t *testing.T) {
|
||||
|
||||
func TestStateful(t *testing.T) {
|
||||
bus := NewBus()
|
||||
emit, err := bus.Emitter(new(EventB), Stateful)
|
||||
em, err := bus.Emitter(new(EventB), Stateful)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer emit.Close()
|
||||
defer em.Close()
|
||||
|
||||
emit(EventB(2))
|
||||
em.Emit(EventB(2))
|
||||
|
||||
eventsA := make(chan EventB, 1)
|
||||
cancelS, err := bus.Subscribe(eventsA)
|
||||
sub, err := bus.Subscribe(new(EventB), BufSize(1))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer cancelS()
|
||||
defer sub.Close()
|
||||
|
||||
if <-eventsA != 2 {
|
||||
if (<-sub.Out()).(EventB) != 2 {
|
||||
t.Fatal("got wrong event")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCloseBlocking(t *testing.T) {
|
||||
bus := NewBus()
|
||||
em, err := bus.Emitter(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
sub, err := bus.Subscribe(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
em.Emit(EventB(159))
|
||||
}()
|
||||
|
||||
time.Sleep(10 * time.Millisecond) // make sure that emit is blocked
|
||||
|
||||
sub.Close() // cancel sub
|
||||
}
|
||||
|
||||
func TestSubFailFully(t *testing.T) {
|
||||
bus := NewBus()
|
||||
em, err := bus.Emitter(new(EventB))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = bus.Subscribe([]interface{}{new(EventB), 5})
|
||||
if err == nil || err.Error() != "subscribe called with non-pointer type" {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
em.Emit(EventB(159)) // will hang if sub doesn't fail properly
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func testMany(t testing.TB, subs, emits, msgs int, stateful bool) {
|
||||
if detectrace.WithRace() && subs+emits > 5000 {
|
||||
if race.WithRace() && subs+emits > 5000 {
|
||||
t.SkipNow()
|
||||
}
|
||||
|
||||
@@ -320,16 +496,19 @@ func testMany(t testing.TB, subs, emits, msgs int, stateful bool) {
|
||||
|
||||
for i := 0; i < subs; i++ {
|
||||
go func() {
|
||||
events := make(chan EventB)
|
||||
cancel, err := bus.Subscribe(events)
|
||||
sub, err := bus.Subscribe(new(EventB))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer cancel()
|
||||
defer sub.Close()
|
||||
|
||||
ready.Done()
|
||||
for i := 0; i < emits*msgs; i++ {
|
||||
atomic.AddInt64(&r, int64(<-events))
|
||||
e, ok := <-sub.Out()
|
||||
if !ok {
|
||||
panic("wat")
|
||||
}
|
||||
atomic.AddInt64(&r, int64(e.(EventB)))
|
||||
}
|
||||
wait.Done()
|
||||
}()
|
||||
@@ -337,18 +516,19 @@ func testMany(t testing.TB, subs, emits, msgs int, stateful bool) {
|
||||
|
||||
for i := 0; i < emits; i++ {
|
||||
go func() {
|
||||
emit, err := bus.Emitter(new(EventB), func(settings *emitterSettings) {
|
||||
settings.makeStateful = stateful
|
||||
em, err := bus.Emitter(new(EventB), func(settings interface{}) error {
|
||||
settings.(*emitterSettings).makeStateful = stateful
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer emit.Close()
|
||||
defer em.Close()
|
||||
|
||||
ready.Wait()
|
||||
|
||||
for i := 0; i < msgs; i++ {
|
||||
emit(EventB(97))
|
||||
em.Emit(EventB(97))
|
||||
}
|
||||
|
||||
wait.Done()
|
||||
@@ -366,69 +546,122 @@ func TestBothMany(t *testing.T) {
|
||||
testMany(t, 10000, 100, 10, false)
|
||||
}
|
||||
|
||||
func BenchmarkSubs(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, b.N, 100, 100, false)
|
||||
type benchCase struct {
|
||||
subs int
|
||||
emits int
|
||||
stateful bool
|
||||
}
|
||||
|
||||
func BenchmarkEmits(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, 100, b.N, 100, false)
|
||||
func (bc benchCase) name() string {
|
||||
return fmt.Sprintf("subs-%03d/emits-%03d/stateful-%t", bc.subs, bc.emits, bc.stateful)
|
||||
}
|
||||
|
||||
func BenchmarkMsgs(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, 100, 100, b.N, false)
|
||||
func genTestCases() []benchCase {
|
||||
ret := make([]benchCase, 0, 200)
|
||||
for stateful := 0; stateful < 2; stateful++ {
|
||||
for subs := uint(0); subs <= 8; subs = subs + 4 {
|
||||
for emits := uint(0); emits <= 8; emits = emits + 4 {
|
||||
ret = append(ret, benchCase{1 << subs, 1 << emits, stateful == 1})
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func BenchmarkOneToMany(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, b.N, 1, 100, false)
|
||||
func BenchmarkEvents(b *testing.B) {
|
||||
for _, bc := range genTestCases() {
|
||||
b.Run(bc.name(), benchMany(bc))
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkManyToOne(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, 1, b.N, 100, false)
|
||||
func benchMany(bc benchCase) func(*testing.B) {
|
||||
return func(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
subs := bc.subs
|
||||
emits := bc.emits
|
||||
stateful := bc.stateful
|
||||
bus := NewBus()
|
||||
var wait sync.WaitGroup
|
||||
var ready sync.WaitGroup
|
||||
wait.Add(subs + emits)
|
||||
ready.Add(subs + emits)
|
||||
|
||||
for i := 0; i < subs; i++ {
|
||||
go func() {
|
||||
sub, err := bus.Subscribe(new(EventB))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer sub.Close()
|
||||
|
||||
ready.Done()
|
||||
ready.Wait()
|
||||
for i := 0; i < (b.N/emits)*emits; i++ {
|
||||
_, ok := <-sub.Out()
|
||||
if !ok {
|
||||
panic("wat")
|
||||
}
|
||||
}
|
||||
wait.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
for i := 0; i < emits; i++ {
|
||||
go func() {
|
||||
em, err := bus.Emitter(new(EventB), func(settings interface{}) error {
|
||||
settings.(*emitterSettings).makeStateful = stateful
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer em.Close()
|
||||
|
||||
ready.Done()
|
||||
ready.Wait()
|
||||
|
||||
for i := 0; i < b.N/emits; i++ {
|
||||
em.Emit(EventB(97))
|
||||
}
|
||||
|
||||
wait.Done()
|
||||
}()
|
||||
}
|
||||
ready.Wait()
|
||||
b.ResetTimer()
|
||||
wait.Wait()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMs1e2m4(b *testing.B) {
|
||||
b.N = 1000000
|
||||
var div = 100
|
||||
|
||||
func BenchmarkSubscribe(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, 10, 100, 10000, false)
|
||||
for i := 0; i < b.N/div; i++ {
|
||||
bus := NewBus()
|
||||
for j := 0; j < div; j++ {
|
||||
bus.Subscribe(new(EventA))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMs1e0m6(b *testing.B) {
|
||||
b.N = 10000000
|
||||
func BenchmarkEmitter(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, 10, 1, 1000000, false)
|
||||
for i := 0; i < b.N/div; i++ {
|
||||
bus := NewBus()
|
||||
for j := 0; j < div; j++ {
|
||||
bus.Emitter(new(EventA))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMs0e0m6(b *testing.B) {
|
||||
b.N = 1000000
|
||||
func BenchmarkSubscribeAndEmitter(b *testing.B) {
|
||||
b.ReportAllocs()
|
||||
testMany(b, 1, 1, 1000000, false)
|
||||
}
|
||||
|
||||
func BenchmarkStatefulMs1e0m6(b *testing.B) {
|
||||
b.N = 10000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 10, 1, 1000000, true)
|
||||
}
|
||||
|
||||
func BenchmarkStatefulMs0e0m6(b *testing.B) {
|
||||
b.N = 1000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 1, 1, 1000000, true)
|
||||
}
|
||||
|
||||
func BenchmarkMs0e6m0(b *testing.B) {
|
||||
b.N = 1000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 1, 1000000, 1, false)
|
||||
}
|
||||
|
||||
func BenchmarkMs6e0m0(b *testing.B) {
|
||||
b.N = 1000000
|
||||
b.ReportAllocs()
|
||||
testMany(b, 1000000, 1, 1, false)
|
||||
for i := 0; i < b.N/div; i++ {
|
||||
bus := NewBus()
|
||||
for j := 0; j < div; j++ {
|
||||
bus.Subscribe(new(EventA))
|
||||
bus.Emitter(new(EventA))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
3
codecov.yml
Normal file
3
codecov.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
coverage:
|
||||
range: "50...100"
|
||||
comment: off
|
||||
32
go.mod
32
go.mod
@@ -1,5 +1,33 @@
|
||||
module github.com/libp2p/go-eventbus
|
||||
|
||||
go 1.12
|
||||
go 1.17
|
||||
|
||||
require github.com/jbenet/go-detect-race v0.0.0-20150302022421-3463798d9574
|
||||
require (
|
||||
github.com/libp2p/go-libp2p-core v0.5.7
|
||||
github.com/libp2p/go-libp2p-testing v0.1.1
|
||||
github.com/stretchr/testify v1.4.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/btcsuite/btcd v0.20.1-beta // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/gogo/protobuf v1.3.1 // indirect
|
||||
github.com/ipfs/go-cid v0.0.5 // indirect
|
||||
github.com/jbenet/goprocess v0.1.4 // indirect
|
||||
github.com/libp2p/go-buffer-pool v0.0.2 // indirect
|
||||
github.com/libp2p/go-openssl v0.0.5 // indirect
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
|
||||
github.com/minio/sha256-simd v0.1.1 // indirect
|
||||
github.com/mr-tron/base58 v1.1.3 // indirect
|
||||
github.com/multiformats/go-base32 v0.0.3 // indirect
|
||||
github.com/multiformats/go-multiaddr v0.2.2 // indirect
|
||||
github.com/multiformats/go-multibase v0.0.1 // indirect
|
||||
github.com/multiformats/go-multihash v0.0.13 // indirect
|
||||
github.com/multiformats/go-varint v0.0.5 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
|
||||
github.com/spaolacci/murmur3 v1.1.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443 // indirect
|
||||
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb // indirect
|
||||
gopkg.in/yaml.v2 v2.2.2 // indirect
|
||||
)
|
||||
|
||||
179
go.sum
179
go.sum
@@ -1,2 +1,177 @@
|
||||
github.com/jbenet/go-detect-race v0.0.0-20150302022421-3463798d9574 h1:Pxjl8Wn3cCU7nB/MCmPEUMbjMHxXFqODW6rce0jpxB4=
|
||||
github.com/jbenet/go-detect-race v0.0.0-20150302022421-3463798d9574/go.mod h1:gynVu6LUw+xMXD3XEvjHQcIbJkWEamnGjJDebRHqTd0=
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
|
||||
github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c/go.mod h1:3J08xEfcugPacsc34/LKRU2yO7YmuT8yt28J8k2+rrI=
|
||||
github.com/btcsuite/btcd v0.20.1-beta h1:Ik4hyJqN8Jfyv3S4AGBOmyouMsYE3EdYODkMbQjwPGw=
|
||||
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
|
||||
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
|
||||
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
|
||||
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
|
||||
github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY=
|
||||
github.com/btcsuite/snappy-go v0.0.0-20151229074030-0bdef8d06723/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc=
|
||||
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY=
|
||||
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
|
||||
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
|
||||
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
|
||||
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
|
||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
|
||||
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
|
||||
github.com/ipfs/go-cid v0.0.2/go.mod h1:GHWU/WuQdMPmIosc4Yn1bcCT7dSeX4lBafM7iqUPQvM=
|
||||
github.com/ipfs/go-cid v0.0.5 h1:o0Ix8e/ql7Zb5UVUJEUfjsWCIY8t48++9lR8qi6oiJU=
|
||||
github.com/ipfs/go-cid v0.0.5/go.mod h1:plgt+Y5MnOey4vO4UlUazGqdbEXuFYitED67FexhXog=
|
||||
github.com/jbenet/go-cienv v0.1.0/go.mod h1:TqNnHUmJgXau0nCzC7kXWeotg3J9W34CUv5Djy1+FlA=
|
||||
github.com/jbenet/goprocess v0.1.3/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
|
||||
github.com/jbenet/goprocess v0.1.4 h1:DRGOFReOMqqDNXwW70QkacFW0YN9QnwLV0Vqk+3oU0o=
|
||||
github.com/jbenet/goprocess v0.1.4/go.mod h1:5yspPrukOVuOLORacaBi858NqyClJPQxYZlqdZVfqY4=
|
||||
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
|
||||
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d/go.mod h1:P2viExyCEfeWGU259JnaQ34Inuec4R38JCyBx2edgD0=
|
||||
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
|
||||
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ=
|
||||
github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs=
|
||||
github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM=
|
||||
github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8=
|
||||
github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
|
||||
github.com/libp2p/go-libp2p-core v0.2.0/go.mod h1:X0eyB0Gy93v0DZtSYbEM7RnMChm9Uv3j7yRXjO77xSI=
|
||||
github.com/libp2p/go-libp2p-core v0.5.7 h1:QK3xRwFxqd0Xd9bSZL+8yZ8ncZZbl6Zngd/+Y+A6sgQ=
|
||||
github.com/libp2p/go-libp2p-core v0.5.7/go.mod h1:txwbVEhHEXikXn9gfC7/UDDw7rkxuX0bJvM49Ykaswo=
|
||||
github.com/libp2p/go-libp2p-testing v0.1.1 h1:U03z3HnGI7Ni8Xx6ONVZvUFOAzWYmolWf5W5jAOPNmU=
|
||||
github.com/libp2p/go-libp2p-testing v0.1.1/go.mod h1:xaZWMJrPUM5GlDBxCeGUi7kI4eqnjVyavGroI2nxEM0=
|
||||
github.com/libp2p/go-msgio v0.0.4/go.mod h1:63lBBgOTDKQL6EWazRMCwXsEeEeK9O2Cd+0+6OOuipQ=
|
||||
github.com/libp2p/go-openssl v0.0.5 h1:pQkejVhF0xp08D4CQUcw8t+BFJeXowja6RVcb5p++EA=
|
||||
github.com/libp2p/go-openssl v0.0.5/go.mod h1:unDrJpgy3oFr+rqXsarWifmJuNnJR4chtO1HmaZjggc=
|
||||
github.com/mailru/easyjson v0.0.0-20180823135443-60711f1a8329/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
|
||||
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
|
||||
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
|
||||
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
|
||||
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
||||
github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
||||
github.com/minio/sha256-simd v0.1.0/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
|
||||
github.com/minio/sha256-simd v0.1.1-0.20190913151208-6de447530771/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
|
||||
github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU=
|
||||
github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM=
|
||||
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
|
||||
github.com/mr-tron/base58 v1.1.2/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
|
||||
github.com/mr-tron/base58 v1.1.3 h1:v+sk57XuaCKGXpWtVBX8YJzO7hMGx4Aajh4TQbdEFdc=
|
||||
github.com/mr-tron/base58 v1.1.3/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
|
||||
github.com/multiformats/go-base32 v0.0.3 h1:tw5+NhuwaOjJCC5Pp82QuXbrmLzWg7uxlMFp8Nq/kkI=
|
||||
github.com/multiformats/go-base32 v0.0.3/go.mod h1:pLiuGC8y0QR3Ue4Zug5UzK9LjgbkL8NSQj0zQ5Nz/AA=
|
||||
github.com/multiformats/go-multiaddr v0.0.4/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44=
|
||||
github.com/multiformats/go-multiaddr v0.2.2 h1:XZLDTszBIJe6m0zF6ITBrEcZR73OPUhCBBS9rYAuUzI=
|
||||
github.com/multiformats/go-multiaddr v0.2.2/go.mod h1:NtfXiOtHvghW9KojvtySjH5y0u0xW5UouOmQQrn6a3Y=
|
||||
github.com/multiformats/go-multibase v0.0.1 h1:PN9/v21eLywrFWdFNsFKaU04kLJzuYzmrJR+ubhT9qA=
|
||||
github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs=
|
||||
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
|
||||
github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po=
|
||||
github.com/multiformats/go-multihash v0.0.13 h1:06x+mk/zj1FoMsgNejLpy6QTvJqlSt/BhLEy87zidlc=
|
||||
github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc=
|
||||
github.com/multiformats/go-varint v0.0.5 h1:XVZwSo04Cs3j/jS0uAEPpT3JY6DzMcVLLoWOSnCxOjg=
|
||||
github.com/multiformats/go-varint v0.0.5/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
|
||||
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
|
||||
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qtVc0ypY=
|
||||
github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0=
|
||||
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 h1:RC6RW7j+1+HkWaX/Yh71Ee5ZHaHYt7ZP4sQgUrm6cDU=
|
||||
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572/go.mod h1:w0SWMsp6j9O/dk4/ZpIhL+3CkG8ofA2vuv7k+ltqUMc=
|
||||
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
|
||||
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
|
||||
github.com/src-d/envconfig v1.0.0/go.mod h1:Q9YQZ7BKITldTBnoxsE5gOeB5y66RyPXeue/R4aaNBc=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/x-cray/logrus-prefixed-formatter v0.5.2/go.mod h1:2duySbKsL6M18s5GU7VPsoEPHyzalCE06qoARUCeBBE=
|
||||
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
|
||||
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
|
||||
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443 h1:IcSOAf4PyMp3U3XbIEj1/xJ2BjNN2jWv7JoyOsMxXUU=
|
||||
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb h1:fgwFCsaw9buMuxNd6+DQfAuSFqbNiQZpcgJQAgJsK6k=
|
||||
golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20181130052023-1c3d964395ce/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/genproto v0.0.0-20190307195333-5fe7a883aa19/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||
google.golang.org/genproto v0.0.0-20190425155659-357c62f0e4bb/go.mod h1:VzzqZJRnGkLBvHegQrXjBqPurQTc5/KpmUdxsrq26oE=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||
gopkg.in/src-d/go-cli.v0 v0.0.0-20181105080154-d492247bbc0d/go.mod h1:z+K8VcOYVYcSwSjGebuDL6176A1XskgbtNl64NSg+n8=
|
||||
gopkg.in/src-d/go-log.v1 v1.0.1/go.mod h1:GN34hKP0g305ysm2/hctJ0Y8nWP3zxXXJ8GFabTyABE=
|
||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
||||
96
interface.go
96
interface.go
@@ -1,96 +0,0 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var closeEmit struct{}
|
||||
|
||||
type subSettings struct {
|
||||
forcedType reflect.Type
|
||||
}
|
||||
|
||||
type SubOption func(interface{}) error
|
||||
|
||||
// ForceSubType is a Subscribe option which overrides the type to which
|
||||
// the subscription will be done. Note that the evtType must be assignable
|
||||
// to channel type.
|
||||
//
|
||||
// This also allows for subscribing to multiple eventbus channels with one
|
||||
// Go channel to get better ordering guarantees.
|
||||
//
|
||||
// Example:
|
||||
// type Event struct{}
|
||||
// func (Event) String() string {
|
||||
// return "event"
|
||||
// }
|
||||
//
|
||||
// eventCh := make(chan fmt.Stringer) // interface { String() string }
|
||||
// cancel, err := eventbus.Subscribe(eventCh, event.ForceSubType(new(Event)))
|
||||
// [...]
|
||||
func ForceSubType(evtType interface{}) SubOption {
|
||||
return func(settings interface{}) error {
|
||||
s := settings.(*subSettings)
|
||||
typ := reflect.TypeOf(evtType)
|
||||
if typ.Kind() != reflect.Ptr {
|
||||
return errors.New("ForceSubType called with non-pointer type")
|
||||
}
|
||||
s.forcedType = typ
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type emitterSettings struct {
|
||||
makeStateful bool
|
||||
}
|
||||
type EmitterOption func(interface{}) error
|
||||
|
||||
// Stateful is an Emitter option which makes makes the eventbus channel
|
||||
// 'remember' last event sent, and when a new subscriber joins the
|
||||
// bus, the remembered event is immediately sent to the subscription
|
||||
// channel.
|
||||
//
|
||||
// This allows to provide state tracking for dynamic systems, and/or
|
||||
// allows new subscribers to verify that there are Emitters on the channel
|
||||
func Stateful(s *emitterSettings) {
|
||||
s.makeStateful = true
|
||||
}
|
||||
|
||||
// Bus is an interface to type-based event delivery system
|
||||
type Bus interface {
|
||||
// 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
|
||||
//
|
||||
// Example:
|
||||
// ch := make(chan EventT, 10)
|
||||
// defer close(ch)
|
||||
// cancel, err := eventbus.Subscribe(ch)
|
||||
// defer cancel()
|
||||
Subscribe(typedChan interface{}, opts ...SubOption) (CancelFunc, error)
|
||||
|
||||
// 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{})
|
||||
Emitter(eventType interface{}, opts ...EmitterOption) (EmitFunc, error)
|
||||
}
|
||||
|
||||
// EmitFunc emits events. If any channel subscribed to the topic is blocked,
|
||||
// calls to EmitFunc will block
|
||||
//
|
||||
// Calling this function with wrong event type will cause a panic
|
||||
type EmitFunc func(event interface{})
|
||||
|
||||
func (f EmitFunc) Close() {
|
||||
f(closeEmit)
|
||||
}
|
||||
|
||||
type CancelFunc func()
|
||||
32
opts.go
Normal file
32
opts.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package eventbus
|
||||
|
||||
type subSettings struct {
|
||||
buffer int
|
||||
}
|
||||
|
||||
var subSettingsDefault = subSettings{
|
||||
buffer: 16,
|
||||
}
|
||||
|
||||
func BufSize(n int) func(interface{}) error {
|
||||
return func(s interface{}) error {
|
||||
s.(*subSettings).buffer = n
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
type emitterSettings struct {
|
||||
makeStateful bool
|
||||
}
|
||||
|
||||
// Stateful is an Emitter option which makes the eventbus channel
|
||||
// 'remember' last event sent, and when a new subscriber joins the
|
||||
// bus, the remembered event is immediately sent to the subscription
|
||||
// channel.
|
||||
//
|
||||
// This allows to provide state tracking for dynamic systems, and/or
|
||||
// allows new subscribers to verify that there are Emitters on the channel
|
||||
func Stateful(s interface{}) error {
|
||||
s.(*emitterSettings).makeStateful = true
|
||||
return nil
|
||||
}
|
||||
3
version.json
Normal file
3
version.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"version": "v0.2.1"
|
||||
}
|
||||
Reference in New Issue
Block a user