From 9cee8ab8887dd24c2f1e89a5e4125c5d2c24bf80 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Wed, 22 Jun 2022 13:52:49 -0400 Subject: [PATCH] Call subscriber funtions in individual goroutines --- server/topic.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/server/topic.go b/server/topic.go index 8ce7953..3bc7473 100644 --- a/server/topic.go +++ b/server/topic.go @@ -50,9 +50,13 @@ func (t *topic) Publish(v *visitor, m *message) error { if len(subscribers) > 0 { log.Debug("%s Forwarding to %d subscriber(s)", logMessagePrefix(v, m), len(subscribers)) for _, s := range subscribers { - if err := s(v, m); err != nil { - log.Warn("%s Error forwarding to subscriber", logMessagePrefix(v, m)) - } + // We call the subscriber functions in their own Go routines because they are blocking, and + // 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 { log.Trace("%s No stream or WebSocket subscribers, not forwarding", logMessagePrefix(v, m))