Mutex on handlers, slight change to ping message and handler

This commit is contained in:
Owen
2026-01-15 21:33:11 -08:00
parent 65287b82e1
commit c1cad78e04

View File

@@ -47,8 +47,11 @@ type Client struct {
metricsCtx context.Context metricsCtx context.Context
configNeedsSave bool // Flag to track if config needs to be saved configNeedsSave bool // Flag to track if config needs to be saved
serverVersion string serverVersion string
configVersion int64 // Latest config version received from server configVersion int64 // Latest config version received from server
configVersionMux sync.RWMutex configVersionMux sync.RWMutex
processingMessage bool // Flag to track if a message is currently being processed
processingMux sync.RWMutex // Protects processingMessage
processingWg sync.WaitGroup // WaitGroup to wait for message processing to complete
} }
type ClientOption func(*Client) type ClientOption func(*Client)
@@ -163,13 +166,11 @@ func (c *Client) GetConfigVersion() int64 {
return c.configVersion return c.configVersion
} }
// setConfigVersion updates the config version if the new version is higher // setConfigVersion updates the config version
func (c *Client) setConfigVersion(version int64) { func (c *Client) setConfigVersion(version int64) {
c.configVersionMux.Lock() c.configVersionMux.Lock()
defer c.configVersionMux.Unlock() defer c.configVersionMux.Unlock()
if version > c.configVersion { c.configVersion = version
c.configVersion = version
}
} }
// Connect establishes the WebSocket connection // Connect establishes the WebSocket connection
@@ -672,12 +673,21 @@ func (c *Client) pingMonitor() {
return return
} }
// Skip ping if a message is currently being processed
c.processingMux.RLock()
isProcessing := c.processingMessage
c.processingMux.RUnlock()
if isProcessing {
logger.Debug("Skipping ping, message is being processed")
continue
}
c.configVersionMux.RLock() c.configVersionMux.RLock()
configVersion := c.configVersion configVersion := c.configVersion
c.configVersionMux.RUnlock() c.configVersionMux.RUnlock()
pingMsg := WSMessage{ pingMsg := WSMessage{
Type: "ping", Type: "newt/ping",
Data: map[string]interface{}{}, Data: map[string]interface{}{},
ConfigVersion: configVersion, ConfigVersion: configVersion,
} }
@@ -767,14 +777,24 @@ func (c *Client) readPumpWithDisconnectDetection(started time.Time) {
} }
} }
// Extract and update config version from message if present // Update config version from incoming message
if msg.ConfigVersion > 0 { c.setConfigVersion(msg.ConfigVersion)
c.setConfigVersion(msg.ConfigVersion)
}
c.handlersMux.RLock() c.handlersMux.RLock()
if handler, ok := c.handlers[msg.Type]; ok { if handler, ok := c.handlers[msg.Type]; ok {
// Mark that we're processing a message
c.processingMux.Lock()
c.processingMessage = true
c.processingMux.Unlock()
c.processingWg.Add(1)
handler(msg) handler(msg)
// Mark that we're done processing
c.processingWg.Done()
c.processingMux.Lock()
c.processingMessage = false
c.processingMux.Unlock()
} }
c.handlersMux.RUnlock() c.handlersMux.RUnlock()
} }