-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
381 lines (310 loc) · 6.61 KB
/
client.go
File metadata and controls
381 lines (310 loc) · 6.61 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package rabbitmq
import (
"sync"
"time"
"github.com/streadway/amqp"
"golang.org/x/sync/singleflight"
)
// ClientOption 客户端选项
type ClientOption func(*Client)
// Client 客户端
type Client struct {
dsn string
vhost string
channelMax int
heartbeat time.Duration
retryInterval time.Duration // 重试时间间隔
connection *amqp.Connection // RabbitMQ 连接
serverCloseChan chan *amqp.Error // 服务器端关闭事件通道
clientCloseChan chan struct{} // 客户端关闭事件通道
syncWorkGroup *singleflight.Group // 排他操作群组,保证同一时间只有一个协程处理任务
locker sync.RWMutex // 读写锁,同步数据操作
state int8 // 客户端状态
consumers map[*Consumer]struct{}
}
//
// Open
// @desc 开启
// @receiver c *Client
// @return error
//
func (c *Client) Open() error {
if _, err := c.createConnection(); err != nil {
return err
}
return nil
}
//
// listenCloseEvent
// @desc 监听关闭事件
// @receiver c *Client
//
func (c *Client) listenCloseEvent() {
select {
// 主动关闭
case <-c.clientCloseChan:
// 服务端关闭
case <-c.serverCloseChan:
c.locker.Lock()
c.state = StateRetry
c.locker.Unlock()
_, _ = c.createConnection()
}
}
//
// createConnection
// @desc 创建连接
// @receiver c *Client
// @return *amqp.Connection
// @return error
//
func (c *Client) createConnection() (*amqp.Connection, error) {
connection, err, _ := c.syncWorkGroup.Do(createConnectionTaskKey, func() (interface{}, error) {
for {
select {
// 主动关闭
case <-c.clientCloseChan:
return nil, ErrClientClosed
default:
var err error
var connection *amqp.Connection
if connection, err = c.checkAliveConnection(); err == nil {
return connection, nil
}
if err != ErrConnectionCreateRequired {
return nil, err
}
if connection, err = c.dial(); err != nil {
time.Sleep(c.retryInterval)
continue
}
if err = c.activateClient(connection); err != nil {
return nil, err
}
return connection, nil
}
}
})
if err != nil {
return nil, err
}
return connection.(*amqp.Connection), nil
}
//
// checkAliveConnection
// @desc 检查存活连接
// @receiver c *Client
// @return *amqp.Connection
// @return error
//
func (c *Client) checkAliveConnection() (*amqp.Connection, error) {
c.locker.RLock()
defer c.locker.RUnlock()
if c.state == StateClosed {
return nil, ErrClientClosed
}
if c.connection != nil && !c.connection.IsClosed() {
return c.connection, nil
}
return nil, ErrConnectionCreateRequired
}
//
// activateClient
// @desc 激活客户端
// @receiver c *Client
// @param connection *amqp.Connection
// @return error
//
func (c *Client) activateClient(connection *amqp.Connection) error {
c.locker.Lock()
defer c.locker.Unlock()
// 客户端已关闭
if c.state == StateClosed {
_ = connection.Close()
return ErrClientClosed
}
c.connection = connection
c.state = StateActive
c.serverCloseChan = make(chan *amqp.Error)
c.connection.NotifyClose(c.serverCloseChan)
go c.listenCloseEvent()
return nil
}
//
// dial
// @desc 连接服务器
// @receiver c *Client
// @return *amqp.Connection
// @return error
//
func (c *Client) dial() (*amqp.Connection, error) {
config := amqp.Config{
Vhost: c.vhost,
ChannelMax: c.channelMax,
Heartbeat: c.heartbeat,
Locale: defaultLocale,
}
return amqp.DialConfig(c.dsn, config)
}
//
// Connection
// @desc 获取连接
// @receiver c *Client
// @return *amqp.Connection
// @return error
//
func (c *Client) Connection() (*amqp.Connection, error) {
connection, err := c.checkAliveConnection()
if err == nil {
return connection, nil
}
if err == ErrConnectionCreateRequired {
return c.createConnection()
}
return nil, err
}
//
// Channel
// @desc 获取 CHANNEL
// @receiver c *Client
// @return *amqp.Channel
// @return error
//
func (c *Client) Channel() (*amqp.Channel, error) {
connection, err := c.Connection()
if err != nil {
return nil, err
}
return connection.Channel()
}
//
// Declare
// @desc 声明
// @receiver c *Client
// @param declaration Declaration
// @return error
//
func (c *Client) Declare(declaration Declaration) error {
channel, err := c.Channel()
if err != nil {
return err
}
defer func() {
_ = channel.Close()
}()
return declaration(channel)
}
//
// State
// @desc 获取客户端状态
// @receiver c *Client
// @return int8
//
func (c *Client) State() int8 {
c.locker.RLock()
defer c.locker.RUnlock()
return c.state
}
//
// Store
// @desc 恢复
// @receiver c *Client
// @return error
//
func (c *Client) Store() error {
c.locker.RLock()
if c.state != StateClosed {
c.locker.RUnlock()
return ErrClientNotClosed
}
c.state = StateInit
c.clientCloseChan = make(chan struct{})
c.locker.RUnlock()
if _, err := c.createConnection(); err != nil {
return err
}
return nil
}
//
// Close
// @desc 关闭客户端
// @receiver c *Client
//
func (c *Client) Close() {
c.locker.Lock()
defer c.locker.Unlock()
if c.state == StateClosed {
return
}
close(c.clientCloseChan)
if c.connection != nil {
_ = c.connection.Close()
}
c.state = StateClosed
}
//
// NewClient
// @desc 创建客户端实例
// @param dsn string
// @param options []ClientOption
// @return *Client
//
func NewClient(dsn string, options ...ClientOption) *Client {
c := &Client{
dsn: dsn,
channelMax: defaultChannelMax,
heartbeat: defaultHeartbeat,
retryInterval: defalutRetryInterval,
clientCloseChan: make(chan struct{}),
syncWorkGroup: new(singleflight.Group),
state: StateInit,
}
for _, option := range options {
option(c)
}
return c
}
//
// WithClientVhost
// @desc 配置虚拟主机
// @param vhost string
// @return ClientOption
//
func WithClientVhost(vhost string) ClientOption {
return func(c *Client) {
c.vhost = vhost
}
}
//
// WithClientChannelMax
// @desc 配置最大 CHANNEL 数量
// @param channelMax int
// @return ClientOption
//
func WithClientChannelMax(channelMax int) ClientOption {
return func(c *Client) {
c.channelMax = channelMax
}
}
//
// WithClientHeartbeat
// @desc 配置心跳间隔
// @param heartbeat time.Duration
// @return ClientOption
//
func WithClientHeartbeat(heartbeat time.Duration) ClientOption {
return func(c *Client) {
c.heartbeat = heartbeat
}
}
//
// WithClientRetryInterval
// @desc 配置重连间隔
// @param retryInterval time.Duration
// @return ClientOption
//
func WithClientRetryInterval(retryInterval time.Duration) ClientOption {
return func(c *Client) {
c.retryInterval = retryInterval
}
}