-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
192 lines (164 loc) · 4.63 KB
/
queue.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// ezmq: An easy golang amqp client.
// Copyright (C) 2022 super9du
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; If not, see <https://www.gnu.org/licenses/>.
package ezmq
import (
amqp "github.com/rabbitmq/amqp091-go"
)
type Queue struct {
c *Connection
declareOpts *QueueDeclareOpts
bindOpts *QueueBindOpts
retryable Retryable
*amqp.Queue
}
// 根据 queueName 声明队列,并绑定 queueName, key 到指定的 exchange。
// Queue 可能会因为网络原因创建失败,不提供一定创建成功保证。
func (q *Queue) DeclareAndBind(queueName, key, exchange string) error {
ch, err := q.c.Channel()
if err != nil {
return err
}
defer ch.Close()
queue, err := ch.QueueDeclare(
queueName,
q.declareOpts.durable,
q.declareOpts.autoDelete,
q.declareOpts.exclusive,
q.declareOpts.noWait,
*getNonNilArgs(q.declareOpts.args),
)
if err != nil {
return err
}
q.Queue = &queue
err = ch.QueueBind(queueName, key, exchange, q.declareOpts.noWait, *getNonNilArgs(q.declareOpts.args))
if err != nil {
return err
}
return nil
}
func (q *Queue) RetryDeclareAndBind(queueName, key, exchange string) error {
var err error
retryable := q.retryable
retryable.retry(func() (brk bool) {
err = q.DeclareAndBind(queueName, key, exchange)
if err == nil || !q.c.CanRetry() {
return true
}
return false
})
return err
}
type QueueBuilder struct {
que *Queue
}
func NewQueueBuilder(c *Connection) *QueueBuilder {
return &QueueBuilder{&Queue{c: c}}
}
func (bld *QueueBuilder) SetQueueDeclareOpts(builderFn func(builder *QueueDeclareOptsBuilder) *QueueDeclareOpts) *QueueBuilder {
bld.que.declareOpts = builderFn(NewQueueDeclareOptsBuilder())
return bld
}
func (bld *QueueBuilder) SetQueueBindOpts(builderFn func(builder *QueueBindOptsBuilder) *QueueBindOpts) *QueueBuilder {
bld.que.bindOpts = builderFn(NewQueueBindOptsBuilder())
return bld
}
func (bld *QueueBuilder) SetRetryable(retryable Retryable) *QueueBuilder {
bld.que.retryable = retryable
return bld
}
func (bld *QueueBuilder) Build() *Queue {
que := bld.que
if que.declareOpts == nil {
que.declareOpts = DefaultQueueDeclareOpts()
}
if que.bindOpts == nil {
que.bindOpts = DefaultBindOpts()
}
if que.retryable == nil {
que.retryable = DefaultTimesRetry()
}
return que
}
type QueueDeclareOpts struct {
durable, autoDelete, exclusive, noWait bool
args *amqp.Table
}
func DefaultQueueDeclareOpts() *QueueDeclareOpts {
return &QueueDeclareOpts{
durable: true,
autoDelete: false,
exclusive: false,
noWait: false,
args: nil,
}
}
type QueueDeclareOptsBuilder struct {
opts *QueueDeclareOpts
}
func NewQueueDeclareOptsBuilder() *QueueDeclareOptsBuilder {
return &QueueDeclareOptsBuilder{DefaultQueueDeclareOpts()}
}
func (bld *QueueDeclareOptsBuilder) SetDurable(b bool) *QueueDeclareOptsBuilder {
bld.opts.durable = b
return bld
}
func (bld *QueueDeclareOptsBuilder) SetAutoDelete(b bool) *QueueDeclareOptsBuilder {
bld.opts.autoDelete = b
return bld
}
func (bld *QueueDeclareOptsBuilder) SetExclusive(b bool) *QueueDeclareOptsBuilder {
bld.opts.exclusive = b
return bld
}
func (bld *QueueDeclareOptsBuilder) SetNowait(b bool) *QueueDeclareOptsBuilder {
bld.opts.noWait = b
return bld
}
func (bld *QueueDeclareOptsBuilder) SetArgs(args *amqp.Table) *QueueDeclareOptsBuilder {
bld.opts.args = args
return bld
}
func (bld *QueueDeclareOptsBuilder) Build() *QueueDeclareOpts {
return bld.opts
}
type QueueBindOpts struct {
noWait bool
args *amqp.Table
}
func DefaultBindOpts() *QueueBindOpts {
return &QueueBindOpts{
noWait: false,
args: nil,
}
}
type QueueBindOptsBuilder struct {
opts *QueueBindOpts
}
func NewQueueBindOptsBuilder() *QueueBindOptsBuilder {
return &QueueBindOptsBuilder{DefaultBindOpts()}
}
func (bld *QueueBindOptsBuilder) SetNoWait(b bool) *QueueBindOptsBuilder {
bld.opts.noWait = b
return bld
}
func (bld *QueueBindOptsBuilder) SetArgs(args *amqp.Table) *QueueBindOptsBuilder {
bld.opts.args = args
return bld
}
func (bld *QueueBindOptsBuilder) Build() *QueueBindOpts {
return bld.opts
}