forked from golang-queue/kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
89 lines (74 loc) · 1.63 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
package kafka
import (
"context"
"github.com/golang-queue/queue"
"github.com/golang-queue/queue/core"
"github.com/segmentio/kafka-go/compress"
)
// Option for queue system
type Option func(*options)
type options struct {
runFunc func(context.Context, core.QueuedMessage) error
logger queue.Logger
addr string
network string
queue string
topic string
partition int //kafka's partition
compression compress.Codec
}
// WithAddr setup the URI
func WithAddr(addr string) Option {
return func(w *options) {
w.addr = addr
}
}
func WithNetwork(network string) Option {
return func(w *options) {
w.network = network
}
}
// WithTopic setup the Topic
func WithTopic(topic string) Option {
return func(w *options) {
w.topic = topic
}
}
// WithPartition setup the partition
func WithPartition(partition int) Option {
return func(w *options) {
w.partition = partition
}
}
// WithQueue setup the queue name
func WithQueue(val string) Option {
return func(w *options) {
w.queue = val
}
}
// WithAddr setup the URI
func WithCompress(compress compress.Codec) Option {
return func(w *options) {
w.compression = compress
}
}
// WithRunFunc setup the run func of queue
func WithRunFunc(fn func(context.Context, core.QueuedMessage) error) Option {
return func(w *options) {
w.runFunc = fn
}
}
// WithLogger set custom logger
func WithLogger(l queue.Logger) Option {
return func(w *options) {
w.logger = l
}
}
func newOptions(opts ...Option) options {
defaultOpts := options{}
for _, opt := range opts {
// Call the option giving the instantiated
opt(&defaultOpts)
}
return defaultOpts
}