Skip to content

Commit

Permalink
Fix datarace while update a channel.lastEventID
Browse files Browse the repository at this point in the history
Protect the option by wrappring into a mutex. The datarace is gone.
It closes alexandrevicenzi#18
  • Loading branch information
iscander committed Jul 5, 2021
1 parent 562d884 commit 58741aa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 2 additions & 0 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
5 changes: 3 additions & 2 deletions sse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down

0 comments on commit 58741aa

Please sign in to comment.