-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.go
144 lines (125 loc) · 3.17 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package rabbitmq
import (
"context"
"github.com/golang-queue/queue"
"github.com/golang-queue/queue/core"
)
// defined in rabbitmq client package.
const (
ExchangeDirect = "direct"
ExchangeFanout = "fanout"
ExchangeTopic = "topic"
ExchangeHeaders = "headers"
)
func isVaildExchange(name string) bool {
switch name {
case ExchangeDirect, ExchangeFanout, ExchangeTopic, ExchangeHeaders:
return true
default:
return false
}
}
// Option for queue system
type Option func(*options)
// AMQP 0-9-1 Model Explained
// ref: https://www.rabbitmq.com/tutorials/amqp-concepts.html
type options struct {
runFunc func(context.Context, core.QueuedMessage) error
logger queue.Logger
addr string
queue string
tag string
// Durable AMQP exchange name
exchangeName string
// Exchange Types: Direct, Fanout, Topic and Headers
exchangeType string
autoAck bool
// AMQP routing key
routingKey string
}
// WithAddr setup the URI
func WithAddr(addr string) Option {
return func(w *options) {
w.addr = addr
}
}
// WithExchangeName setup the Exchange name
// Exchanges are AMQP 0-9-1 entities where messages are sent to.
// Exchanges take a message and route it into zero or more queues.
func WithExchangeName(val string) Option {
return func(w *options) {
w.exchangeName = val
}
}
// WithExchangeType setup the Exchange type
// The routing algorithm used depends on the exchange type and rules called bindings.
// AMQP 0-9-1 brokers provide four exchange types:
// Direct exchange (Empty string) and amq.direct
// Fanout exchange amq.fanout
// Topic exchange amq.topic
// Headers exchange amq.match (and amq.headers in RabbitMQ)
func WithExchangeType(val string) Option {
return func(w *options) {
w.exchangeType = val
}
}
// WithRoutingKey setup AMQP routing key
func WithRoutingKey(val string) Option {
return func(w *options) {
w.routingKey = val
}
}
// WithAddr setup the tag
func WithTag(val string) Option {
return func(w *options) {
w.tag = val
}
}
// WithAutoAck enable message auto-ack
func WithAutoAck(val bool) Option {
return func(w *options) {
w.autoAck = val
}
}
// WithQueue setup the queue name
func WithQueue(val string) Option {
return func(w *options) {
w.queue = val
}
}
// 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{
addr: "amqp://guest:guest@localhost:5672/",
queue: "golang-queue",
tag: "golang-queue",
exchangeName: "test-exchange",
exchangeType: ExchangeDirect,
routingKey: "test-key",
logger: queue.NewLogger(),
autoAck: false,
runFunc: func(context.Context, core.QueuedMessage) error {
return nil
},
}
// Loop through each option
for _, opt := range opts {
// Call the option giving the instantiated
opt(&defaultOpts)
}
if !isVaildExchange(defaultOpts.exchangeType) {
defaultOpts.logger.Fatal("invaild exchange type: ", defaultOpts.exchangeType)
}
return defaultOpts
}