mirror of
https://github.com/fosrl/newt.git
synced 2026-03-05 18:26:42 +00:00
Add version and send it down
This commit is contained in:
@@ -47,6 +47,8 @@ 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
|
||||||
|
configVersionMux sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientOption func(*Client)
|
type ClientOption func(*Client)
|
||||||
@@ -154,6 +156,22 @@ func (c *Client) GetServerVersion() string {
|
|||||||
return c.serverVersion
|
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
|
// Connect establishes the WebSocket connection
|
||||||
func (c *Client) Connect() error {
|
func (c *Client) Connect() error {
|
||||||
go c.connectWithRetry()
|
go c.connectWithRetry()
|
||||||
@@ -653,12 +671,22 @@ func (c *Client) pingMonitor() {
|
|||||||
if c.conn == nil {
|
if c.conn == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Send application-level ping with config version
|
||||||
|
pingMsg := WSMessage{
|
||||||
|
Type: "ping",
|
||||||
|
Data: map[string]interface{}{
|
||||||
|
"configVersion": c.GetConfigVersion(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
c.writeMux.Lock()
|
c.writeMux.Lock()
|
||||||
err := c.conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(c.pingTimeout))
|
err := c.conn.WriteJSON(pingMsg)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
telemetry.IncWSMessage(c.metricsContext(), "out", "ping")
|
telemetry.IncWSMessage(c.metricsContext(), "out", "ping")
|
||||||
}
|
}
|
||||||
c.writeMux.Unlock()
|
c.writeMux.Unlock()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Check if we're shutting down before logging error and reconnecting
|
// Check if we're shutting down before logging error and reconnecting
|
||||||
select {
|
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()
|
c.handlersMux.RLock()
|
||||||
if handler, ok := c.handlers[msg.Type]; ok {
|
if handler, ok := c.handlers[msg.Type]; ok {
|
||||||
handler(msg)
|
handler(msg)
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ type TokenResponse struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type WSMessage struct {
|
type WSMessage struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Data interface{} `json:"data"`
|
Data interface{} `json:"data"`
|
||||||
|
ConfigVersion int64 `json:"configVersion,omitempty"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user