From c0ee174b13e6cb0643a5b62bcfb4e4998751d701 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Tue, 9 Nov 2021 14:48:25 -0500 Subject: [PATCH] Make publishing asynchronous --- server/topic.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/server/topic.go b/server/topic.go index 881a28bc..fb0ecac4 100644 --- a/server/topic.go +++ b/server/topic.go @@ -28,6 +28,7 @@ func newTopic(id string, last time.Time) *topic { } } +// Subscribe subscribes to this topic func (t *topic) Subscribe(s subscriber) int { t.mu.Lock() defer t.mu.Unlock() @@ -37,24 +38,29 @@ func (t *topic) Subscribe(s subscriber) int { return subscriberID } +// Unsubscribe removes the subscription from the list of subscribers func (t *topic) Unsubscribe(id int) { t.mu.Lock() defer t.mu.Unlock() delete(t.subscribers, id) } +// Publish asynchronously publishes to all subscribers func (t *topic) Publish(m *message) error { - t.mu.Lock() - defer t.mu.Unlock() - t.last = time.Now() - for _, s := range t.subscribers { - if err := s(m); err != nil { - log.Printf("error publishing message to subscriber") + go func() { + t.mu.Lock() + defer t.mu.Unlock() + t.last = time.Now() + for _, s := range t.subscribers { + if err := s(m); err != nil { + log.Printf("error publishing message to subscriber") + } } - } + }() return nil } +// Subscribers returns the number of subscribers to this topic func (t *topic) Subscribers() int { t.mu.Lock() defer t.mu.Unlock()