fix(concurrency): guard the three shared maps with a mutex
clientPointer, myClientPointer and killChannel are created once at startup and
passed by reference into eleven packages. All of them read and wrote those plain
Go maps from different goroutines, with no synchronisation.
Go does not tolerate that: the runtime kills the process with
fatal error: concurrent map writes
and a fatal error is not recoverable — a deferred recover() does not catch it.
Measured in production: the process died inside StartClient while five instances
were connecting at once. The orchestrator restarted it within seconds, but every
number was offline in the meantime, and it repeats on any restart that brings up
more than a couple of instances together.
The three maps become safemap.Map[T], a generic wrapper over a RWMutex. It lives
in its own package because MyClient is defined in pkg/whatsmeow/service, and
putting the type there would create an import cycle with the ten packages that
receive the maps.
98 call sites changed across 12 files, with the compiler as the judge: anything
missed is a build error, not a silent bug.
Tests: with -race, a plain map running the same exercise reports DATA RACE and
fails, while safemap.Map passes. Both halves verified.
(cherry picked from commit 3860fa278dc2b901705ffbddbed5cf59d70a85e4)
This commit is contained in:
1 parent
a5f7e1c98a
commit
0c4ac7d9a9
14 files changed
+311
-129
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package chat_service
|
||||
|
||||
import (
|
||||
"github.com/evolution-foundation/evolution-go/pkg/safemap"
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
@@ -25,7 +26,7 @@ type ChatService interface {
|
||||
}
|
||||
|
||||
type chatService struct {
|
||||
clientPointer map[string]*whatsmeow.Client
|
||||
clientPointer *safemap.Map[*whatsmeow.Client]
|
||||
whatsmeowService whatsmeow_service.WhatsmeowService
|
||||
loggerWrapper *logger_wrapper.LoggerManager
|
||||
}
|
||||
@@ -40,7 +41,7 @@ type HistorySyncRequestStruct struct {
|
||||
}
|
||||
|
||||
func (c *chatService) ensureClientConnected(instanceId string) (*whatsmeow.Client, error) {
|
||||
client := c.clientPointer[instanceId]
|
||||
client := c.clientPointer.Get(instanceId)
|
||||
c.loggerWrapper.GetLogger(instanceId).LogInfo("[%s] Checking client connection status - Client exists: %v", instanceId, client != nil)
|
||||
|
||||
if client == nil {
|
||||
@@ -54,7 +55,7 @@ func (c *chatService) ensureClientConnected(instanceId string) (*whatsmeow.Clien
|
||||
c.loggerWrapper.GetLogger(instanceId).LogInfo("[%s] Instance started, waiting 2 seconds...", instanceId)
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
client = c.clientPointer[instanceId]
|
||||
client = c.clientPointer.Get(instanceId)
|
||||
c.loggerWrapper.GetLogger(instanceId).LogInfo("[%s] Checking new client - Exists: %v, Connected: %v",
|
||||
instanceId,
|
||||
client != nil,
|
||||
@@ -244,7 +245,7 @@ func (c *chatService) HistorySyncRequest(data *HistorySyncRequestStruct, instanc
|
||||
}
|
||||
|
||||
func NewChatService(
|
||||
clientPointer map[string]*whatsmeow.Client,
|
||||
clientPointer *safemap.Map[*whatsmeow.Client],
|
||||
whatsmeowService whatsmeow_service.WhatsmeowService,
|
||||
loggerWrapper *logger_wrapper.LoggerManager,
|
||||
) ChatService {
|
||||
|
||||
Reference in new issue
Block a user