| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package rabbitpubsub |
| 16 |
|
| 17 |
// This file implements a fake for the parts of the AMQP protocol used by our RabbitMQ |
| 18 |
// implementation. |
| 19 |
|
| 20 |
import ( |
| 21 |
"context" |
| 22 |
"fmt" |
| 23 |
"sync" |
| 24 |
"time" |
| 25 |
|
| 26 |
amqp "github.com/rabbitmq/amqp091-go" |
| 27 |
) |
| 28 |
|
| 29 |
// fakeConnection implements the amqpConnection interface. |
| 30 |
// It also doubles as the state of the fake server. |
| 31 |
type fakeConnection struct { |
| 32 |
mu sync.Mutex |
| 33 |
closed chan struct{} |
| 34 |
exchanges map[string]*exchange // exchange names are server-scoped |
| 35 |
queues map[string]*queue // queue names are server-scoped |
| 36 |
} |
| 37 |
|
| 38 |
// fakeChannel implements the amqpChannel interface. |
| 39 |
type fakeChannel struct { |
| 40 |
conn *fakeConnection |
| 41 |
// The following fields are protected by conn.mu. |
| 42 |
deliveryTag uint64 // counter; used to distinguish published messages |
| 43 |
pubChans []chan<- amqp.Confirmation |
| 44 |
returnChans []chan<- amqp.Return |
| 45 |
closeChans []chan<- *amqp.Error |
| 46 |
consumerCancels map[string]func() // from consumer name to cancel func for the context |
| 47 |
|
| 48 |
closeMu sync.Mutex |
| 49 |
closed chan struct{} |
| 50 |
} |
| 51 |
|
| 52 |
// An exchange is a collection of queues. |
| 53 |
// Every queue is also in the fakeConnection.queues map, so they can be looked up |
| 54 |
// by name. An exchange needs a list of its own queues (the ones bound to it) so |
| 55 |
// it can deliver incoming messages to them. |
| 56 |
type exchange struct { |
| 57 |
queues []*queue |
| 58 |
} |
| 59 |
|
| 60 |
// A queue holds a set of messages to be delivered. |
| 61 |
type queue struct { |
| 62 |
messages []amqp.Delivery |
| 63 |
pendingAck map[uint64]amqp.Delivery |
| 64 |
} |
| 65 |
|
| 66 |
func newFakeConnection() *fakeConnection { |
| 67 |
return &fakeConnection{ |
| 68 |
exchanges: map[string]*exchange{}, |
| 69 |
queues: map[string]*queue{}, |
| 70 |
closed: make(chan struct{}), |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
// Channel creates a new AMQP fake channel. |
| 75 |
func (c *fakeConnection) Channel() (amqpChannel, error) { |
| 76 |
if chanIsClosed(c.closed) { |
| 77 |
return nil, amqp.ErrClosed |
| 78 |
} |
| 79 |
return &fakeChannel{ |
| 80 |
conn: c, |
| 81 |
consumerCancels: map[string]func(){}, |
| 82 |
closed: make(chan struct{}), |
| 83 |
}, nil |
| 84 |
} |
| 85 |
|
| 86 |
func (c *fakeConnection) Close() error { |
| 87 |
c.mu.Lock() |
| 88 |
defer c.mu.Unlock() |
| 89 |
closeChan(c.closed) |
| 90 |
return nil |
| 91 |
} |
| 92 |
|
| 93 |
// getExchange returns the named exchange, or error if it doesn't exist. |
| 94 |
// It closes the channel on error. |
| 95 |
// It must be called with the lock held. |
| 96 |
func (ch *fakeChannel) getExchange(name string) (*exchange, error) { |
| 97 |
if ex := ch.conn.exchanges[name]; ex != nil { |
| 98 |
return ex, nil |
| 99 |
} |
| 100 |
return nil, ch.errorf(amqp.NotFound, "exchange %q not found", name) |
| 101 |
} |
| 102 |
|
| 103 |
// errorf returns an amqp.Error and closes the channel. (In the AMQP protocol, any channel error |
| 104 |
// closes the channel and makes it unusable.) |
| 105 |
// It must be called with ch.conn.mu held. |
| 106 |
func (ch *fakeChannel) errorf(code int, reasonFormat string, args ...any) error { |
| 107 |
_ = ch.Close() |
| 108 |
return &amqp.Error{Code: code, Reason: fmt.Sprintf(reasonFormat, args...)} |
| 109 |
} |
| 110 |
|
| 111 |
// Report whether the channel or its connection is closed. Does not require the lock. |
| 112 |
func (ch *fakeChannel) isClosed() bool { |
| 113 |
return chanIsClosed(ch.closed) || chanIsClosed(ch.conn.closed) |
| 114 |
} |
| 115 |
|
| 116 |
// ExchangeDeclare creates a new exchange with the given name if one doesn't already |
| 117 |
// exist. |
| 118 |
func (ch *fakeChannel) ExchangeDeclare(name string) error { |
| 119 |
if ch.isClosed() { |
| 120 |
return amqp.ErrClosed |
| 121 |
} |
| 122 |
|
| 123 |
ch.conn.mu.Lock() |
| 124 |
defer ch.conn.mu.Unlock() |
| 125 |
|
| 126 |
if _, ok := ch.conn.exchanges[name]; !ok { |
| 127 |
ch.conn.exchanges[name] = &exchange{} |
| 128 |
} |
| 129 |
return nil |
| 130 |
} |
| 131 |
|
| 132 |
// QueueDeclareAndBind binds a queue to the given exchange. |
| 133 |
// The exchange must exist. |
| 134 |
// If the queue doesn't exist, it's created. |
| 135 |
func (ch *fakeChannel) QueueDeclareAndBind(queueName, exchangeName string) error { |
| 136 |
if ch.isClosed() { |
| 137 |
return amqp.ErrClosed |
| 138 |
} |
| 139 |
ch.conn.mu.Lock() |
| 140 |
defer ch.conn.mu.Unlock() |
| 141 |
|
| 142 |
ex, err := ch.getExchange(exchangeName) |
| 143 |
if err != nil { |
| 144 |
return err |
| 145 |
} |
| 146 |
if _, ok := ch.conn.queues[queueName]; ok { |
| 147 |
return nil |
| 148 |
} |
| 149 |
q := &queue{pendingAck: map[uint64]amqp.Delivery{}} |
| 150 |
ch.conn.queues[queueName] = q |
| 151 |
ex.queues = append(ex.queues, q) |
| 152 |
return nil |
| 153 |
} |
| 154 |
|
| 155 |
func (ch *fakeChannel) Publish(exchangeName, routingKey string, pub amqp.Publishing) error { |
| 156 |
return ch.PublishWithContext(context.Background(), exchangeName, routingKey, pub) |
| 157 |
} |
| 158 |
|
| 159 |
func (ch *fakeChannel) PublishWithContext(ctx context.Context, |
| 160 |
exchangeName, routingKey string, |
| 161 |
pub amqp.Publishing, |
| 162 |
) error { |
| 163 |
if ch.isClosed() { |
| 164 |
return amqp.ErrClosed |
| 165 |
} |
| 166 |
|
| 167 |
ch.conn.mu.Lock() |
| 168 |
defer ch.conn.mu.Unlock() |
| 169 |
|
| 170 |
ex, err := ch.getExchange(exchangeName) |
| 171 |
if err != nil { |
| 172 |
return err |
| 173 |
} |
| 174 |
if len(ex.queues) == 0 { |
| 175 |
// The message is unroutable. Send a Return to all channels registered with |
| 176 |
// NotifyReturn. |
| 177 |
ret := amqp.Return{ |
| 178 |
Exchange: exchangeName, |
| 179 |
ReplyCode: amqp.NoRoute, |
| 180 |
ReplyText: "NO_ROUTE: no queues bound to exchange", |
| 181 |
RoutingKey: routingKey, |
| 182 |
} |
| 183 |
for _, c := range ch.returnChans { |
| 184 |
select { |
| 185 |
case c <- ret: |
| 186 |
case <-ch.closed: |
| 187 |
return amqp.ErrClosed |
| 188 |
case <-ch.conn.closed: |
| 189 |
return amqp.ErrClosed |
| 190 |
} |
| 191 |
} |
| 192 |
} else { |
| 193 |
// Each published message in the channel gets a new delivery tag, starting at 1. |
| 194 |
ch.deliveryTag++ |
| 195 |
// Convert the Publishing into a Delivery. |
| 196 |
del := amqp.Delivery{ |
| 197 |
Headers: pub.Headers, |
| 198 |
Body: pub.Body, |
| 199 |
DeliveryTag: ch.deliveryTag, |
| 200 |
// We don't care about the other fields. |
| 201 |
} |
| 202 |
// All exchanges are "fanout" exchanges, so the message is sent to all queues. |
| 203 |
for _, q := range ex.queues { |
| 204 |
q.messages = append(q.messages, del) |
| 205 |
} |
| 206 |
} |
| 207 |
// Every Go channel registered with NotifyPublish gets a confirmation message. |
| 208 |
// Ack is true even if the message was unroutable. |
| 209 |
for _, c := range ch.pubChans { |
| 210 |
select { |
| 211 |
case c <- amqp.Confirmation{DeliveryTag: ch.deliveryTag, Ack: true}: |
| 212 |
case <-ch.closed: |
| 213 |
return amqp.ErrClosed |
| 214 |
case <-ch.conn.closed: |
| 215 |
return amqp.ErrClosed |
| 216 |
} |
| 217 |
} |
| 218 |
return nil |
| 219 |
} |
| 220 |
|
| 221 |
// Consume starts a consumer that reads from the given queue. |
| 222 |
// The consumerName can be used in a Cancel call to stop the consumer. |
| 223 |
func (ch *fakeChannel) Consume(queueName, consumerName string) (<-chan amqp.Delivery, error) { |
| 224 |
if ch.isClosed() { |
| 225 |
return nil, amqp.ErrClosed |
| 226 |
} |
| 227 |
ch.conn.mu.Lock() |
| 228 |
defer ch.conn.mu.Unlock() |
| 229 |
|
| 230 |
q, ok := ch.conn.queues[queueName] |
| 231 |
if !ok { |
| 232 |
return nil, ch.errorf(amqp.NotFound, "queue %q not found", queueName) |
| 233 |
} |
| 234 |
if _, ok := ch.consumerCancels[consumerName]; ok { |
| 235 |
return nil, ch.errorf(amqp.PreconditionFailed, "consumer %q already exists", consumerName) |
| 236 |
} |
| 237 |
ctx, cancel := context.WithCancel(context.Background()) |
| 238 |
ch.consumerCancels[consumerName] = cancel // used by fakeChannel.Cancel |
| 239 |
delc := make(chan amqp.Delivery) |
| 240 |
go func() { |
| 241 |
// For this simple fake, just deliver one message every once in a while if |
| 242 |
// any are available, until the consumer is canceled. |
| 243 |
for { |
| 244 |
m, ok := ch.takeOneMessage(q) |
| 245 |
if ok { |
| 246 |
select { |
| 247 |
case delc <- m: |
| 248 |
case <-ctx.Done(): |
| 249 |
// ignore errors here and below |
| 250 |
return |
| 251 |
case <-ch.closed: |
| 252 |
return |
| 253 |
case <-ch.conn.closed: |
| 254 |
return |
| 255 |
} |
| 256 |
} |
| 257 |
select { |
| 258 |
case <-time.After(10 * time.Millisecond): |
| 259 |
case <-ctx.Done(): |
| 260 |
// ignore error |
| 261 |
return |
| 262 |
} |
| 263 |
} |
| 264 |
}() |
| 265 |
return delc, nil |
| 266 |
} |
| 267 |
|
| 268 |
// Take a message from q, if one is available. We just remove |
| 269 |
// the message from the queue permanently. In a more sophisticated implementation |
| 270 |
// we'd mark it as outstanding and keep it around until it got acked, but we don't |
| 271 |
// need acks for this fake. |
| 272 |
func (ch *fakeChannel) takeOneMessage(q *queue) (amqp.Delivery, bool) { |
| 273 |
ch.conn.mu.Lock() |
| 274 |
defer ch.conn.mu.Unlock() |
| 275 |
if len(q.messages) == 0 { |
| 276 |
return amqp.Delivery{}, false |
| 277 |
} |
| 278 |
m := q.messages[0] |
| 279 |
q.messages = q.messages[1:] |
| 280 |
q.pendingAck[m.DeliveryTag] = m |
| 281 |
return m, true |
| 282 |
} |
| 283 |
|
| 284 |
// Ack removes the message from q.pendingAck. |
| 285 |
func (ch *fakeChannel) Ack(tag uint64) error { |
| 286 |
if ch.isClosed() { |
| 287 |
return amqp.ErrClosed |
| 288 |
} |
| 289 |
ch.conn.mu.Lock() |
| 290 |
defer ch.conn.mu.Unlock() |
| 291 |
for _, q := range ch.conn.queues { |
| 292 |
if _, ok := q.pendingAck[tag]; ok { |
| 293 |
delete(q.pendingAck, tag) |
| 294 |
return nil |
| 295 |
} |
| 296 |
} |
| 297 |
// No error if we couldn't find it to ack it. |
| 298 |
return nil |
| 299 |
} |
| 300 |
|
| 301 |
// Nack moves the message from q.pendingAck back to q.messages |
| 302 |
// to be redelivered. |
| 303 |
func (ch *fakeChannel) Nack(tag uint64) error { |
| 304 |
if ch.isClosed() { |
| 305 |
return amqp.ErrClosed |
| 306 |
} |
| 307 |
ch.conn.mu.Lock() |
| 308 |
defer ch.conn.mu.Unlock() |
| 309 |
for _, q := range ch.conn.queues { |
| 310 |
if m, ok := q.pendingAck[tag]; ok { |
| 311 |
delete(q.pendingAck, tag) |
| 312 |
q.messages = append(q.messages, m) |
| 313 |
return nil |
| 314 |
} |
| 315 |
} |
| 316 |
// No error if we couldn't find it to nack it. |
| 317 |
return nil |
| 318 |
} |
| 319 |
|
| 320 |
// Cancel stops the consumer's goroutine. |
| 321 |
func (ch *fakeChannel) Cancel(consumerName string) error { |
| 322 |
if ch.isClosed() { |
| 323 |
return amqp.ErrClosed |
| 324 |
} |
| 325 |
ch.conn.mu.Lock() |
| 326 |
defer ch.conn.mu.Unlock() |
| 327 |
|
| 328 |
cancel, ok := ch.consumerCancels[consumerName] |
| 329 |
if !ok { |
| 330 |
return ch.errorf(amqp.NotFound, "consumer %q not found", consumerName) |
| 331 |
} |
| 332 |
cancel() |
| 333 |
delete(ch.consumerCancels, consumerName) |
| 334 |
return nil |
| 335 |
} |
| 336 |
|
| 337 |
// NotifyPublish remembers its argument channel so it can be notified for every |
| 338 |
// published message. It returns its argument. |
| 339 |
func (ch *fakeChannel) NotifyPublish(c chan amqp.Confirmation) chan amqp.Confirmation { |
| 340 |
ch.conn.mu.Lock() |
| 341 |
defer ch.conn.mu.Unlock() |
| 342 |
ch.pubChans = append(ch.pubChans, c) |
| 343 |
return c |
| 344 |
} |
| 345 |
|
| 346 |
// NotifyReturn remembers its argument channel so it can be notified for every |
| 347 |
// published message that's returned due to being unroutable. |
| 348 |
func (ch *fakeChannel) NotifyReturn(c chan amqp.Return) chan amqp.Return { |
| 349 |
ch.conn.mu.Lock() |
| 350 |
defer ch.conn.mu.Unlock() |
| 351 |
ch.returnChans = append(ch.returnChans, c) |
| 352 |
return c |
| 353 |
} |
| 354 |
|
| 355 |
// NotifyClose remembers its argument channel so it can be notified when |
| 356 |
// the channel is closed. |
| 357 |
func (ch *fakeChannel) NotifyClose(c chan *amqp.Error) chan *amqp.Error { |
| 358 |
ch.conn.mu.Lock() |
| 359 |
defer ch.conn.mu.Unlock() |
| 360 |
ch.closeChans = append(ch.closeChans, c) |
| 361 |
return c |
| 362 |
} |
| 363 |
|
| 364 |
// Close marks the fakeChannel as closed and sends an error to all channels |
| 365 |
// registered with NotifyClose. |
| 366 |
func (ch *fakeChannel) Close() error { |
| 367 |
if chanIsClosed(ch.conn.closed) { |
| 368 |
return amqp.ErrClosed |
| 369 |
} |
| 370 |
ch.closeMu.Lock() |
| 371 |
closeChans := ch.closeChans |
| 372 |
closeChan(ch.closed) |
| 373 |
ch.closeMu.Unlock() |
| 374 |
for _, c := range closeChans { |
| 375 |
// Don't block on notifying. |
| 376 |
select { |
| 377 |
case c <- amqp.ErrClosed: |
| 378 |
default: |
| 379 |
} |
| 380 |
} |
| 381 |
return nil |
| 382 |
} |
| 383 |
|
| 384 |
func (ch *fakeChannel) ExchangeDelete(name string) error { |
| 385 |
ch.conn.mu.Lock() |
| 386 |
defer ch.conn.mu.Unlock() |
| 387 |
|
| 388 |
delete(ch.conn.exchanges, name) |
| 389 |
return nil |
| 390 |
} |
| 391 |
|
| 392 |
func (ch *fakeChannel) QueueDelete(name string) error { |
| 393 |
ch.conn.mu.Lock() |
| 394 |
defer ch.conn.mu.Unlock() |
| 395 |
|
| 396 |
delete(ch.conn.queues, name) |
| 397 |
return nil |
| 398 |
} |
| 399 |
|
| 400 |
func (ch *fakeChannel) Qos(_, _ int, _ bool) error { |
| 401 |
if ch.isClosed() { |
| 402 |
return amqp.ErrClosed |
| 403 |
} |
| 404 |
|
| 405 |
return nil |
| 406 |
} |
| 407 |
|
| 408 |
// Assumes nothing is ever written to the channel. |
| 409 |
func chanIsClosed(ch chan struct{}) bool { |
| 410 |
select { |
| 411 |
case <-ch: |
| 412 |
return true |
| 413 |
default: |
| 414 |
return false |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
// Avoid panic when closing a closed channel. |
| 419 |
// Must be called with the lock held. |
| 420 |
func closeChan(ch chan struct{}) { |
| 421 |
select { |
| 422 |
case <-ch: |
| 423 |
default: |
| 424 |
close(ch) |
| 425 |
} |
| 426 |
} |