Skip to content

Commit

Permalink
Replace the time.After with the timer for efficiency.
Browse files Browse the repository at this point in the history
Signed-off-by: Daniele Vasselli <[email protected]>
  • Loading branch information
Daniele Vasselli committed Mar 21, 2024
1 parent f21bdb1 commit d0120ee
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,9 +799,15 @@ func (c *client) Publish(topic string, qos byte, retained bool, payload interfac
if publishWaitTimeout == 0 {
publishWaitTimeout = time.Second * 30
}
t := time.NewTimer(publishWaitTimeout)
defer func() {
if !t.Stop() {
<-t.C
}
}()
select {
case c.obound <- &PacketAndToken{p: pub, t: token}:
case <-time.After(publishWaitTimeout):
case <-t.C:
token.setError(errors.New("publish was broken by timeout"))
}
}
Expand Down

0 comments on commit d0120ee

Please sign in to comment.