-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
377 lines (330 loc) · 9.31 KB
/
publisher.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
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
package cony
import (
"sync/atomic"
"time"
"github.com/pkg/errors"
"github.com/davecgh/go-spew/spew"
"github.com/streadway/amqp"
)
const defaultConfirmTimeout = 2 * time.Second
var (
// ErrPublisherStopped is an indicator that publisher is stopped
// could be returned from Write() and Publish() methods
ErrPublisherStopped = errors.New("publisher stopped")
ErrConfirmTimeOut = errors.New("confirm timed out")
ErrPublishTimeOut = errors.New("publish timed out")
ErrPublishingNotConfirmed = errors.New("publishing NOT confirmed")
ErrPublisherServingStopped = errors.New("publisher serving stopped")
)
// PublisherOpt is a functional option type for Publisher
type PublisherOpt func(*Publisher)
type publishMaybeErr struct {
pub *amqp.Publishing
err chan error
key string
exchange string
}
// Publisher hold definition for AMQP publishing
type Publisher struct {
exchange string
key string
tmpl amqp.Publishing
pubChan chan *publishMaybeErr
errs chan error
stopCh chan struct{}
servingFlag *uint32
stopServingCh chan chan struct{} // used to just stopping serving by client
sync chan struct{} // sync channel is used to synchronize important operations
confirmTimeout time.Duration
logger Logger
}
// Errors returns channel with AMQP channel level errors
func (p *Publisher) Errors() <-chan error {
return p.errs
}
// StopCh returns channel which will be closed when publisher stopped
func (p *Publisher) StopCh() <-chan struct{} {
return p.stopCh
}
func (p *Publisher) Exchange() string {
return p.exchange
}
func (p *Publisher) RoutingKey() string {
return p.key
}
func (p *Publisher) ConfirmTimeout() time.Duration {
return p.confirmTimeout
}
// Serving returns if this publisher is serving at the moment
func (p *Publisher) Serving() bool {
return atomic.LoadUint32(p.servingFlag) == serving
}
func (p *Publisher) setServing() {
atomic.StoreUint32(p.servingFlag, serving)
}
func (p *Publisher) unsetServing() {
atomic.StoreUint32(p.servingFlag, 0)
}
// Cancel this publisher.
//
// This will CLOSE Deliveries() channel
func (p *Publisher) stop() {
if !p.Stopped() {
close(p.stopCh)
p.logger.Warn("Publisher stopped")
}
}
// Canceled checks if Consumer is cancelled
func (p *Publisher) Stopped() bool {
select {
case <-p.stopCh:
return true
default:
return false
}
}
func (p *Publisher) reportErr(err error) bool {
if err != nil {
select {
case p.errs <- err:
default:
}
return true
}
return false
}
// Template will be used, input buffer will be added as Publishing.Body.
// return int will always be len(b)
//
// Implements io.Writer
//
// WARNING: this is blocking call, it will not return until connection is
// available. The only way to stop it is to use Cancel() method.
func (p *Publisher) Write(b []byte, timeout time.Duration) (int, error) {
pub := p.tmpl
pub.Body = b
return len(b), p.Publish(&pub, timeout)
}
// PublishWithRoutingKey used to publish custom amqp.Publishing and routing key
//
// WARNING: this is blocking call, it will not return until connection is
// available. The only way to stop it is to use Cancel() method.
func (p *Publisher) PublishWithParams(pub *amqp.Publishing, exchange string, key string, timeout time.Duration) error {
reqRepl := &publishMaybeErr{
pub: pub,
err: make(chan error),
exchange: exchange,
key: key,
}
// wait for publishing
waitTimer := time.NewTimer(timeout)
defer waitTimer.Stop()
select {
case <-p.stopCh:
// received stop signal
return ErrPublisherStopped
case p.pubChan <- reqRepl:
case <-waitTimer.C:
// received timeout signal
return ErrPublishTimeOut
}
p.logger.Debug("Publishing published to internal channel")
err := <-reqRepl.err
p.logger.Debug("Publishing result ", "err ", err)
return err
}
// Publish used to publish custom amqp.Publishing
//
// WARNING: this is blocking call, it will not return until connection is
// available. The only way to stop it is to use Cancel() method.
func (p *Publisher) Publish(pub *amqp.Publishing, timeout time.Duration) error {
return p.PublishWithParams(pub, p.exchange, p.key, timeout)
}
func (p *Publisher) serve(chConstr channelConstrucor) {
p.logger.Info("Publisher serve called")
defer p.logger.Info("Publisher serve returned")
// here we try to obtain sync lock
// if another serve method running - stop that serving
// this is done in purpose to run just one serving per Publisher
syncLoop:
for {
res := make(chan struct{})
select {
case <-p.stopCh:
p.logger.Info("Publisher serve sync returned due to Publisher stopped")
return
case <-p.sync:
p.logger.Info("Publisher serve sync obtained")
break syncLoop
case p.stopServingCh <- res:
p.logger.Info("Publisher serve stopServing sent")
// receive stopServingCh response
select {
case <-p.stopCh:
p.logger.Info("Publisher serve sync returned due to Publisher stopped")
return
case <-res:
}
}
}
defer func() {
p.logger.Info("Publisher serve sync released")
p.sync <- struct{}{}
}()
if p.Stopped() {
p.reportErr(ErrPublisherStopped)
return
}
p.setServing()
defer p.unsetServing()
ch, err := chConstr()
if err != nil {
p.reportErr(errors.Wrap(err, "Publisher serve error while creating amqp channel"))
return
}
defer ch.Close()
err = ch.Confirm(false)
if err != nil {
p.reportErr(errors.Wrap(err, "Publisher serve error while enabling amqp channel confirmation"))
return
}
chanConfirms := ch.NotifyPublish(make(chan amqp.Confirmation, 1))
chanErrs := ch.NotifyClose(make(chan *amqp.Error, 1))
for {
// to exit as soon as possible
select {
case <-p.stopCh:
p.reportErr(ErrPublisherStopped)
return
default:
}
// to exit or receive stopServing as soon as possible
select {
case <-p.stopCh:
p.reportErr(ErrPublisherStopped)
return
case notify := <-p.stopServingCh:
p.logger.Warn("stopServing signal received")
p.reportErr(ErrPublisherServingStopped)
close(notify)
return
default:
}
// to exit or receive stopServing or receive NotifyClose as soon as possible
select {
case <-p.stopCh:
p.reportErr(ErrPublisherStopped)
return
case notify := <-p.stopServingCh:
p.logger.Warn("stopServing signal received")
p.reportErr(ErrPublisherServingStopped)
close(notify)
return
case amqpErr := <-chanErrs:
p.reportErr(errors.New(spew.Sdump(amqpErr)))
return
default:
}
// main select
select {
case <-p.stopCh:
p.reportErr(ErrPublisherStopped)
return
case notify := <-p.stopServingCh:
p.logger.Warn("stopServing signal received")
p.reportErr(ErrPublisherServingStopped)
close(notify)
return
case amqpErr := <-chanErrs:
p.reportErr(errors.New(spew.Sdump(amqpErr)))
return
case envelop := <-p.pubChan:
now := time.Now()
err := ch.Publish(
envelop.exchange, // exchange
envelop.key, // key
false, // mandatory
false, // immediate
*envelop.pub, // msg amqp.Publishing
)
// if some error happened return from this serving
if err != nil {
err = errors.Wrap(err, "error occurred while publising")
envelop.err <- err
p.reportErr(err)
return
}
p.logger.Info("Publishing successfully published to amqp", "(publish took)", time.Since(now))
// wait for confirm with timeout
waitTimer := time.NewTimer(p.confirmTimeout)
now = time.Now()
select {
case <-waitTimer.C:
p.logger.Info("Publishing confirm timed out", "(confirm took)", time.Since(now))
waitTimer.Stop()
err = ErrConfirmTimeOut
envelop.err <- err
p.reportErr(err)
return
case confirm := <-chanConfirms:
waitTimer.Stop()
if confirm.Ack {
p.logger.Info("Publishing successfully confirmed", "(confirm took)", time.Since(now), "deliverTag", confirm.DeliveryTag)
envelop.err <- nil // one way to continue
} else {
p.logger.Info("Publishing NOT confirmed", "(confirm took)", time.Since(now), "deliverTag", confirm.DeliveryTag)
err = ErrPublishingNotConfirmed
envelop.err <- err
p.reportErr(err)
return
}
}
}
}
}
// NewPublisher is a Publisher constructor
func NewPublisher(exchange string, key string, opts ...PublisherOpt) *Publisher {
p := &Publisher{
exchange: exchange,
key: key,
pubChan: make(chan *publishMaybeErr),
errs: make(chan error, 100),
sync: make(chan struct{}, 1),
stopCh: make(chan struct{}),
stopServingCh: make(chan chan struct{}),
servingFlag: new(uint32),
logger: noopLogger{},
confirmTimeout: defaultConfirmTimeout,
}
// init sync
p.sync <- struct{}{}
for _, o := range opts {
o(p)
}
return p
}
// PublisherLog is a functional option, used in `NewPublisher` constructor
// to set the logger
func PublisherLog(l Logger) PublisherOpt {
return func(p *Publisher) {
if l != nil {
p.logger = l
}
}
}
// PublisherConfirmTimeout is a functional option, used in `NewPublisher` constructor
// to set the confirm timeout
func PublisherConfirmTimeout(t time.Duration) PublisherOpt {
return func(p *Publisher) {
if t > 0 {
p.confirmTimeout = t
}
}
}
// PublishingTemplate Publisher's functional option. Provide template
// amqp.Publishing and save typing.
func PublishingTemplate(t amqp.Publishing) PublisherOpt {
return func(p *Publisher) {
p.tmpl = t
}
}