-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconnection_manager.go
176 lines (136 loc) · 3.29 KB
/
reconnection_manager.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package pubnub
import (
"log"
"math"
"sync"
"time"
)
const (
RECONNECTION_INTERVAL = 3
RECONNECTION_MINEXPONENTIALBACKOFF = 1
RECONNECTION_MAXEXPONENTIALBACKOFF = 32
)
type ReconnectionManager struct {
sync.RWMutex
timerMutex sync.RWMutex
ExponentialMultiplier int
FailedCalls int
Milliseconds int
OnReconnection func()
OnMaxReconnectionExhaustion func()
DoneTimer chan bool
Timer *time.Ticker
pubnub *PubNub
}
func newReconnectionManager(pubnub *PubNub) *ReconnectionManager {
manager := &ReconnectionManager{}
manager.pubnub = pubnub
manager.ExponentialMultiplier = 1
manager.FailedCalls = 0
manager.Milliseconds = 1000
return manager
}
func (m *ReconnectionManager) HandleReconnection(handler func()) {
m.Lock()
m.OnReconnection = handler
m.Unlock()
}
func (m *ReconnectionManager) HandleOnMaxReconnectionExhaustion(handler func()) {
m.Lock()
m.OnMaxReconnectionExhaustion = handler
m.Unlock()
}
func (m *ReconnectionManager) startPolling() {
if m.pubnub.Config.PNReconnectionPolicy == PNNonePolicy {
log.Println("reconnection policy is disabled, please handle reconnection manually")
return
}
m.Lock()
m.ExponentialMultiplier = 1
m.FailedCalls = 0
m.Unlock()
m.registerHeartbeatTimer()
}
func (m *ReconnectionManager) registerHeartbeatTimer() {
m.stopHeartbeatTimer()
if m.pubnub.Config.PNReconnectionPolicy == PNNonePolicy {
log.Println("Reconnection policy is disabled, please handle reconnection manually.")
return
}
maxRetries := m.pubnub.Config.MaximumReconnectionRetries
m.RLock()
failedCalls := m.FailedCalls
m.RUnlock()
if maxRetries != -1 && failedCalls >= maxRetries {
go m.OnMaxReconnectionExhaustion()
return
}
timerInterval := RECONNECTION_INTERVAL
if m.pubnub.Config.PNReconnectionPolicy == PNExponentialPolicy {
timerInterval = int(math.Pow(2, float64(m.ExponentialMultiplier)) - 1)
if timerInterval > RECONNECTION_MAXEXPONENTIALBACKOFF {
timerInterval = RECONNECTION_MINEXPONENTIALBACKOFF
m.Lock()
m.ExponentialMultiplier = 1
m.Unlock()
// TODO: add timestamp
log.Printf("timerInterval > MAXEXPONENTIALBACKOFF at: \n")
} else {
timerInterval = RECONNECTION_MINEXPONENTIALBACKOFF
}
// log.debug("timerInterval = " + String.valueOf(timerInterval) + " at: " +
// Calendar.getInstance().getTime().toString());
}
if m.pubnub.Config.PNReconnectionPolicy == PNLinearPolicy {
timerInterval = RECONNECTION_INTERVAL
}
m.Lock()
m.Timer = time.NewTicker(time.Duration(timerInterval) * time.Second)
m.Unlock()
go func() {
// Lock??
if m.Timer == nil {
return
}
m.Lock()
timer := m.Timer.C
doneT := m.DoneTimer
m.Unlock()
for {
select {
case <-m.pubnub.context.Done():
return
case <-timer:
go m.callTime()
case <-doneT:
return
}
}
}()
}
func (m *ReconnectionManager) stopHeartbeatTimer() {
m.timerMutex.Lock()
if m.Timer != nil {
m.Timer.Stop()
}
if m.DoneTimer != nil {
m.DoneTimer <- true
}
m.timerMutex.Unlock()
}
func (m *ReconnectionManager) callTime() {
_, status, err := m.pubnub.Time().Execute()
if err != nil {
return
}
if status.Error == nil {
m.stopHeartbeatTimer()
m.OnReconnection()
return
}
m.Lock()
m.ExponentialMultiplier++
m.FailedCalls++
m.Unlock()
m.registerHeartbeatTimer()
}