-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathheartbeat.go
147 lines (129 loc) · 2.87 KB
/
heartbeat.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package muxado
import (
"encoding/binary"
"io"
"math/rand"
"net"
"time"
)
const (
defaultHeartbeatInterval = 10 * time.Second
defaultHeartbeatTolerance = 15 * time.Second
defaultStreamType StreamType = 0xFFFFFFFF
)
type HeartbeatConfig struct {
Interval time.Duration
Tolerance time.Duration
Type StreamType
}
func NewHeartbeatConfig() *HeartbeatConfig {
return &HeartbeatConfig{
Interval: defaultHeartbeatInterval,
Tolerance: defaultHeartbeatTolerance,
Type: defaultStreamType,
}
}
type Heartbeat struct {
TypedStreamSession
config HeartbeatConfig
closed chan int
cb func(time.Duration)
}
func NewHeartbeat(sess TypedStreamSession, cb func(time.Duration), config *HeartbeatConfig) *Heartbeat {
if config == nil {
config = NewHeartbeatConfig()
}
return &Heartbeat{
TypedStreamSession: sess,
config: *config,
closed: make(chan int, 1),
cb: cb,
}
}
func (h *Heartbeat) Accept() (net.Conn, error) {
return h.AcceptTypedStream()
}
func (h *Heartbeat) AcceptStream() (Stream, error) {
return h.TypedStreamSession.AcceptTypedStream()
}
func (h *Heartbeat) Close() error {
select {
case h.closed <- 1:
default:
}
return h.TypedStreamSession.Close()
}
func (h *Heartbeat) AcceptTypedStream() (TypedStream, error) {
for {
str, err := h.TypedStreamSession.AcceptTypedStream()
if err != nil {
return nil, err
}
if str.StreamType() != h.config.Type {
return str, nil
}
go h.responder(str)
}
}
func (h *Heartbeat) Start() {
mark := make(chan time.Duration)
go h.requester(mark)
go h.check(mark)
}
func (h *Heartbeat) check(mark chan time.Duration) {
t := time.NewTimer(h.config.Interval + h.config.Tolerance)
for {
select {
case <-t.C:
// timed out waiting for a response!
h.cb(0)
case dur := <-mark:
h.cb(dur)
t.Reset(h.config.Interval + h.config.Tolerance)
case <-h.closed:
return
}
}
}
func (h *Heartbeat) requester(mark chan time.Duration) {
// make random number generator
r := rand.New(rand.NewSource(time.Now().Unix()))
// open a new stream for the heartbeat
stream, err := h.OpenTypedStream(h.config.Type)
if err != nil {
return
}
// send heartbeats and then check that we got them back
for {
time.Sleep(h.config.Interval)
start := time.Now()
// assign a new random value to echo
id := uint32(r.Int31())
if err := binary.Write(stream, binary.BigEndian, id); err != nil {
return
}
var respId uint32
if err := binary.Read(stream, binary.BigEndian, &respId); err != nil {
return
}
if id != respId {
return
}
// record the time
mark <- time.Since(start)
}
}
func (h *Heartbeat) responder(s Stream) {
// read the next heartbeat id and respond
buf := make([]byte, 4)
for {
_, err := io.ReadFull(s, buf)
if err != nil {
return
}
_, err = s.Write(buf)
if err != nil {
return
}
}
}