-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
94 lines (78 loc) · 1.85 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package nsq
import (
"context"
"github.com/golang-queue/queue"
"github.com/golang-queue/queue/core"
)
// An Option configures a mutex.
type Option interface {
Apply(*Options)
}
// OptionFunc is a function that configures a queue.
type OptionFunc func(*Options)
// Apply calls f(option)
func (f OptionFunc) Apply(option *Options) {
f(option)
}
type Options struct {
maxInFlight int
addr string
topic string
channel string
runFunc func(context.Context, core.QueuedMessage) error
logger queue.Logger
}
// WithAddr setup the addr of NSQ
func WithAddr(addr string) Option {
return OptionFunc(func(o *Options) {
o.addr = addr
})
}
// WithTopic setup the topic of NSQ
func WithTopic(topic string) Option {
return OptionFunc(func(o *Options) {
o.topic = topic
})
}
// WithChannel setup the channel of NSQ
func WithChannel(channel string) Option {
return OptionFunc(func(o *Options) {
o.channel = channel
})
}
// WithRunFunc setup the run func of queue
func WithRunFunc(fn func(context.Context, core.QueuedMessage) error) Option {
return OptionFunc(func(o *Options) {
o.runFunc = fn
})
}
// WithMaxInFlight Maximum number of messages to allow in flight (concurrency knob)
func WithMaxInFlight(num int) Option {
return OptionFunc(func(o *Options) {
o.maxInFlight = num
})
}
// WithLogger set custom logger
func WithLogger(l queue.Logger) Option {
return OptionFunc(func(o *Options) {
o.logger = l
})
}
func newOptions(opts ...Option) Options {
defaultOpts := Options{
addr: "127.0.0.1:4150",
topic: "gorush",
channel: "ch",
maxInFlight: 1,
logger: queue.NewLogger(),
runFunc: func(context.Context, core.QueuedMessage) error {
return nil
},
}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated
opt.Apply(&defaultOpts)
}
return defaultOpts
}