-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
160 lines (135 loc) · 3.19 KB
/
client.go
File metadata and controls
160 lines (135 loc) · 3.19 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
package amqpx
import (
"context"
"fmt"
"sync"
"time"
"github.com/rabbitmq/amqp091-go"
"golang.org/x/sync/semaphore"
)
const (
defaultConnectTimeout = time.Second * 4
)
// A Client represents connection to rabbitmq.
type Client struct {
dialer dialer
mx sync.RWMutex
amqpConn Connection
marshaler Marshaler
unmarshaler map[string]Unmarshaler
notifyClose chan *amqp091.Error
wg *sync.WaitGroup
done context.Context
cancel context.CancelFunc
logger LogFunc
wrapConsume []ConsumeInterceptor
wrapPublish []PublishInterceptor
}
// Connect creates a connection.
func Connect(opts ...ClientOption) (*Client, error) {
opt := newClientOptions()
for _, o := range opts {
o(&opt)
}
if err := opt.validate(); err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), defaultConnectTimeout)
defer cancel()
amqpConn, err := opt.dialer.Dial(ctx)
if err != nil {
return nil, err
}
conn := &Client{
dialer: opt.dialer,
amqpConn: amqpConn,
marshaler: opt.marshaler,
unmarshaler: opt.unmarshaler,
notifyClose: amqpConn.NotifyClose(make(chan *amqp091.Error, 1)),
wg: &sync.WaitGroup{},
logger: opt.logger,
wrapConsume: opt.wrapConsume,
wrapPublish: opt.wrapPublish,
}
conn.done, conn.cancel = context.WithCancel(context.Background())
go conn.loop()
return conn, nil
}
// IsConnOpen returns true if the connection is open.
func (c *Client) IsConnOpen() bool {
c.mx.RLock()
defer c.mx.RUnlock()
return !c.amqpConn.IsClosed()
}
// NewConsumer creates a consumer.
func (c *Client) NewConsumer(queue string, fn HandlerValue, opts ...ConsumerOption) error {
opt := consumerOptions{
interceptor: c.wrapConsume,
unmarshaler: c.unmarshaler,
}
for _, o := range opts {
o(&opt)
}
if err := opt.validate(fn); err != nil {
return fmt.Errorf("amqpx: queue %q consumer-tag %q: %s", queue, opt.tag, err)
}
fn.init(opt.unmarshaler)
cons := &consumer{
conn: c.conn,
queue: queue,
tag: opt.tag,
opts: opt.channel,
log: c.logger,
limit: semaphore.NewWeighted(int64(opt.concurrency)),
wg: c.wg,
fn: fn.serve,
done: c.done,
}
// wrap the end fn with the interceptor chain.
if len(opt.interceptor) != 0 {
cons.fn = opt.interceptor[len(opt.interceptor)-1](cons.fn)
for i := len(opt.interceptor) - 2; i >= 0; i-- {
cons.fn = opt.interceptor[i](cons.fn)
}
}
if err := cons.initChannel(); err != nil {
return fmt.Errorf("amqpx: queue %q consumer-tag %q: %s", cons.queue, cons.tag, err)
}
go cons.serve()
return nil
}
// Close closes Connection.
// Waits all consumers.
func (c *Client) Close() {
c.cancel()
c.wg.Wait()
c.mx.Lock()
defer c.mx.Unlock()
c.amqpConn.Close()
}
func (c *Client) setConn(conn Connection) {
c.mx.Lock()
defer c.mx.Unlock()
c.amqpConn.Close()
c.amqpConn = conn
c.notifyClose = c.amqpConn.NotifyClose(make(chan *amqp091.Error, 1))
}
func (c *Client) conn() Connection {
c.mx.RLock()
defer c.mx.RUnlock()
return c.amqpConn
}
func (c *Client) loop() {
for {
select {
case <-c.done.Done():
return
case <-c.notifyClose:
conn, err := c.dialer.Dial(c.done)
if err != nil {
return
}
c.setConn(conn)
}
}
}