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.
  • Loading branch information
ptsneves committed Feb 11, 2022
1 parent e8ce195 commit f574bf2
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions client.go
Expand Up @@ -456,31 +456,25 @@ func (c *client) Disconnect(quiesce uint) {
c.setConnected(disconnected)

status := atomic.LoadUint32(&c.status)
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 f574bf2

Please sign in to comment.