forked from eclipse-paho/paho.mqtt.golang
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwatchdog.go
More file actions
39 lines (33 loc) · 1.08 KB
/
watchdog.go
File metadata and controls
39 lines (33 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package mqtt
import (
"errors"
"time"
)
// ackWatchdog monitors the packet sent to the broker that expects a response
// triggers a reconnection if no response is received within the timeout period.
func ackWatchdog(c *client, ackTimeout time.Duration) {
defer c.workers.Done()
ticker := time.NewTicker(ackTimeout / 2)
defer ticker.Stop()
DEBUG.Println(NET, "ack watchdog started")
c.logger.Debug("ack watchdog started", componentAttr(NET))
for {
select {
case <-c.stop:
DEBUG.Println(NET, "ack watchdog stopping")
c.logger.Debug("ack watchdog stopping", componentAttr(NET))
return
case <-ticker.C:
fastReconnectTimeVal := c.fastReconnectCheckStartTime.Load().(time.Time)
lastReceivedTimeVal := c.lastReceived.Load().(time.Time)
if fastReconnectTimeVal.After(lastReceivedTimeVal) {
if time.Since(fastReconnectTimeVal) >= ackTimeout {
ERROR.Println(NET, "ack watchdog timeout detected")
c.logger.Error("ack watchdog timeout detected", componentAttr(NET))
c.internalConnLost(errors.New("ack watchdog timeout detected"))
return
}
}
}
}
}