@@ -15,105 +15,40 @@ import (
15
15
16
16
var _ queue.Worker = (* Worker )(nil )
17
17
18
- // Option for queue system
19
- type Option func (* Worker )
20
-
21
18
// Worker for NSQ
22
19
type Worker struct {
23
- q * nsq.Consumer
24
- p * nsq.Producer
25
- startOnce sync.Once
26
- stopOnce sync.Once
27
- stop chan struct {}
28
- maxInFlight int
29
- addr string
30
- topic string
31
- channel string
32
- runFunc func (context.Context , queue.QueuedMessage ) error
33
- logger queue.Logger
34
- stopFlag int32
35
- startFlag int32
36
- metric queue.Metric
37
- disable bool
20
+ q * nsq.Consumer
21
+ p * nsq.Producer
22
+ startOnce sync.Once
23
+ stopOnce sync.Once
24
+ stop chan struct {}
25
+ stopFlag int32
26
+ startFlag int32
27
+ opts options
38
28
}
39
29
40
30
func (w * Worker ) incBusyWorker () {
41
- w .metric .IncBusyWorker ()
31
+ w .opts . metric .IncBusyWorker ()
42
32
}
43
33
44
34
func (w * Worker ) decBusyWorker () {
45
- w .metric .DecBusyWorker ()
35
+ w .opts . metric .DecBusyWorker ()
46
36
}
47
37
48
38
// BusyWorkers return count of busy workers currently.
49
39
func (w * Worker ) BusyWorkers () uint64 {
50
- return w .metric .BusyWorkers ()
51
- }
52
-
53
- // WithAddr setup the addr of NSQ
54
- func WithAddr (addr string ) Option {
55
- return func (w * Worker ) {
56
- w .addr = addr
57
- }
58
- }
59
-
60
- // WithTopic setup the topic of NSQ
61
- func WithTopic (topic string ) Option {
62
- return func (w * Worker ) {
63
- w .topic = topic
64
- }
65
- }
66
-
67
- // WithChannel setup the channel of NSQ
68
- func WithChannel (channel string ) Option {
69
- return func (w * Worker ) {
70
- w .channel = channel
71
- }
72
- }
73
-
74
- // WithRunFunc setup the run func of queue
75
- func WithRunFunc (fn func (context.Context , queue.QueuedMessage ) error ) Option {
76
- return func (w * Worker ) {
77
- w .runFunc = fn
78
- }
79
- }
80
-
81
- // WithMaxInFlight Maximum number of messages to allow in flight (concurrency knob)
82
- func WithMaxInFlight (num int ) Option {
83
- return func (w * Worker ) {
84
- w .maxInFlight = num
85
- }
86
- }
87
-
88
- // WithLogger set custom logger
89
- func WithLogger (l queue.Logger ) Option {
90
- return func (w * Worker ) {
91
- w .logger = l
92
- }
93
- }
94
-
95
- // WithMetric set custom Metric
96
- func WithMetric (m queue.Metric ) Option {
97
- return func (w * Worker ) {
98
- w .metric = m
99
- }
100
- }
101
-
102
- func withDisable () Option {
103
- return func (w * Worker ) {
104
- w .disable = true
105
- }
40
+ return w .opts .metric .BusyWorkers ()
106
41
}
107
42
108
43
// NewWorker for struc
109
44
func NewWorker (opts ... Option ) * Worker {
110
- w := & Worker {
45
+ defaultOpts := options {
111
46
addr : "127.0.0.1:4150" ,
112
47
topic : "gorush" ,
113
48
channel : "ch" ,
114
49
maxInFlight : runtime .NumCPU (),
115
- stop : make ( chan struct {}),
116
- logger : queue .NewLogger (),
50
+
51
+ logger : queue .NewLogger (),
117
52
runFunc : func (context.Context , queue.QueuedMessage ) error {
118
53
return nil
119
54
},
@@ -123,7 +58,12 @@ func NewWorker(opts ...Option) *Worker {
123
58
// Loop through each option
124
59
for _ , opt := range opts {
125
60
// Call the option giving the instantiated
126
- opt (w )
61
+ opt (& defaultOpts )
62
+ }
63
+
64
+ w := & Worker {
65
+ opts : defaultOpts ,
66
+ stop : make (chan struct {}),
127
67
}
128
68
129
69
w .startProducerAndConsumer ()
@@ -132,19 +72,19 @@ func NewWorker(opts ...Option) *Worker {
132
72
}
133
73
134
74
func (w * Worker ) startProducerAndConsumer () {
135
- if w .disable {
75
+ if w .opts . disable {
136
76
return
137
77
}
138
78
139
79
var err error
140
80
cfg := nsq .NewConfig ()
141
- cfg .MaxInFlight = w .maxInFlight
142
- w .q , err = nsq .NewConsumer (w .topic , w .channel , cfg )
81
+ cfg .MaxInFlight = w .opts . maxInFlight
82
+ w .q , err = nsq .NewConsumer (w .opts . topic , w . opts .channel , cfg )
143
83
if err != nil {
144
84
panic (err )
145
85
}
146
86
147
- w .p , err = nsq .NewProducer (w .addr , cfg )
87
+ w .p , err = nsq .NewProducer (w .opts . addr , cfg )
148
88
if err != nil {
149
89
panic (err )
150
90
}
@@ -159,7 +99,7 @@ func (w *Worker) BeforeRun() error {
159
99
func (w * Worker ) AfterRun () error {
160
100
w .startOnce .Do (func () {
161
101
time .Sleep (100 * time .Millisecond )
162
- err := w .q .ConnectToNSQD (w .addr )
102
+ err := w .q .ConnectToNSQD (w .opts . addr )
163
103
if err != nil {
164
104
panic ("Could not connect nsq server: " + err .Error ())
165
105
}
@@ -192,7 +132,7 @@ func (w *Worker) handle(job queue.Job) error {
192
132
}()
193
133
194
134
// run custom process function
195
- done <- w .runFunc (ctx , job )
135
+ done <- w .opts . runFunc (ctx , job )
196
136
}()
197
137
198
138
select {
@@ -253,7 +193,7 @@ func (w *Worker) Run() error {
253
193
select {
254
194
case <- w .stop :
255
195
case err := <- panicChan :
256
- w .logger .Error (err )
196
+ w .opts . logger .Error (err )
257
197
}
258
198
259
199
// wait job completed
@@ -297,7 +237,7 @@ func (w *Worker) Queue(job queue.QueuedMessage) error {
297
237
return queue .ErrQueueShutdown
298
238
}
299
239
300
- err := w .p .Publish (w .topic , job .Bytes ())
240
+ err := w .p .Publish (w .opts . topic , job .Bytes ())
301
241
if err != nil {
302
242
return err
303
243
}
0 commit comments