Call subscriber funtions in individual goroutines

This commit is contained in:
Philipp Heckel 2022-06-22 13:52:49 -04:00
parent ed9d99fd57
commit 9cee8ab888

View file

@ -50,9 +50,13 @@ func (t *topic) Publish(v *visitor, m *message) error {
if len(subscribers) > 0 { if len(subscribers) > 0 {
log.Debug("%s Forwarding to %d subscriber(s)", logMessagePrefix(v, m), len(subscribers)) log.Debug("%s Forwarding to %d subscriber(s)", logMessagePrefix(v, m), len(subscribers))
for _, s := range subscribers { for _, s := range subscribers {
if err := s(v, m); err != nil { // We call the subscriber functions in their own Go routines because they are blocking, and
log.Warn("%s Error forwarding to subscriber", logMessagePrefix(v, m)) // we don't want individual slow subscribers to be able to block others.
} go func(s subscriber) {
if err := s(v, m); err != nil {
log.Warn("%s Error forwarding to subscriber", logMessagePrefix(v, m))
}
}(s)
} }
} else { } else {
log.Trace("%s No stream or WebSocket subscribers, not forwarding", logMessagePrefix(v, m)) log.Trace("%s No stream or WebSocket subscribers, not forwarding", logMessagePrefix(v, m))