Skip to content

Commit d0120ee

Browse files
author
Daniele Vasselli
committed
Replace the time.After with the timer for efficiency.
Signed-off-by: Daniele Vasselli <[email protected]>
1 parent f21bdb1 commit d0120ee

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,9 +799,15 @@ func (c *client) Publish(topic string, qos byte, retained bool, payload interfac
799799
if publishWaitTimeout == 0 {
800800
publishWaitTimeout = time.Second * 30
801801
}
802+
t := time.NewTimer(publishWaitTimeout)
803+
defer func() {
804+
if !t.Stop() {
805+
<-t.C
806+
}
807+
}()
802808
select {
803809
case c.obound <- &PacketAndToken{p: pub, t: token}:
804-
case <-time.After(publishWaitTimeout):
810+
case <-t.C:
805811
token.setError(errors.New("publish was broken by timeout"))
806812
}
807813
}

0 commit comments

Comments
 (0)