From 2053473d2707634f39b1c7a750b412e46cb367a1 Mon Sep 17 00:00:00 2001 From: martonp Date: Sat, 14 May 2022 22:33:07 +0700 Subject: [PATCH] Do not update to disconnected if initial connect fails due to invalid cert --- client/comms/wsconn.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/client/comms/wsconn.go b/client/comms/wsconn.go index 0992a9f25d..59a54bf336 100644 --- a/client/comms/wsconn.go +++ b/client/comms/wsconn.go @@ -189,6 +189,21 @@ func (conn *wsConn) setConnectionStatus(status ConnectionStatus) { } } +// setStatusToDisconnectedIfConnected will update the conenction status +// to disconnected if it was previously connected, but it will not overwrite +// another more specific disconnected status, such as InvalidCert. +func (conn *wsConn) setStatusToDisconnectedIfConnected() { + conn.connectedMtx.Lock() + statusChange := conn.connectionStatus == Connected + if statusChange { + conn.connectionStatus = Disconnected + } + conn.connectedMtx.Unlock() + if statusChange && conn.cfg.ConnectEventFunc != nil { + conn.cfg.ConnectEventFunc(Disconnected) + } +} + // connect attempts to establish a websocket connection. func (conn *wsConn) connect(ctx context.Context) error { dialer := &websocket.Dialer{ @@ -427,7 +442,7 @@ func (conn *wsConn) Connect(ctx context.Context) (*sync.WaitGroup, error) { go func() { defer conn.wg.Done() <-ctxInternal.Done() - conn.setConnectionStatus(Disconnected) + conn.setStatusToDisconnectedIfConnected() conn.wsMtx.Lock() if conn.ws != nil { conn.log.Debug("Sending close 1000 (normal) message.")