From cc23e5bccc676de5af2e5833850375381864f6bf Mon Sep 17 00:00:00 2001 From: Owen Date: Fri, 19 Dec 2025 16:45:54 -0500 Subject: [PATCH] Add version and send it down --- websocket/client.go | 35 ++++++++++++++++++++++++++++++++++- websocket/types.go | 5 +++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/websocket/client.go b/websocket/client.go index da1fa88..c0fea18 100644 --- a/websocket/client.go +++ b/websocket/client.go @@ -47,6 +47,8 @@ type Client struct { metricsCtx context.Context configNeedsSave bool // Flag to track if config needs to be saved serverVersion string + configVersion int64 // Latest config version received from server + configVersionMux sync.RWMutex } type ClientOption func(*Client) @@ -154,6 +156,22 @@ func (c *Client) GetServerVersion() string { return c.serverVersion } +// GetConfigVersion returns the latest config version received from server +func (c *Client) GetConfigVersion() int64 { + c.configVersionMux.RLock() + defer c.configVersionMux.RUnlock() + return c.configVersion +} + +// setConfigVersion updates the config version if the new version is higher +func (c *Client) setConfigVersion(version int64) { + c.configVersionMux.Lock() + defer c.configVersionMux.Unlock() + if version > c.configVersion { + c.configVersion = version + } +} + // Connect establishes the WebSocket connection func (c *Client) Connect() error { go c.connectWithRetry() @@ -653,12 +671,22 @@ func (c *Client) pingMonitor() { if c.conn == nil { return } + + // Send application-level ping with config version + pingMsg := WSMessage{ + Type: "ping", + Data: map[string]interface{}{ + "configVersion": c.GetConfigVersion(), + }, + } + c.writeMux.Lock() - err := c.conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(c.pingTimeout)) + err := c.conn.WriteJSON(pingMsg) if err == nil { telemetry.IncWSMessage(c.metricsContext(), "out", "ping") } c.writeMux.Unlock() + if err != nil { // Check if we're shutting down before logging error and reconnecting select { @@ -737,6 +765,11 @@ func (c *Client) readPumpWithDisconnectDetection(started time.Time) { } } + // Extract and update config version from message if present + if msg.ConfigVersion > 0 { + c.setConfigVersion(msg.ConfigVersion) + } + c.handlersMux.RLock() if handler, ok := c.handlers[msg.Type]; ok { handler(msg) diff --git a/websocket/types.go b/websocket/types.go index 1196d64..381f7a1 100644 --- a/websocket/types.go +++ b/websocket/types.go @@ -17,6 +17,7 @@ type TokenResponse struct { } type WSMessage struct { - Type string `json:"type"` - Data interface{} `json:"data"` + Type string `json:"type"` + Data interface{} `json:"data"` + ConfigVersion int64 `json:"configVersion,omitempty"` }