Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eventbus): warn applications whenever events aren't read #2026

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion p2p/host/eventbus/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import (
"reflect"
"sync"
"sync/atomic"
"time"

logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/event"
)

var log = logging.Logger("eventbus")

// /////////////////////
// BUS

Expand Down Expand Up @@ -347,7 +351,27 @@ func (n *node) emit(evt interface{}) {
}

for _, ch := range n.sinks {
ch <- evt
select {
case ch <- evt:
default:
n.emitWithWarn(evt, ch)
}
}
n.lk.Unlock()
}

func (n *node) emitWithWarn(evt any, sink chan any) {
// warn periodically, otherwise a single log may get lost
t := time.NewTicker(time.Millisecond * 100)
defer t.Stop()

for {
select {
case sink <- evt:
return
case <-t.C:
// warn node operator or dev about slow event consumption by application
log.Warn("SLOW EVENT CONSUMER OF TYPE %s", n.typ)
}
}
}