-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
241 lines (208 loc) · 5.3 KB
/
client.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
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
package grmq
import (
"context"
"sync"
"sync/atomic"
"time"
"github.com/pkg/errors"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/txix-open/grmq/consumer"
"github.com/txix-open/grmq/publisher"
"github.com/txix-open/grmq/topology"
)
type closer interface {
Close() error
}
type DialConfig struct {
amqp.Config
DialTimeout time.Duration
}
type Client struct {
url string
dialConfig DialConfig
consumers []consumer.Consumer
publishers []*publisher.Publisher
declarations topology.Declarations
reconnectTimeout time.Duration
observer Observer
mustReconnect *atomic.Bool
reportErrOnce *sync.Once
firstOccurredError chan error
close chan struct{}
shutdownDone chan struct{}
}
func New(url string, options ...ClientOption) *Client {
mustReconnect := &atomic.Bool{}
mustReconnect.Store(true)
c := &Client{
url: url,
dialConfig: DialConfig{
Config: amqp.Config{
Heartbeat: 10 * time.Second,
Locale: "en_US",
},
DialTimeout: 30 * time.Second,
},
reconnectTimeout: 1 * time.Second,
mustReconnect: mustReconnect,
reportErrOnce: &sync.Once{},
firstOccurredError: make(chan error, 1),
close: make(chan struct{}),
shutdownDone: make(chan struct{}),
observer: NoopObserver{},
}
for _, opt := range options {
opt(c)
}
if c.dialConfig.Dial == nil {
timeout := c.dialConfig.DialTimeout
if timeout == 0 {
timeout = 30 * time.Second
}
c.dialConfig.Dial = amqp.DefaultDial(timeout)
}
return c
}
// Run
// Block and wait first successfully established session
// It means all declarations were applied successfully
// All publishers were initialized
// All consumers were run
// Returns first occurred error during first session opening or nil
func (s *Client) Run(ctx context.Context) error {
go s.run(ctx)
select {
case <-ctx.Done():
return ctx.Err()
case err := <-s.firstOccurredError:
if err != nil {
s.mustReconnect.Store(false)
}
return err
}
}
// Serve
// Similar to Run but doesn't wait first successful session
// Just pass to Observer occurred errors and retry
func (s *Client) Serve(ctx context.Context) {
go s.run(ctx)
}
func (s *Client) run(ctx context.Context) {
defer func() {
close(s.shutdownDone)
}()
for {
err := s.runSession()
if err == nil { //normal close
return
}
s.observer.ClientError(err)
if !s.mustReconnect.Load() {
return //prevent goroutine leak for Run if error occurred
}
select {
case <-ctx.Done():
s.observer.ClientError(ctx.Err())
return
case <-s.close:
return //shutdown called
case <-time.After(s.reconnectTimeout):
}
}
}
func (s *Client) runSession() (err error) {
defer func() {
if err != nil {
s.reportFirstOccurredErrorOnes(err)
}
}()
conn, err := amqp.DialConfig(s.url, s.dialConfig.Config)
if err != nil {
return errors.WithMessage(err, "dial")
}
defer conn.Close()
ch, err := conn.Channel()
if err != nil {
return errors.WithMessage(err, "create channel for declarator")
}
declarator := NewDeclarator(s.declarations, ch)
err = declarator.Run()
if err != nil {
return errors.WithMessage(err, "run declarator")
}
err = declarator.Close()
if err != nil {
return errors.WithMessage(err, "close declarator")
}
closers := make([]closer, 0)
defer func() {
for i := len(closers); i > 0; i-- {
_ = closers[i-1].Close()
}
}()
for _, publisher := range s.publishers {
publisherUnit, err := s.runPublisher(publisher, conn)
if err != nil {
return errors.WithMessagef(err, "run publisher exchange: '%s', routingKey: '%s'", publisher.Exchange, publisher.RoutingKey)
}
closers = append(closers, publisherUnit)
}
for _, consumer := range s.consumers {
ch, err = conn.Channel()
if err != nil {
return errors.WithMessagef(err, "create channel for consumer '%s'", consumer.Queue)
}
var retryPub *Publisher
if consumer.RetryPolicy != nil {
retryPub, err = s.runPublisher(publisher.New("", ""), conn)
if err != nil {
return errors.WithMessagef(err, "run retry publisher for consumer '%s'", consumer.Queue)
}
closers = append(closers, retryPub)
}
consumerUnit := NewConsumer(consumer, ch, retryPub, s.observer)
err = consumerUnit.Run()
if err != nil {
return errors.WithMessagef(err, "run consumer '%s'", consumer.Queue)
}
closers = append(closers, consumerUnit)
}
s.reportFirstOccurredErrorOnes(nil) //to unblock Run
s.observer.ClientReady()
connCloseChan := make(chan *amqp.Error, 1)
connCloseChan = conn.NotifyClose(connCloseChan)
select {
case err, isOpen := <-connCloseChan:
if !isOpen {
return nil
}
return err
case <-s.close:
return nil
}
}
// Shutdown
// Perform graceful shutdown
func (s *Client) Shutdown() {
close(s.close)
s.observer.ShutdownStarted()
<-s.shutdownDone
s.observer.ShutdownDone()
}
func (s *Client) runPublisher(publisher *publisher.Publisher, conn *amqp.Connection) (*Publisher, error) {
ch, err := conn.Channel()
if err != nil {
return nil, errors.WithMessage(err, "create channel")
}
publisherUnit := NewPublisher(publisher, ch, s.observer)
err = publisherUnit.Run()
if err != nil {
return nil, errors.WithMessage(err, "run publisher")
}
return publisherUnit, nil
}
func (s *Client) reportFirstOccurredErrorOnes(err error) {
s.reportErrOnce.Do(func() {
s.firstOccurredError <- err
})
}