-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsubscriber.go
173 lines (146 loc) · 3.23 KB
/
subscriber.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
package eventpool
import (
"context"
"io"
"time"
)
const (
waitSleepClose = 1
)
type SubscriberFunc func(name string, message io.Reader) error
type SubscriberConfigFunc func(c *subscriberConfig)
type subscriberConfig struct {
bufferSize int
maxWorkers int
maxRetry int
errorHook func(name string, job io.Reader)
recoverHook func(name string, job io.Reader)
closeHook func(name string)
}
func BufferSize(bufferSize int) func(config *subscriberConfig) {
return func(config *subscriberConfig) {
if bufferSize == 0 {
return
}
config.bufferSize = bufferSize
}
}
func MaxWorker(max int) func(config *subscriberConfig) {
return func(config *subscriberConfig) {
if max == 0 {
return
}
config.maxWorkers = max
}
}
func MaxRetry(max int) func(config *subscriberConfig) {
return func(config *subscriberConfig) {
if max == 0 {
return
}
config.maxRetry = max
}
}
// RecoverHook handling if receive the signal panic
func RecoverHook(recoverHook func(name string, job io.Reader)) func(config *subscriberConfig) {
return func(config *subscriberConfig) {
config.recoverHook = recoverHook
}
}
// CloseHook handling for close the eventpool
func CloseHook(closeHook func(name string)) func(config *subscriberConfig) {
return func(config *subscriberConfig) {
config.closeHook = closeHook
}
}
// ErrorHook handling for dead-letter queue
func ErrorHook(errorHook func(name string, job io.Reader)) func(config *subscriberConfig) {
return func(config *subscriberConfig) {
config.errorHook = errorHook
}
}
type subscriber struct {
name string
jobs chan io.Reader
fn SubscriberFunc
ctx context.Context
cancel context.CancelFunc
config subscriberConfig
}
func newSubscriber(name string, fn SubscriberFunc, opts ...SubscriberConfigFunc) *subscriber {
cfg := subscriberConfig{
maxWorkers: 10,
bufferSize: 100,
maxRetry: 3,
closeHook: nil,
recoverHook: nil,
}
for _, opt := range opts {
opt(&cfg)
}
ctx, cancel := context.WithCancel(context.Background())
return &subscriber{
name: name,
jobs: make(chan io.Reader, cfg.bufferSize),
fn: fn,
ctx: ctx,
cancel: cancel,
config: cfg,
}
}
func (s *subscriber) listen() {
for i := 0; i < s.config.maxWorkers; i++ {
go s.spawn(i)
}
}
func (s *subscriber) spawn(worker int) {
for {
select {
case <-s.ctx.Done():
return
case job := <-s.jobs:
maxRetry := 0
Retry:
err := s.process(s.name, job) // create new func to handle panic recover
if err != nil {
maxRetry++
if maxRetry <= s.config.maxRetry {
goto Retry
}
if maxRetry > s.config.maxRetry && s.config.errorHook != nil {
s.config.errorHook(s.name, job)
}
}
}
}
}
func (s *subscriber) process(name string, job io.Reader) error {
defer func() {
if r := recover(); r != nil {
if s.config.recoverHook != nil {
s.config.recoverHook(s.name, job)
}
}
}()
err := s.fn(name, job)
if err != nil {
return err
}
return nil
}
func (s *subscriber) cap() int {
return len(s.jobs)
}
func (s *subscriber) close() {
Retry:
if s.cap() > 0 {
time.Sleep(waitSleepClose * time.Second)
goto Retry
}
// cancel until the job getting done
s.cancel()
if s.config.closeHook == nil {
return
}
s.config.closeHook(s.name)
}