Dont start the ping until we are connected

This commit is contained in:
Owen
2026-01-17 17:32:01 -08:00
parent 9d77a1daf7
commit 43c8a14fda
3 changed files with 29 additions and 2 deletions

View File

@@ -198,6 +198,9 @@ func (o *Olm) handleConnect(msg websocket.WSMessage) {
o.connected = true
// Start ping monitor now that we are registered and connected
o.websocket.StartPingMonitor()
// Invoke onConnected callback if configured
if o.olmConfig.OnConnected != nil {
go o.olmConfig.OnConnected()

View File

@@ -362,6 +362,8 @@ func (o *Olm) StartTunnel(config TunnelConfig) {
if o.connected {
logger.Debug("Already connected, skipping registration")
// Restart ping monitor on reconnect since the old one would have exited
o.websocket.StartPingMonitor()
return nil
}

View File

@@ -100,6 +100,8 @@ type Client struct {
processingMux sync.RWMutex // Protects processingMessage
processingWg sync.WaitGroup // WaitGroup to wait for message processing to complete
getPingData func() map[string]any // Callback to get additional ping data
pingStarted bool // Flag to track if ping monitor has been started
pingStartedMux sync.Mutex // Protects pingStarted
}
type ClientOption func(*Client)
@@ -575,8 +577,14 @@ func (c *Client) establishConnection() error {
c.conn = conn
c.setConnected(true)
// Start the ping monitor
go c.pingMonitor()
// Reset ping started flag on new connection
c.pingStartedMux.Lock()
c.pingStarted = false
c.pingStartedMux.Unlock()
// Note: ping monitor is NOT started here - it will be started when
// StartPingMonitor() is called after registration completes
// Start the read pump with disconnect detection
go c.readPumpWithDisconnectDetection()
@@ -715,6 +723,20 @@ func (c *Client) pingMonitor() {
}
}
// StartPingMonitor starts the ping monitor goroutine.
// This should be called after the client is registered and connected.
// It is safe to call multiple times - only the first call will start the monitor.
func (c *Client) StartPingMonitor() {
c.pingStartedMux.Lock()
defer c.pingStartedMux.Unlock()
if c.pingStarted {
return
}
c.pingStarted = true
go c.pingMonitor()
}
// GetConfigVersion returns the current config version
func (c *Client) GetConfigVersion() int {
c.configVersionMux.RLock()