@@ -8,10 +8,20 @@ import (
8
8
"github.com/golang-queue/queue/core"
9
9
)
10
10
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
+ }
13
23
14
- type options struct {
24
+ type Options struct {
15
25
maxInFlight int
16
26
addr string
17
27
topic string
@@ -23,55 +33,55 @@ type options struct {
23
33
24
34
// WithAddr setup the addr of NSQ
25
35
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
+ })
29
39
}
30
40
31
41
// WithTopic setup the topic of NSQ
32
42
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
+ })
36
46
}
37
47
38
48
// WithChannel setup the channel of NSQ
39
49
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
+ })
43
53
}
44
54
45
55
// WithRunFunc setup the run func of queue
46
56
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
+ })
50
60
}
51
61
52
62
// WithMaxInFlight Maximum number of messages to allow in flight (concurrency knob)
53
63
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
+ })
57
67
}
58
68
59
69
// WithLogger set custom logger
60
70
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
+ })
64
74
}
65
75
66
76
// WithDisableConsumer disable consumer
67
77
func WithDisableConsumer () Option {
68
- return func (w * options ) {
69
- w .disableConsumer = true
70
- }
78
+ return OptionFunc ( func (o * Options ) {
79
+ o .disableConsumer = true
80
+ })
71
81
}
72
82
73
- func newOptions (opts ... Option ) options {
74
- defaultOpts := options {
83
+ func newOptions (opts ... Option ) Options {
84
+ defaultOpts := Options {
75
85
addr : "127.0.0.1:4150" ,
76
86
topic : "gorush" ,
77
87
channel : "ch" ,
@@ -86,7 +96,7 @@ func newOptions(opts ...Option) options {
86
96
// Loop through each option
87
97
for _ , opt := range opts {
88
98
// Call the option giving the instantiated
89
- opt (& defaultOpts )
99
+ opt . Apply (& defaultOpts )
90
100
}
91
101
92
102
return defaultOpts
0 commit comments