From ac29fdf5dbd9840c81414431062955c4ae3d1cef Mon Sep 17 00:00:00 2001 From: Paulo Neves Date: Fri, 11 Feb 2022 15:29:05 +0100 Subject: [PATCH] client: localized disconnectSent logic the diconnectSent logic was being used in it's own conditional when it naturally is not conditional when the writing to c.ouboundP is successful. Signed-off-by: Paulo Neves --- client.go | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/client.go b/client.go index 84b9b88..4c869f1 100644 --- a/client.go +++ b/client.go @@ -456,31 +456,26 @@ func (c *client) Disconnect(quiesce uint) { status := atomic.LoadUint32(&c.status) c.setConnected(disconnected) - if status == connected { - DEBUG.Println(CLI, "disconnecting") - dm := packets.NewControlPacket(packets.Disconnect).(*packets.DisconnectPacket) - dt := newToken(packets.Disconnect) - disconnectSent := false - select { - case c.oboundP <- &PacketAndToken{p: dm, t: dt}: - disconnectSent = true - case <-c.commsStopped: - WARN.Println("Disconnect packet could not be sent because comms stopped") - case <-time.After(time.Duration(quiesce) * time.Millisecond): - WARN.Println("Disconnect packet not sent due to timeout") - } - - // wait for work to finish, or quiesce time consumed - if disconnectSent { - DEBUG.Println(CLI, "calling WaitTimeout") - dt.WaitTimeout(time.Duration(quiesce) * time.Millisecond) - DEBUG.Println(CLI, "WaitTimeout done") - } - } else { + if status != connected { WARN.Println(CLI, "Disconnect() called but not connected (disconnected/reconnecting)") + return } + DEBUG.Println(CLI, "disconnecting") + dm := packets.NewControlPacket(packets.Disconnect).(*packets.DisconnectPacket) + dt := newToken(packets.Disconnect) + select { + case c.oboundP <- &PacketAndToken{p: dm, t: dt}: + // wait for work to finish, or quiesce time consumed + DEBUG.Println(CLI, "calling WaitTimeout") + dt.WaitTimeout(time.Duration(quiesce) * time.Millisecond) + DEBUG.Println(CLI, "WaitTimeout done") + case <-c.commsStopped: + WARN.Println("Disconnect packet could not be sent because comms stopped") + case <-time.After(time.Duration(quiesce) * time.Millisecond): + WARN.Println("Disconnect packet not sent due to timeout") + } } // forceDisconnect will end the connection with the mqtt broker immediately (used for tests only)