-
Notifications
You must be signed in to change notification settings - Fork 6
/
supervisor.go
584 lines (533 loc) · 15 KB
/
supervisor.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
package gunfish
import (
"bytes"
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/kayac/Gunfish/apns"
"github.com/kayac/Gunfish/config"
"github.com/kayac/Gunfish/fcm"
"github.com/satori/go.uuid"
"github.com/sirupsen/logrus"
)
// Supervisor monitor mutiple http2 clients.
type Supervisor struct {
queue chan *[]Request // supervisor's queue that recieves POST requests.
retryq chan Request // enqueues this retry queue when to failed to send notification on the http layer.
cmdq chan Command // enqueues this command queue when to get error response from apns.
exit chan struct{} // exit channel is used to stop the supervisor.
ticker *time.Ticker // ticker checks retry queue that has notifications to resend periodically.
wgrp *sync.WaitGroup
workers []*Worker
}
// Worker sends notification to apns.
type Worker struct {
ac *apns.Client
fc *fcm.Client
queue chan Request
respq chan SenderResponse
wgrp *sync.WaitGroup
sn int
id int
errorHandler func(Request, *http.Response, error)
successHandler func(Request, *http.Response)
}
// SenderResponse is responses to worker from sender.
type SenderResponse struct {
Results []Result `json:"response"`
RespTime float64 `json:"response_time"`
Req Request `json:"request"`
Err error `json:"error_msg"`
UID string `json:"resp_uid"`
}
// Command has execute command and input stream.
type Command struct {
command string
input []byte
}
// EnqueueClientRequest enqueues request to supervisor's queue from external application service
func (s *Supervisor) EnqueueClientRequest(reqs *[]Request) error {
logf := logrus.Fields{
"type": "supervisor",
"request_size": len(*reqs),
"queue_size": len(s.queue),
"retry_queue_size": len(s.retryq),
}
select {
case s.queue <- reqs:
LogWithFields(logf).Debugf("Enqueued request from provider.")
default:
LogWithFields(logf).Warnf("Supervisor's queue is full.")
return fmt.Errorf("Supervisor's queue is full")
}
return nil
}
// StartSupervisor starts supervisor
func StartSupervisor(conf *config.Config) (Supervisor, error) {
// Calculates each worker queue size to accept requests with a given parameter of requests per sec as flow rate.
var wqSize int
tp := ((conf.Provider.RequestQueueSize * int(AverageResponseTime/time.Millisecond)) / 1000) / SenderNum
dif := (RequestPerSec - conf.Provider.RequestQueueSize/tp)
if dif > 0 {
wqSize = dif * int(FlowRateInterval/time.Second) / conf.Provider.WorkerNum
} else {
wqSize = -1 * dif * int(FlowRateInterval/time.Second) / conf.Provider.WorkerNum
}
// Initialize Supervisor
swgrp := &sync.WaitGroup{}
s := Supervisor{
queue: make(chan *[]Request, conf.Provider.QueueSize),
retryq: make(chan Request, conf.Provider.RequestQueueSize*conf.Provider.WorkerNum),
cmdq: make(chan Command, wqSize*conf.Provider.WorkerNum),
exit: make(chan struct{}, 1),
ticker: time.NewTicker(RetryWaitTime),
wgrp: swgrp,
}
LogWithFields(logrus.Fields{}).Infof("Retry queue size: %d", cap(s.retryq))
LogWithFields(logrus.Fields{}).Infof("Queue size: %d", cap(s.queue))
// Time ticker to retry to send
go func() {
for {
select {
case <-s.ticker.C:
// Number of request retry send at once.
for cnt := 0; cnt < RetryOnceCount; cnt++ {
select {
case req := <-s.retryq:
reqs := &[]Request{req}
select {
case s.queue <- reqs:
LogWithFields(logrus.Fields{"type": "retry", "resend_cnt": req.Tries}).
Debugf("Enqueue to retry to send notification.")
default:
LogWithFields(logrus.Fields{"type": "retry"}).
Infof("Could not retry to enqueue because the supervisor queue is full.")
}
default:
break
}
}
case <-s.exit:
s.ticker.Stop()
return
}
}
}()
// spawn command
for i := 0; i < conf.Provider.WorkerNum; i++ {
s.wgrp.Add(1)
go func() {
logf := logrus.Fields{"type": "cmd_worker"}
for c := range s.cmdq {
LogWithFields(logf).Debugf("invoking command: %s %s", c.command, string(c.input))
src := bytes.NewBuffer(c.input)
out, err := invokePipe(c.command, src)
if err != nil {
LogWithFields(logf).Errorf("(%s) %s", err.Error(), string(out))
} else {
LogWithFields(logf).Debugf("Success to execute command")
}
}
s.wgrp.Done()
}()
}
// Spawn workers
var err error
for i := 0; i < conf.Provider.WorkerNum; i++ {
var (
ac *apns.Client
fc *fcm.Client
)
if conf.Apns.Enabled {
ac, err = apns.NewClient(conf.Apns)
if err != nil {
LogWithFields(logrus.Fields{
"type": "supervisor",
}).Errorf("%s", err.Error())
break
}
}
if conf.FCM.Enabled {
fc, err = fcm.NewClient(conf.FCM.APIKey, nil, fcm.ClientTimeout)
if err != nil {
LogWithFields(logrus.Fields{
"type": "supervisor",
}).Errorf("%s", err.Error())
break
}
}
worker := Worker{
id: i,
queue: make(chan Request, wqSize),
respq: make(chan SenderResponse, wqSize),
wgrp: &sync.WaitGroup{},
sn: SenderNum,
ac: ac,
fc: fc,
}
s.workers = append(s.workers, &worker)
s.wgrp.Add(1)
go s.spawnWorker(worker, conf)
LogWithFields(logrus.Fields{
"type": "worker",
"worker_id": i,
}).Debugf("Spawned worker-%d.", i)
}
if err != nil {
return Supervisor{}, err
}
return s, nil
}
// Shutdown supervisor
func (s *Supervisor) Shutdown() {
LogWithFields(logrus.Fields{
"type": "supervisor",
}).Infoln("Waiting for stopping supervisor...")
// Waiting for processing notification requests
zeroCnt := 0
tryCnt := 0
for zeroCnt < RestartWaitCount {
// if 's.counter' is not 0 potentially, here loop should not cancel to wait.
if len(s.queue)+len(s.cmdq)+len(s.retryq)+s.workersAllQueueLength() > 0 {
zeroCnt = 0
tryCnt++
} else {
zeroCnt++
tryCnt = 0
}
// force terminate application waiting for over 2 min.
// RestartWaitCount: 50
// ShutdownWaitTime: 10 (msec)
// 40 * 50 * 6 * 10 (msec) / 1,000 / 60 = 2 (min)
if tryCnt > RestartWaitCount*40*6 {
break
}
time.Sleep(ShutdownWaitTime)
}
close(s.exit)
close(s.cmdq)
s.wgrp.Wait()
close(s.queue)
close(s.retryq)
LogWithFields(logrus.Fields{
"type": "supervisor",
}).Infoln("Stoped supervisor.")
}
func (s *Supervisor) spawnWorker(w Worker, conf *config.Config) {
atomic.AddInt64(&(srvStats.Workers), 1)
defer func() {
atomic.AddInt64(&(srvStats.Workers), -1)
close(w.respq)
s.wgrp.Done()
}()
// Queue of SenderResopnse
for i := 0; i < w.sn; i++ {
w.wgrp.Add(1)
LogWithFields(logrus.Fields{
"type": "worker",
"worker_id": w.id,
}).Debugf("Spawned a sender-%d-%d.", w.id, i)
// spawnSender
go spawnSender(w.queue, w.respq, w.wgrp, w.ac, w.fc)
}
func() {
for {
select {
case reqs := <-s.queue:
w.receiveRequests(reqs)
case resp := <-w.respq:
w.receiveResponse(resp, s.retryq, s.cmdq)
case <-s.exit:
return
}
}
}()
close(w.queue)
w.wgrp.Wait()
}
func (w *Worker) receiveResponse(resp SenderResponse, retryq chan<- Request, cmdq chan Command) {
req := resp.Req
switch t := req.Notification.(type) {
case apns.Notification:
no := req.Notification.(apns.Notification)
logf := logrus.Fields{
"type": "worker",
"status": "-",
"apns_id": "-",
"token": no.Token,
"payload": no.Payload,
"worker_id": w.id,
"res_queue_size": len(w.respq),
"resend_cnt": req.Tries,
"response_time": resp.RespTime,
"resp_uid": resp.UID,
}
handleAPNsResponse(resp, retryq, cmdq, logf)
case fcm.Payload:
p := req.Notification.(fcm.Payload)
logf := logrus.Fields{
"type": "worker",
"reg_ids_length": len(p.RegistrationIDs),
"notification": p.Notification,
"data": p.Data,
"worker_id": w.id,
"res_queue_size": len(w.respq),
"resend_cnt": req.Tries,
"response_time": resp.RespTime,
"resp_uid": resp.UID,
}
handleFCMResponse(resp, retryq, cmdq, logf)
default:
LogWithFields(logrus.Fields{"type": "worker"}).Infof("Unknown request type:%s", t)
}
}
func handleAPNsResponse(resp SenderResponse, retryq chan<- Request, cmdq chan Command, logf logrus.Fields) {
req := resp.Req
// Response handling
if resp.Err != nil {
atomic.AddInt64(&(srvStats.ErrCount), 1)
if len(resp.Results) > 0 {
result := resp.Results[0]
for _, key := range result.ExtraKeys() {
logf[key] = result.ExtraValue(key)
}
logf["status"] = result.Status()
LogWithFields(logf).Errorf("%s", resp.Err)
// Error handling
onResponse(result, errorResponseHandler.HookCmd(), cmdq)
} else {
// if 'result' is nil, HTTP connection error with APNS.
retry(retryq, req, errors.New("http connection error between APNs"), logf)
}
} else {
atomic.AddInt64(&(srvStats.SentCount), 1)
if len(resp.Results) > 0 {
result := resp.Results[0]
for _, key := range result.ExtraKeys() {
logf[key] = result.ExtraValue(key)
}
if err := result.Err(); err != nil {
atomic.AddInt64(&(srvStats.ErrCount), 1)
// retry when provider auhentication token is expired
if err.Error() == apns.ExpiredProviderToken.String() {
retry(retryq, req, err, logf)
}
onResponse(result, errorResponseHandler.HookCmd(), cmdq)
LogWithFields(logf).Errorf("%s", err)
} else {
onResponse(result, "", cmdq)
LogWithFields(logf).Info("Succeeded to send a notification")
}
}
}
}
func handleFCMResponse(resp SenderResponse, retryq chan<- Request, cmdq chan Command, logf logrus.Fields) {
if resp.Err != nil {
req := resp.Req
LogWithFields(logf).Warnf("response is nil. reason: %s", resp.Err.Error())
if req.Tries < SendRetryCount {
req.Tries++
atomic.AddInt64(&(srvStats.RetryCount), 1)
logf["resend_cnt"] = req.Tries
select {
case retryq <- req:
LogWithFields(logf).
Debugf("Retry to enqueue into retryq because of http connection error with FCM.")
default:
LogWithFields(logf).
Warnf("Supervisor retry queue is full.")
}
} else {
LogWithFields(logf).
Warnf("Retry count is over than %d. Could not deliver notification.", SendRetryCount)
}
return
}
for _, result := range resp.Results {
// success when Error is nothing
err := result.Err()
if err == nil {
atomic.AddInt64(&(srvStats.SentCount), 1)
LogWithFields(logf).Info("Succeeded to send a notification")
continue
}
// handle error response each registration_id
atomic.AddInt64(&(srvStats.ErrCount), 1)
if err.Error() == fcm.InvalidRegistration.String() {
// TODO: should delete registration_id from server data store
onResponse(result, errorResponseHandler.HookCmd(), cmdq)
LogWithFields(logf).Errorf("%s", err)
} else {
LogWithFields(logf).Errorf("Unknown error message: %s", err)
}
}
}
func (w *Worker) receiveRequests(reqs *[]Request) {
logf := logrus.Fields{
"type": "worker",
"worker_id": w.id,
"worker_queue_size": len(w.queue),
"request_size": len(*reqs),
}
for _, req := range *reqs {
select {
case w.queue <- req:
LogWithFields(logf).
Debugf("Enqueue request into worker's queue")
}
}
}
func spawnSender(wq <-chan Request, respq chan<- SenderResponse, wgrp *sync.WaitGroup, ac *apns.Client, fc *fcm.Client) {
defer wgrp.Done()
for req := range wq {
var sres SenderResponse
switch t := req.Notification.(type) {
case apns.Notification:
if ac == nil {
LogWithFields(logrus.Fields{"type": "sender"}).
Errorf("apns client is not present")
continue
}
no := req.Notification.(apns.Notification)
start := time.Now()
results, err := ac.Send(no)
respTime := time.Now().Sub(start).Seconds()
rs := make([]Result, 0, len(results))
for _, v := range results {
rs = append(rs, v)
}
sres = SenderResponse{
Results: rs,
RespTime: respTime,
Req: req, // Must copy
Err: err,
UID: uuid.NewV4().String(),
}
case fcm.Payload:
if fc == nil {
LogWithFields(logrus.Fields{"type": "sender"}).
Errorf("fcm client is not present")
continue
}
p := req.Notification.(fcm.Payload)
start := time.Now()
results, err := fc.Send(p)
respTime := time.Now().Sub(start).Seconds()
rs := make([]Result, 0, len(results))
for _, v := range results {
rs = append(rs, v)
}
sres = SenderResponse{
Results: rs,
RespTime: respTime,
Req: req,
Err: err,
UID: uuid.NewV4().String(),
}
default:
LogWithFields(logrus.Fields{"type": "sender"}).
Errorf("Unknown request data type: %s", t)
continue
}
select {
case respq <- sres:
LogWithFields(logrus.Fields{"type": "sender", "resp_queue_size": len(respq)}).
Debugf("Enqueue response into respq.")
default:
LogWithFields(logrus.Fields{"type": "sender", "resp_queue_size": len(respq)}).
Warnf("Response queue is full.")
}
}
}
func (s Supervisor) workersAllQueueLength() int {
sum := 0
for _, w := range s.workers {
sum += len(w.queue) + len(w.respq)
}
return sum
}
func onResponse(result Result, cmd string, cmdq chan<- Command) {
logf := logrus.Fields{
"provider": result.Provider(),
"type": "on_response",
"token": result.RecipientIdentifier(),
}
for _, key := range result.ExtraKeys() {
logf[key] = result.ExtraValue(key)
}
// on error handler
if err := result.Err(); err != nil {
errorResponseHandler.OnResponse(result)
} else {
successResponseHandler.OnResponse(result)
}
if cmd == "" {
return
}
b, _ := result.MarshalJSON()
command := Command{
command: cmd,
input: b,
}
select {
case cmdq <- command:
LogWithFields(logf).Debugf("Enqueue command: %v", command)
default:
LogWithFields(logf).Warnf("Command queue is full, so could not execute commnad: %v", command)
}
}
func invokePipe(hook string, src io.Reader) ([]byte, error) {
logf := logrus.Fields{"type": "invoke_pipe"}
cmd := exec.Command("sh", "-c", hook)
stdin, err := cmd.StdinPipe()
if err != nil {
return nil, fmt.Errorf("failed: %v %s", cmd, err.Error())
}
var b bytes.Buffer
// merge std(out|err) of command to gunfish
if OutputHookStdout {
cmd.Stdout = os.Stdout
} else {
cmd.Stdout = &b
}
if OutputHookStderr {
cmd.Stderr = os.Stderr
} else {
cmd.Stderr = &b
}
// src copy to cmd.stdin
_, err = io.Copy(stdin, src)
if e, ok := err.(*os.PathError); ok && e.Err == syscall.EPIPE {
LogWithFields(logf).Errorf(e.Error())
} else if err != nil {
LogWithFields(logf).Errorf("failed to write STDIN: cmd( %s ), error( %s )", hook, err.Error())
}
stdin.Close()
err = cmd.Run()
return b.Bytes(), err
}
func retry(retryq chan<- Request, req Request, err error, logf logrus.Fields) {
if req.Tries < SendRetryCount {
req.Tries++
atomic.AddInt64(&(srvStats.RetryCount), 1)
logf["resend_cnt"] = req.Tries
select {
case retryq <- req:
LogWithFields(logf).
Debugf("%s: Retry to enqueue into retryq.", err.Error())
default:
LogWithFields(logf).
Warnf("Supervisor retry queue is full.")
}
} else {
LogWithFields(logf).
Warnf("Retry count is over than %d. Could not deliver notification.", SendRetryCount)
}
}