-
Notifications
You must be signed in to change notification settings - Fork 0
/
c_keepalive.go
73 lines (65 loc) · 1.57 KB
/
c_keepalive.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
package upax_go
// upax_go/c_keepalive.go
import (
xu "github.com/jddixon/xlUtil_go"
"time"
)
// Send keepalives at the specified interval. If the lifespan is
// greater than 0, do this that many times and then halt. Otherwise
// do this forever.
//
type ClientKeepAliveMgr struct {
Interval time.Duration
lifespan int
soFar int
MsgCh chan *UpaxClientMsg
StopCh chan bool
DoneCh chan bool
}
// Send keepalive messages to msgCh at the specified interval.
// The interval should be expressed in units of time, but the lifespan
// is the number of intervals, except that if the lifespan is less
// than or equal to zero, the lifespan is effectively infinite.
//
func NewClientKeepAliveMgr(interval time.Duration, lifespan int,
msgCh chan *UpaxClientMsg, stopCh, doneCh chan bool) (
mgr *ClientKeepAliveMgr, err error) {
if msgCh == nil {
err = NilMsgCh
} else {
if lifespan <= 0 {
lifespan = xu.MAX_INT
}
mgr = &ClientKeepAliveMgr{
Interval: interval,
lifespan: lifespan,
MsgCh: msgCh,
StopCh: stopCh,
DoneCh: doneCh}
}
return
}
func (mgr *ClientKeepAliveMgr) Run() {
running := true
for running {
select {
case <-time.After(mgr.Interval):
op := UpaxClientMsg_KeepAlive
msgOut := &UpaxClientMsg{
Op: &op,
// MsgN needs to be assigned when the message is
// actually sent.
}
mgr.MsgCh <- msgOut
mgr.soFar++
if mgr.soFar >= mgr.lifespan {
running = false
}
case <-mgr.StopCh:
running = false
}
}
// without a delay we may lose the last keepalive)
time.Sleep(mgr.Interval)
mgr.DoneCh <- true
}