This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
/
publisher.go
199 lines (173 loc) · 4.65 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
package axrmq
import (
"context"
"errors"
"github.com/streadway/amqp"
)
// publisher publishes AMQP messages reliably using AMQP "publisher confirms".
// https://www.rabbitmq.com/confirms.html#publisher-confirms
//
// It maintains a capped-size pool of AMQP channels which are placed into
// "confirm mode" when they are first created.
type publisher struct {
conn *amqp.Connection
ep string
channels chan *channel
}
// newPublisher returns a new publisher with a channel pool
func newPublisher(
conn *amqp.Connection,
ep string,
poolSize int,
) *publisher {
return &publisher{
conn,
ep,
make(chan *channel, poolSize),
}
}
// channel is a handle to an AMQP channel that has been placed into "confirm
// mode", along with (Go) channels used to signal when confirmations have been
// received from the broker.
type channel struct {
Channel *amqp.Channel
Close chan *amqp.Error
Return chan amqp.Return
Confirm chan amqp.Confirmation
}
// PublishUnicast sends a unicast message directly to a specific endpoint.
func (p *publisher) PublishUnicast(ctx context.Context, pub amqp.Publishing, ep string) error {
return p.publish(
ctx,
unicastExchange,
unicastRoutingKey(pub.Type, ep),
true, // mandatory
pub,
)
}
// PublishMulticast sends a multicast message to the its subscribers.
func (p *publisher) PublishMulticast(ctx context.Context, pub amqp.Publishing) error {
return p.publish(
ctx,
multicastExchange,
multicastRoutingKey(pub.Type),
false, // mandatory
pub,
)
}
func (p *publisher) RepublishAsError(ctx context.Context, del amqp.Delivery) error {
_, queue := queueNames(p.ep)
return p.publish(
ctx,
"",
queue,
true, // mandatory
amqp.Publishing{
AppId: del.AppId,
MessageId: del.MessageId,
ReplyTo: del.ReplyTo,
CorrelationId: del.CorrelationId,
Timestamp: del.Timestamp,
Type: del.Type,
ContentType: del.ContentType,
Body: del.Body,
},
)
}
// publish sends a message to the broker, and blocks until a confirmation is
// received.
//
// It returns an error if the broker does not acknowledge publication of the
// message. Otherwise it has the same behavior as amqp.Channel.Publish().
func (p *publisher) publish(
ctx context.Context,
exchange string,
key string,
mandatory bool,
msg amqp.Publishing,
) error {
msg.DeliveryMode = 2 // persistent
ch, err := p.acquire()
if err != nil {
return err
}
if err := ch.Channel.Publish(
exchange,
key,
mandatory,
false, // immediate
msg,
); err != nil {
return err
}
select {
case r := <-ch.Return:
go p.confirmThenRelease(ch)
return errors.New("broker could not route message, " + r.ReplyText)
case c := <-ch.Confirm:
// wait for a confirmation from the broker, once we receive one
// (regardless of whether it's an ack or a nack) we can return the
// channel to the pool
p.release(ch)
if c.Ack {
return nil
}
// there's no more meaningful error to be returned here. The RMQ docs
// simply say: "basic.nack will only be delivered if an internal error
// occurs in the Erlang process responsible for a queue."
return errors.New("broker did not confirm message publication")
case err := <-ch.Close:
// if the channel is closed before we receive the confirmation, we do
// not return the channel to the pool
return err
case <-ctx.Done():
// if our context is canceled before we receive the confirmation, return
// the channel the pool only after our confirmation has been consumed.
go p.confirmThenRelease(ch)
return ctx.Err()
}
}
// acquire gets a channel from the pool, or opens a new channel and places it
// into "confirm mode" if the pool is empty.
func (p *publisher) acquire() (*channel, error) {
select {
case ch := <-p.channels:
return ch, nil
default:
}
c, err := p.conn.Channel()
if err != nil {
return nil, err
}
if err := c.Confirm(false); err != nil { // false = noWait
return nil, err
}
ch := &channel{
Channel: c,
Close: make(chan *amqp.Error),
Return: make(chan amqp.Return, 1),
Confirm: make(chan amqp.Confirmation, 1),
}
c.NotifyClose(ch.Close)
c.NotifyReturn(ch.Return)
c.NotifyPublish(ch.Confirm)
return ch, nil
}
// release returns a channel to the pool, or closes it if the pool is full.
func (p *publisher) release(ch *channel) {
select {
case p.channels <- ch:
default:
_ = ch.Channel.Close()
}
}
// confirmThenRelease waits for the next confirm on ch before returning it to
// the pool. This ensures that some future publisher doesn't see a previous
// call's confirmation message as its own.
func (p *publisher) confirmThenRelease(ch *channel) {
select {
case <-ch.Confirm:
p.release(ch)
case <-ch.Close:
}
}