Skip to content

Commit 3f98429

Browse files
committed
refactor: add Option interface
1 parent e35fa14 commit 3f98429

File tree

2 files changed

+38
-28
lines changed

2 files changed

+38
-28
lines changed

nsq.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Worker struct {
2323
stop chan struct{}
2424
stopFlag int32
2525
startFlag int32
26-
opts options
26+
opts Options
2727
tasks chan *nsq.Message
2828
}
2929

options.go

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@ import (
88
"github.com/golang-queue/queue/core"
99
)
1010

11-
// Option for queue system
12-
type Option func(*options)
11+
// An Option configures a mutex.
12+
type Option interface {
13+
Apply(*Options)
14+
}
15+
16+
// OptionFunc is a function that configures a queue.
17+
type OptionFunc func(*Options)
18+
19+
// Apply calls f(option)
20+
func (f OptionFunc) Apply(option *Options) {
21+
f(option)
22+
}
1323

14-
type options struct {
24+
type Options struct {
1525
maxInFlight int
1626
addr string
1727
topic string
@@ -23,55 +33,55 @@ type options struct {
2333

2434
// WithAddr setup the addr of NSQ
2535
func WithAddr(addr string) Option {
26-
return func(w *options) {
27-
w.addr = addr
28-
}
36+
return OptionFunc(func(o *Options) {
37+
o.addr = addr
38+
})
2939
}
3040

3141
// WithTopic setup the topic of NSQ
3242
func WithTopic(topic string) Option {
33-
return func(w *options) {
34-
w.topic = topic
35-
}
43+
return OptionFunc(func(o *Options) {
44+
o.topic = topic
45+
})
3646
}
3747

3848
// WithChannel setup the channel of NSQ
3949
func WithChannel(channel string) Option {
40-
return func(w *options) {
41-
w.channel = channel
42-
}
50+
return OptionFunc(func(o *Options) {
51+
o.channel = channel
52+
})
4353
}
4454

4555
// WithRunFunc setup the run func of queue
4656
func WithRunFunc(fn func(context.Context, core.QueuedMessage) error) Option {
47-
return func(w *options) {
48-
w.runFunc = fn
49-
}
57+
return OptionFunc(func(o *Options) {
58+
o.runFunc = fn
59+
})
5060
}
5161

5262
// WithMaxInFlight Maximum number of messages to allow in flight (concurrency knob)
5363
func WithMaxInFlight(num int) Option {
54-
return func(w *options) {
55-
w.maxInFlight = num
56-
}
64+
return OptionFunc(func(o *Options) {
65+
o.maxInFlight = num
66+
})
5767
}
5868

5969
// WithLogger set custom logger
6070
func WithLogger(l queue.Logger) Option {
61-
return func(w *options) {
62-
w.logger = l
63-
}
71+
return OptionFunc(func(o *Options) {
72+
o.logger = l
73+
})
6474
}
6575

6676
// WithDisableConsumer disable consumer
6777
func WithDisableConsumer() Option {
68-
return func(w *options) {
69-
w.disableConsumer = true
70-
}
78+
return OptionFunc(func(o *Options) {
79+
o.disableConsumer = true
80+
})
7181
}
7282

73-
func newOptions(opts ...Option) options {
74-
defaultOpts := options{
83+
func newOptions(opts ...Option) Options {
84+
defaultOpts := Options{
7585
addr: "127.0.0.1:4150",
7686
topic: "gorush",
7787
channel: "ch",
@@ -86,7 +96,7 @@ func newOptions(opts ...Option) options {
8696
// Loop through each option
8797
for _, opt := range opts {
8898
// Call the option giving the instantiated
89-
opt(&defaultOpts)
99+
opt.Apply(&defaultOpts)
90100
}
91101

92102
return defaultOpts

0 commit comments

Comments
 (0)