@@ -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
2535func 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
3242func 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
3949func 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
4656func 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)
5363func 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
6070func 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
6777func 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