forked from eclipse/paho.golang
/
pinger.go
122 lines (112 loc) · 3.3 KB
/
pinger.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package paho
import (
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"github.com/Lorderot/paho.golang/packets"
)
// PingFailHandler is a type for the function that is invoked
// when we have sent a Pingreq to the server and not received
// a Pingresp within 1.5x our pingtimeout
type PingFailHandler func(error)
// Pinger is an interface of the functions for a struct that is
// used to manage sending PingRequests and responding to
// PingResponses
// Start() takes a net.Conn which is a connection over which an
// MQTT session has already been established, and a time.Duration
// of the keepalive setting passed to the server when the MQTT
// session was established.
// Stop() is used to stop the Pinger
// PingResp() is the function that is called by the Client when
// a PingResponse is received
// SetDebug() is used to pass in a Logger to be used to log debug
// information, for example sharing a logger with the main client
type Pinger interface {
Start(net.Conn, time.Duration)
Stop()
PingResp()
SetDebug(Logger)
}
// PingHandler is the library provided default Pinger
type PingHandler struct {
mu sync.Mutex
lastPing time.Time
conn net.Conn
stop chan struct{}
pingFailHandler PingFailHandler
pingOutstanding int32
debug Logger
}
// DefaultPingerWithCustomFailHandler returns an instance of the
// default Pinger but with a custom PingFailHandler that is called
// when the client has not received a response to a PingRequest
// within the appropriate amount of time
func DefaultPingerWithCustomFailHandler(pfh PingFailHandler) *PingHandler {
return &PingHandler{
pingFailHandler: pfh,
debug: NOOPLogger{},
}
}
// Start is the library provided Pinger's implementation of
// the required interface function()
func (p *PingHandler) Start(c net.Conn, pt time.Duration) {
p.mu.Lock()
p.conn = c
p.stop = make(chan struct{})
p.mu.Unlock()
checkTicker := time.NewTicker(pt / 4)
defer checkTicker.Stop()
for {
select {
case <-p.stop:
return
case <-checkTicker.C:
if atomic.LoadInt32(&p.pingOutstanding) > 0 && time.Since(p.lastPing) > (pt+pt>>1) {
p.pingFailHandler(fmt.Errorf("ping resp timed out"))
//ping outstanding and not reset in 1.5 times ping timer
return
}
if time.Since(p.lastPing) >= pt {
//time to send a ping
if _, err := packets.NewControlPacket(packets.PINGREQ).WriteTo(p.conn); err != nil {
if p.pingFailHandler != nil {
p.pingFailHandler(err)
}
return
}
atomic.AddInt32(&p.pingOutstanding, 1)
p.lastPing = time.Now()
p.debug.Println("pingHandler sending ping request")
}
}
}
}
// Stop is the library provided Pinger's implementation of
// the required interface function()
func (p *PingHandler) Stop() {
p.mu.Lock()
defer p.mu.Unlock()
if p.stop == nil {
return
}
p.debug.Println("pingHandler stopping")
select {
case <-p.stop:
//Already stopped, do nothing
default:
close(p.stop)
}
}
// PingResp is the library provided Pinger's implementation of
// the required interface function()
func (p *PingHandler) PingResp() {
p.debug.Println("pingHandler resetting pingOutstanding")
atomic.StoreInt32(&p.pingOutstanding, 0)
}
// SetDebug sets the logger l to be used for printing debug
// information for the pinger
func (p *PingHandler) SetDebug(l Logger) {
p.debug = l
}