From 58741aa877bcb591c8e0e68b083d04d7c673a4b5 Mon Sep 17 00:00:00 2001 From: Oleksandr Ohurtsov Date: Mon, 5 Jul 2021 13:44:36 +0300 Subject: [PATCH] Fix datarace while update a channel.lastEventID Protect the option by wrappring into a mutex. The datarace is gone. It closes https://github.com/alexandrevicenzi/go-sse/issues/18 --- channel.go | 2 ++ sse_test.go | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/channel.go b/channel.go index 82be419..c66f948 100644 --- a/channel.go +++ b/channel.go @@ -23,7 +23,9 @@ func newChannel(name string) *Channel { // SendMessage broadcast a message to all clients in a channel. func (c *Channel) SendMessage(message *Message) { + c.mu.Lock() c.lastEventID = message.id + c.mu.Unlock() c.mu.RLock() diff --git a/sse_test.go b/sse_test.go index e632ca6..63634fc 100644 --- a/sse_test.go +++ b/sse_test.go @@ -90,8 +90,9 @@ func TestServer(t *testing.T) { } func TestMultipleTopics(t *testing.T) { - // usage pattern we have a client which subsribed to multiple channels - // in one connection + // The usage pattern: Imagine we have a client which subsribed to multiple topics + // inside a connection. It track changes of specific items state by their ID + // for example. sendersWg := sync.WaitGroup{} workerWg := sync.WaitGroup{} m := sync.Mutex{}