-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpush_service_pool.go
224 lines (188 loc) · 5.37 KB
/
push_service_pool.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package apns
import (
"container/list"
"sync/atomic"
"time"
)
var pushPool *pushServicePool
func initPushService() {
pushPool = &pushServicePool{}
pushPool.poolSize = defaultConfig.MinPoolSize
pushPool.services = &pushServiceList{list.New()}
pushPool.releasedServices = make(chan *pushService, defaultConfig.MaxPoolSize)
pushPool.reportQueue = make(chan *notifReport, defaultConfig.MinPoolSize/2)
pushPool.reportCount = &reportCount{}
for i := uint(0); i < pushPool.poolSize; i++ {
err := pushPool.services.New()
if err != nil {
logerr("Unable to start connection on init %s", err)
continue
}
}
go pushPool.monitor()
logdebug("Connectio pool inited with %d connections from %d", pushPool.services.Len(), pushPool.poolSize)
}
type pushServicePool struct {
poolSize uint
services *pushServiceList
releasedServices chan *pushService
reportQueue chan *notifReport
reportCount *reportCount
upgradePoolCount int
downgradePoolCount int
needToRemoveOne bool
lastFaildRetry time.Time
}
func (psp *pushServicePool) monitor() {
checker := time.Tick(time.Second * 1)
reportTiker := time.Tick(defaultConfig.ReportingInterval)
for {
select {
case <-reportTiker:
psp.report()
case report := <-psp.reportQueue:
psp.addReport(report)
case <-checker:
createNewConnection := false
upgrade, downgrade := psp.checkScale()
if upgrade {
psp.poolSize++
createNewConnection = true
logwarn("Push service pool size will upgrade to: %d", psp.poolSize)
} else if downgrade {
psp.services.Remove(nil)
psp.poolSize--
logwarn("Push service pool size will downgrade to: %d", psp.poolSize)
}
if !createNewConnection && psp.recreateFailed() {
createNewConnection = true
loginfo("A failed push service is going to be revived")
}
if createNewConnection {
err := psp.services.New()
if err != nil {
logerr("Unable to start a new apple connection: %s", err)
break
}
}
case service := <-psp.releasedServices:
if service.lastError != nil {
psp.services.Remove(service)
logwarn("I have a failed push service, it will be removed from pool (%s)", service.lastError)
break
}
}
}
}
func (psp *pushServicePool) recreateFailed() bool {
if psp.services.Len() >= psp.poolSize {
return false
}
if time.Since(psp.lastFaildRetry) >= defaultConfig.ReconnectDistanceTime {
psp.lastFaildRetry = time.Now()
return true
}
return false
}
func (psp *pushServicePool) checkScale() (upgrade, downgrade bool) {
messageQueueSize := atomic.LoadInt64(&messagesInQueue)
if messageQueueSize > int64(defaultConfig.UpgradePool) {
psp.upgradePoolCount++
} else {
psp.upgradePoolCount = 0
}
if messageQueueSize < int64(defaultConfig.DowngradePool) {
psp.downgradePoolCount++
} else {
psp.downgradePoolCount = 0
}
if psp.upgradePoolCount >= 10 { //10 seconds
if psp.poolSize >= defaultConfig.MaxPoolSize {
logwarn("Upgrade pool requested but i'm in max pool size (messages: %d, pool size: %d) ",
messageQueueSize, psp.poolSize)
return false, false
}
return true, false
}
if psp.downgradePoolCount >= 300 { //5 minutes
if psp.poolSize <= defaultConfig.MinPoolSize {
return false, false
}
return false, true
}
return false, false
}
type notifReport struct {
sent int64
resent int64
success int64
failed int64
confirmTime time.Duration
sendingTime time.Duration
}
type reportCount struct {
totalSent int64
totalResent int64
successConfirmed int64
failedConfirmed int64
confirmTime time.Duration
sendingTime time.Duration
}
func (psp *pushServicePool) report() {
format := "APNs Report - ActivePool: %d, PoolSize: %d, Queue: %d, Sent: %d, ReSent: %d, Confirmed: %d, ConfirmedFailed: %d, ConfirmTimeAvg: %s, SendingTime: %s"
queueSize := atomic.LoadInt64(&messagesInQueue)
rc := psp.reportCount
var (
confirmTimeAvg time.Duration = 0
sendingTimeAvg time.Duration = 0
)
if rc.successConfirmed+rc.failedConfirmed > 0 {
confirmTimeAvg = rc.confirmTime / time.Duration(rc.successConfirmed+rc.failedConfirmed)
sendingTimeAvg = rc.confirmTime / time.Duration(rc.successConfirmed+rc.failedConfirmed)
}
loginfo(format, psp.services.Len(), psp.poolSize, queueSize, rc.totalSent, rc.totalResent, rc.successConfirmed, rc.failedConfirmed, confirmTimeAvg, sendingTimeAvg)
psp.reportCount = &reportCount{}
}
func (psp *pushServicePool) addReport(report *notifReport) {
rc := psp.reportCount
rc.totalSent += report.sent
rc.totalResent += report.resent
rc.successConfirmed += report.success
rc.failedConfirmed += report.failed
rc.confirmTime += report.confirmTime
rc.sendingTime += report.sendingTime
}
type pushServiceList struct {
psList *list.List
}
func (psl *pushServiceList) New() error {
ps, err := newPushService()
if err != nil {
return err
}
psl.psList.PushBack(ps)
return nil
}
func (psl *pushServiceList) Remove(s *pushService) {
var toRemove *list.Element
if s == nil {
toRemove = psl.psList.Front()
} else {
for e := psl.psList.Front(); e != nil; e = e.Next() {
sp := e.Value.(*pushService)
if sp == s {
toRemove = e
break
}
}
}
if toRemove == nil {
logerr("I wanted to remove a push service and i didn't find it, this is a bug")
return
}
toRemove.Value.(*pushService).destroy()
psl.psList.Remove(toRemove)
}
func (psl *pushServiceList) Len() uint {
return uint(psl.psList.Len())
}