Skip to content

Commit ed24fa1

Browse files
committed
refactor(options): add new interface and new Apply method.
1 parent d1132ff commit ed24fa1

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

options.go

+27-17
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,66 @@ var (
1717
defaultMetric = NewMetric()
1818
)
1919

20-
// Option for queue system
21-
type Option func(*Options)
20+
// An Option configures a mutex.
21+
type Option interface {
22+
Apply(*Options)
23+
}
24+
25+
// OptionFunc is a function that configures a queue.
26+
type OptionFunc func(*Options)
27+
28+
// Apply calls f(option)
29+
func (f OptionFunc) Apply(option *Options) {
30+
f(option)
31+
}
2232

2333
// WithWorkerCount set worker count
2434
func WithWorkerCount(num int) Option {
25-
return func(q *Options) {
35+
return OptionFunc(func(q *Options) {
2636
q.workerCount = num
27-
}
37+
})
2838
}
2939

3040
// WithQueueSize set worker count
3141
func WithQueueSize(num int) Option {
32-
return func(q *Options) {
42+
return OptionFunc(func(q *Options) {
3343
q.queueSize = num
34-
}
44+
})
3545
}
3646

3747
// WithLogger set custom logger
3848
func WithLogger(l Logger) Option {
39-
return func(q *Options) {
49+
return OptionFunc(func(q *Options) {
4050
q.logger = l
41-
}
51+
})
4252
}
4353

4454
// WithMetric set custom Metric
4555
func WithMetric(m Metric) Option {
46-
return func(q *Options) {
56+
return OptionFunc(func(q *Options) {
4757
q.metric = m
48-
}
58+
})
4959
}
5060

5161
// WithWorker set custom worker
5262
func WithWorker(w core.Worker) Option {
53-
return func(q *Options) {
63+
return OptionFunc(func(q *Options) {
5464
q.worker = w
55-
}
65+
})
5666
}
5767

5868
// WithFn set custom job function
5969
func WithFn(fn func(context.Context, core.QueuedMessage) error) Option {
60-
return func(q *Options) {
70+
return OptionFunc(func(q *Options) {
6171
q.fn = fn
62-
}
72+
})
6373
}
6474

6575
// WithTimeOut set custom timeout
6676
func WithTimeOut(t time.Duration) Option {
67-
return func(q *Options) {
77+
return OptionFunc(func(q *Options) {
6878
q.timeout = t
69-
}
79+
})
7080
}
7181

7282
// Options for custom args in Queue
@@ -95,7 +105,7 @@ func NewOptions(opts ...Option) *Options {
95105
// Loop through each option
96106
for _, opt := range opts {
97107
// Call the option giving the instantiated
98-
opt(o)
108+
opt.Apply(o)
99109
}
100110

101111
return o

0 commit comments

Comments
 (0)