Skip to content

Commit ac29fdf

Browse files
committed
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 <[email protected]>
1 parent 3ec7db3 commit ac29fdf

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

client.go

+16-21
Original file line numberDiff line numberDiff line change
@@ -456,31 +456,26 @@ func (c *client) Disconnect(quiesce uint) {
456456

457457
status := atomic.LoadUint32(&c.status)
458458
c.setConnected(disconnected)
459-
if status == connected {
460-
DEBUG.Println(CLI, "disconnecting")
461459

462-
dm := packets.NewControlPacket(packets.Disconnect).(*packets.DisconnectPacket)
463-
dt := newToken(packets.Disconnect)
464-
disconnectSent := false
465-
select {
466-
case c.oboundP <- &PacketAndToken{p: dm, t: dt}:
467-
disconnectSent = true
468-
case <-c.commsStopped:
469-
WARN.Println("Disconnect packet could not be sent because comms stopped")
470-
case <-time.After(time.Duration(quiesce) * time.Millisecond):
471-
WARN.Println("Disconnect packet not sent due to timeout")
472-
}
473-
474-
// wait for work to finish, or quiesce time consumed
475-
if disconnectSent {
476-
DEBUG.Println(CLI, "calling WaitTimeout")
477-
dt.WaitTimeout(time.Duration(quiesce) * time.Millisecond)
478-
DEBUG.Println(CLI, "WaitTimeout done")
479-
}
480-
} else {
460+
if status != connected {
481461
WARN.Println(CLI, "Disconnect() called but not connected (disconnected/reconnecting)")
462+
return
482463
}
483464

465+
DEBUG.Println(CLI, "disconnecting")
466+
dm := packets.NewControlPacket(packets.Disconnect).(*packets.DisconnectPacket)
467+
dt := newToken(packets.Disconnect)
468+
select {
469+
case c.oboundP <- &PacketAndToken{p: dm, t: dt}:
470+
// wait for work to finish, or quiesce time consumed
471+
DEBUG.Println(CLI, "calling WaitTimeout")
472+
dt.WaitTimeout(time.Duration(quiesce) * time.Millisecond)
473+
DEBUG.Println(CLI, "WaitTimeout done")
474+
case <-c.commsStopped:
475+
WARN.Println("Disconnect packet could not be sent because comms stopped")
476+
case <-time.After(time.Duration(quiesce) * time.Millisecond):
477+
WARN.Println("Disconnect packet not sent due to timeout")
478+
}
484479
}
485480

486481
// forceDisconnect will end the connection with the mqtt broker immediately (used for tests only)

0 commit comments

Comments
 (0)