Skip to content

Commit

Permalink
client: localized disconnectSent logic
Browse files Browse the repository at this point in the history
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 <ptsneves@gmail.com>
  • Loading branch information
ptsneves committed Mar 10, 2022
1 parent 3ec7db3 commit ac29fdf
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions client.go
Expand Up @@ -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)
Expand Down

0 comments on commit ac29fdf

Please sign in to comment.