From f8a157606c870ba3f5dfbb66010515b9bbdba592 Mon Sep 17 00:00:00 2001 From: Yourim Cha Date: Tue, 23 May 2023 16:58:30 +0900 Subject: [PATCH] Resolve concurrent map issue --- server/backend/sync/memory/coordinator.go | 5 +---- server/backend/sync/memory/pubsub.go | 12 ++++++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/server/backend/sync/memory/coordinator.go b/server/backend/sync/memory/coordinator.go index 8ab03a19a..9b08a1cc1 100644 --- a/server/backend/sync/memory/coordinator.go +++ b/server/backend/sync/memory/coordinator.go @@ -65,10 +65,7 @@ func (c *Coordinator) Subscribe( return nil, nil, err } - var peers []types.Client - for _, sub := range c.pubSub.subscriptionsMapByDocID[documentID].Map() { - peers = append(peers, sub.Subscriber()) - } + peers := c.pubSub.GetPeers(documentID) return sub, peers, nil } diff --git a/server/backend/sync/memory/pubsub.go b/server/backend/sync/memory/pubsub.go index b736e457c..733a15eec 100644 --- a/server/backend/sync/memory/pubsub.go +++ b/server/backend/sync/memory/pubsub.go @@ -201,6 +201,18 @@ func (m *PubSub) Publish( } } +// GetPeers returns the peers of the given document. +func (m *PubSub) GetPeers(documentID types.ID) []types.Client { + m.subscriptionsMapMu.RLock() + defer m.subscriptionsMapMu.RUnlock() + + var peers []types.Client + for _, sub := range m.subscriptionsMapByDocID[documentID].Map() { + peers = append(peers, sub.Subscriber()) + } + return peers +} + // UpdatePresence updates the presence of the given client. func (m *PubSub) UpdatePresence( publisher *types.Client,