async-rabbitmq 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +43 -0
- data/README.md +36 -2
- data/lib/async_rabbitmq/channel.rb +193 -19
- data/lib/async_rabbitmq/notifier.rb +2 -0
- data/lib/async_rabbitmq/session.rb +9 -2
- data/lib/async_rabbitmq/version.rb +1 -1
- data/lib/async_rabbitmq.rb +2 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 42cacea6b44ce97ab03abac57c1d7b0b198103ba0225d8c9bc8aa37f6145fbb4
|
|
4
|
+
data.tar.gz: ac847f5cd9f87acaa811a539a728b5b5bb6eb3c4694b0408733775e705266676
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 709f0f1339d28fa2ed77d031f375967970cb94d8d879e230bb3c2a223c84a701573ea5ed36b3fd5dbf24eb38ce0acf83d0ec234d23da7478c383621290d48ce9
|
|
7
|
+
data.tar.gz: fbec76e2babf6f6f32ca6e7f86eea6fa2644609102a9bc7b936e72a48ee236b9d0f964b021a17846b6aac95c768072f7a943adbadb650272b4c230d7dd93f79d
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,49 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
## [0.8.0] - 2026-11-14
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- A server-sent `basic.cancel` no longer leaves the queue in the topology registry. The broker
|
|
10
|
+
cancels a consumer when it can no longer serve the queue, and one of the ways that happens is
|
|
11
|
+
somebody deleting the queue - after which the next connection recovery re-declared it and the
|
|
12
|
+
deletion never stuck. The client now checks, passively, whether the queue is really gone and
|
|
13
|
+
forgets it, with its bindings, if it is. Only `NOT_FOUND` counts as gone: a replicated queue
|
|
14
|
+
mid-election is unavailable, not deleted, and is left in the registry to be re-declared.
|
|
15
|
+
- `on_cancel` and `on_error` no longer run on the channel's dispatch fiber. The obvious thing to
|
|
16
|
+
write in either is an RPC - re-consume, reopen - and an RPC on that fiber waits for a reply only
|
|
17
|
+
that fiber can deliver: fifteen seconds to `rpc_timeout`, then a channel resync. An exception out
|
|
18
|
+
of either escaped `dispatch_loop`, whose rescue sits outside its loop, so the channel stopped
|
|
19
|
+
delivering anything at all, confirms and RPC replies included. Both now run in a task of their
|
|
20
|
+
own and an exception is logged. Bunny dispatches cancel notifications to its consumer work pool
|
|
21
|
+
and the Java client to its work service, for the same reasons. `on_error` also runs after the
|
|
22
|
+
close has released the fibers it stranded, not before.
|
|
23
|
+
- The log line for a broker-sent cancel names the queue, not just the consumer tag.
|
|
24
|
+
|
|
25
|
+
### Added
|
|
26
|
+
|
|
27
|
+
- `recover_cancelled_consumers:` on `Session.new` and `Session#open_channel`, with
|
|
28
|
+
`Channel#recover_cancelled_consumers=` and `Channel#recovers_cancelled_consumers?`. Off by
|
|
29
|
+
default, as it is in Bunny. When on, a consumer the broker cancels is re-subscribed under the
|
|
30
|
+
same tag, backing off from 1s to 30s for as long as the queue exists and the channel is open.
|
|
31
|
+
Recovery declares nothing: it passively checks that the queue is still there and re-consumes, so
|
|
32
|
+
a queue somebody deleted is never handed back. Re-declaring is the application's call.
|
|
33
|
+
- `consumer.recovered` and `consumer.recovery_failed` events. `consumer.recovery_failed` carries
|
|
34
|
+
`reason: :queue_deleted` - emitted whether or not recovery is enabled, since that is the point
|
|
35
|
+
at which a consumer is known not to be coming back - or `reason: :error` with the exception, once
|
|
36
|
+
per episode rather than once per attempt.
|
|
37
|
+
|
|
38
|
+
## [0.7.1] - 2026-10-18
|
|
39
|
+
|
|
40
|
+
### Added
|
|
41
|
+
|
|
42
|
+
- `AsyncRabbitMQ::DEFAULT_CONTENT_TYPE` and `AsyncRabbitMQ::Channel::DEFAULT_CONTENT_TYPE` constants (`"application/octet-stream"`).
|
|
43
|
+
|
|
44
|
+
### Changed
|
|
45
|
+
|
|
46
|
+
- Default publish properties aligned with Bunny parity: `content_type` defaults to `"application/octet-stream"`, `persistent` defaults to `true` (`delivery_mode: 2`), and `priority` defaults to `0`.
|
|
47
|
+
|
|
5
48
|
## [0.7.0] - 2026-10-17
|
|
6
49
|
|
|
7
50
|
### Fixed
|
data/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# async-rabbitmq
|
|
2
2
|
|
|
3
3
|
A fiber-native RabbitMQ (AMQP 0-9-1) client for Ruby, built on the amazing work
|
|
4
|
-
of [Bunny](https://github.com/ruby-amqp/bunny).
|
|
4
|
+
of [Bunny](https://github.com/ruby-amqp/bunny) and [Async](https://github.com/socketry/async).
|
|
5
5
|
|
|
6
6
|
## Installing
|
|
7
7
|
|
|
@@ -466,7 +466,39 @@ starts), `on_blocked` / `on_unblocked` (connection.blocked),
|
|
|
466
466
|
`Channel#on_return` (mandatory messages the broker could not route),
|
|
467
467
|
`on_cancel` (broker cancelled a consumer), `on_error` (broker closed the
|
|
468
468
|
channel). A broker-closed channel can be reopened in place with
|
|
469
|
-
`Channel#reopen`.
|
|
469
|
+
`Channel#reopen`. Every one of these runs in a task of its own, so a callback
|
|
470
|
+
may issue RPCs — re-consume, reopen, declare — and a callback that raises is
|
|
471
|
+
logged rather than taken out on the channel.
|
|
472
|
+
|
|
473
|
+
### A consumer the broker cancels
|
|
474
|
+
|
|
475
|
+
RabbitMQ cancels a consumer when it can no longer serve the queue to it: the
|
|
476
|
+
queue was deleted, the node holding it failed, a replicated queue's leader
|
|
477
|
+
moved. The connection is untouched, so none of the recovery above runs — a
|
|
478
|
+
`basic.cancel` frame is the only notice there is, and without `on_cancel` the
|
|
479
|
+
worker goes on publishing and heartbeating while consuming nothing.
|
|
480
|
+
|
|
481
|
+
Nothing is re-subscribed by default, which matches Bunny, the Java client and
|
|
482
|
+
the .NET client. `recover_cancelled_consumers: true`, on the session or on one
|
|
483
|
+
channel, re-subscribes under the same consumer tag, backing off from 1s to 30s
|
|
484
|
+
for as long as the queue exists and the channel is open.
|
|
485
|
+
|
|
486
|
+
**Recovery never declares anything.** One of the reasons a consumer gets
|
|
487
|
+
cancelled is somebody deleting the queue, and a client that answered that by
|
|
488
|
+
re-declaring would hand back a queue nobody could get rid of while a consumer
|
|
489
|
+
was attached. So the client passively checks whether the queue is still there
|
|
490
|
+
and re-consumes only if it is. If it has gone, the consumer stays gone and the
|
|
491
|
+
queue is dropped from the topology registry too — otherwise the next connection
|
|
492
|
+
recovery would re-declare it and the deletion would never stick. Only
|
|
493
|
+
`NOT_FOUND` counts as gone; a replicated queue mid-election is unavailable, not
|
|
494
|
+
deleted. Re-declaring is the application's call, and `on_cancel` is where to
|
|
495
|
+
make it: declare the queue there and the re-subscribe that follows will find
|
|
496
|
+
it.
|
|
497
|
+
|
|
498
|
+
```ruby
|
|
499
|
+
session = AsyncRabbitMQ::Session.new(..., recover_cancelled_consumers: true)
|
|
500
|
+
channel.on_cancel { |tag| logger.warn("consumer #{tag} cancelled by the broker") }
|
|
501
|
+
```
|
|
470
502
|
|
|
471
503
|
## Errors
|
|
472
504
|
|
|
@@ -520,6 +552,8 @@ logged and skipped; it never breaks the connection.
|
|
|
520
552
|
| `channel.rpc` | `channel`, `method` (`"queue.declare-ok"`), `duration` |
|
|
521
553
|
| `consumer.registered` | `channel`, `queue`, `consumer_tag`, `manual_ack` |
|
|
522
554
|
| `consumer.cancelled` | `channel`, `consumer_tag`, `queue`, `reason` (`:client`, `:broker`) |
|
|
555
|
+
| `consumer.recovered` | `channel`, `consumer_tag`, `queue`, `attempts` |
|
|
556
|
+
| `consumer.recovery_failed` | `channel`, `consumer_tag`, `queue`, `reason` (`:queue_deleted`, `:error`), `error` |
|
|
523
557
|
| `message.published` | `channel`, `exchange`, `routing_key`, `count`, `bytes`, `delivery_tag` |
|
|
524
558
|
| `message.confirmed` | `channel`, `delivery_tag`, `multiple`, `acked` |
|
|
525
559
|
| `message.returned` | `channel`, `exchange`, `routing_key`, `code`, `text`, `bytes` |
|
|
@@ -37,6 +37,8 @@ module AsyncRabbitMQ
|
|
|
37
37
|
# delivery the verbs are otherwise equivalent.
|
|
38
38
|
ERROR_DISPOSITIONS = %i[dead_letter retry release leave].freeze
|
|
39
39
|
|
|
40
|
+
DEFAULT_CONTENT_TYPE = "application/octet-stream".freeze
|
|
41
|
+
|
|
40
42
|
# A handler is application code and can raise anything. Catching only
|
|
41
43
|
# StandardError let a handler raising outside that hierarchy escape and take
|
|
42
44
|
# the channel's frame dispatch with it, so the catch below is deliberately
|
|
@@ -69,6 +71,12 @@ module AsyncRabbitMQ
|
|
|
69
71
|
# after an RPC timeout, used when the session has no rpc_timeout set.
|
|
70
72
|
RESYNC_TIMEOUT = 5
|
|
71
73
|
|
|
74
|
+
# First wait before re-subscribing a consumer the broker cancelled, and the
|
|
75
|
+
# ceiling the doubling stops at. Only used when the channel was asked to
|
|
76
|
+
# recover cancelled consumers; see #recovers_cancelled_consumers?.
|
|
77
|
+
RECOVER_CONSUMER_DELAY = 1
|
|
78
|
+
RECOVER_CONSUMER_MAX_DELAY = 30
|
|
79
|
+
|
|
72
80
|
# Pushed onto the work queue to retire one handler worker. Compared by
|
|
73
81
|
# identity, so it can never collide with a delivery.
|
|
74
82
|
WORKER_STOP = Object.new.freeze
|
|
@@ -92,7 +100,8 @@ module AsyncRabbitMQ
|
|
|
92
100
|
# concurrently on this channel (Bunny-parity: default 1 = serialized).
|
|
93
101
|
# Resizable at runtime via #pool_size=; basic_qos adjusts it automatically
|
|
94
102
|
# when prefetch_count > 0 so the two stay coupled.
|
|
95
|
-
def initialize(channel_id, session, frame_io, frame_max:, logger:, pool_size: 1, rpc_timeout: nil
|
|
103
|
+
def initialize(channel_id, session, frame_io, frame_max:, logger:, pool_size: 1, rpc_timeout: nil,
|
|
104
|
+
recover_cancelled_consumers: false)
|
|
96
105
|
@channel_id = channel_id
|
|
97
106
|
@session = session
|
|
98
107
|
@notifier = session.respond_to?(:notifier) ? session.notifier : nil
|
|
@@ -122,6 +131,7 @@ module AsyncRabbitMQ
|
|
|
122
131
|
@on_cancel = nil
|
|
123
132
|
@on_error = nil
|
|
124
133
|
@on_handler_error = nil
|
|
134
|
+
@recover_cancelled_consumers = recover_cancelled_consumers
|
|
125
135
|
@on_error_disposition = :dead_letter
|
|
126
136
|
@resyncing = false
|
|
127
137
|
@resync_condition = nil
|
|
@@ -645,10 +655,33 @@ module AsyncRabbitMQ
|
|
|
645
655
|
# Register a callback invoked when the broker cancels a consumer server-side
|
|
646
656
|
# (e.g. the queue is deleted or an HA failover occurs).
|
|
647
657
|
# The block receives the consumer_tag that was cancelled.
|
|
658
|
+
#
|
|
659
|
+
# The callback runs in a task of its own, so it may issue RPCs - a
|
|
660
|
+
# re-consume, a declare - and an exception out of it is logged rather than
|
|
661
|
+
# taken out on the channel.
|
|
648
662
|
def on_cancel(&block)
|
|
649
663
|
@on_cancel = block
|
|
650
664
|
end
|
|
651
665
|
|
|
666
|
+
# Re-subscribe a consumer the broker cancels, under the same consumer tag.
|
|
667
|
+
#
|
|
668
|
+
# Off by default, as it is in Bunny, and for a reason worth stating: the
|
|
669
|
+
# broker cancels a consumer when it can no longer serve the queue, and one
|
|
670
|
+
# of the ways that happens is somebody deleting the queue. A client that
|
|
671
|
+
# recovers by re-declaring would hand back a queue nobody can get rid of
|
|
672
|
+
# while a consumer is attached. So recovery here never declares anything.
|
|
673
|
+
# It passively checks that the queue is still there and re-consumes if it
|
|
674
|
+
# is; if the queue has gone it stops, and forgets the queue so that a later
|
|
675
|
+
# connection recovery does not re-declare it either.
|
|
676
|
+
#
|
|
677
|
+
# Retries back off from RECOVER_CONSUMER_DELAY to RECOVER_CONSUMER_MAX_DELAY
|
|
678
|
+
# and continue for as long as the queue exists and the channel is open.
|
|
679
|
+
attr_writer :recover_cancelled_consumers
|
|
680
|
+
|
|
681
|
+
def recovers_cancelled_consumers?
|
|
682
|
+
@recover_cancelled_consumers
|
|
683
|
+
end
|
|
684
|
+
|
|
652
685
|
# Register a callback invoked when the broker closes this channel due to an
|
|
653
686
|
# error (e.g. 404 queue not found). The block receives the channel and the
|
|
654
687
|
# AMQ::Protocol::Channel::Close method frame.
|
|
@@ -1271,18 +1304,7 @@ module AsyncRabbitMQ
|
|
|
1271
1304
|
handle_channel_close(method)
|
|
1272
1305
|
|
|
1273
1306
|
when AMQ::Protocol::Basic::Cancel
|
|
1274
|
-
|
|
1275
|
-
# Remove from @consumers so deliveries are no longer dispatched to a dead block.
|
|
1276
|
-
entry = @consumers.delete(method.consumer_tag)
|
|
1277
|
-
topology&.delete_consumer(method.consumer_tag)
|
|
1278
|
-
if entry
|
|
1279
|
-
@logger.warn("Channel #{@channel_id}: broker cancelled consumer #{method.consumer_tag}")
|
|
1280
|
-
instrument("consumer.cancelled") do
|
|
1281
|
-
{ channel: @channel_id, consumer_tag: method.consumer_tag, queue: entry[:queue_name], reason: :broker }
|
|
1282
|
-
end
|
|
1283
|
-
@on_cancel&.call(method.consumer_tag)
|
|
1284
|
-
end
|
|
1285
|
-
wake_each_waiters(method.consumer_tag)
|
|
1307
|
+
handle_broker_cancel(method.consumer_tag)
|
|
1286
1308
|
# No CancelOk to send for server-initiated cancel (no-wait is implicit).
|
|
1287
1309
|
|
|
1288
1310
|
when AMQ::Protocol::Channel::Flow
|
|
@@ -1352,7 +1374,6 @@ module AsyncRabbitMQ
|
|
|
1352
1374
|
forget_consumers_in_topology
|
|
1353
1375
|
@session.channel_closed(@channel_id)
|
|
1354
1376
|
instrument("channel.closed") { { channel: @channel_id, reason: :broker, code: code, text: text } }
|
|
1355
|
-
@on_error&.call(self, method)
|
|
1356
1377
|
|
|
1357
1378
|
error = if FrameIO::SOFT_ERROR_CODES.include?(code)
|
|
1358
1379
|
ChannelError.new(code: code, text: text, channel_id: @channel_id, close_method: method)
|
|
@@ -1367,6 +1388,157 @@ module AsyncRabbitMQ
|
|
|
1367
1388
|
@queue&.push(nil)
|
|
1368
1389
|
stop_workers(discard: true)
|
|
1369
1390
|
wake_each_waiters
|
|
1391
|
+
# Last, and in a task of its own. The lines above exist to release fibers
|
|
1392
|
+
# this close has stranded; a hook that is slow, or that raises, must not
|
|
1393
|
+
# come between them and the fibers waiting on them. The natural thing to
|
|
1394
|
+
# write in this hook is a reopen, which is an RPC, and an RPC on the
|
|
1395
|
+
# dispatch fiber waits for a reply only the dispatch fiber can deliver.
|
|
1396
|
+
notify_app("on_error") { @on_error.call(self, method) } if @on_error
|
|
1397
|
+
end
|
|
1398
|
+
|
|
1399
|
+
# An application callback never runs on the dispatch fiber. Two reasons,
|
|
1400
|
+
# both load-bearing. An RPC issued from a callback waits for a reply that
|
|
1401
|
+
# only this fiber delivers, so it deadlocks until rpc_timeout and then
|
|
1402
|
+
# drags the channel through a resync - and a re-consume is the obvious
|
|
1403
|
+
# thing to write in on_cancel. And an exception out of a callback escapes
|
|
1404
|
+
# #handle_message, whose rescue sits outside dispatch_loop's `loop`, so the
|
|
1405
|
+
# loop ends and the channel stops delivering anything at all: no
|
|
1406
|
+
# deliveries, no confirms, no RPC replies, with one line to say so.
|
|
1407
|
+
#
|
|
1408
|
+
# Bunny hands cancel notifications to its consumer work pool and the Java
|
|
1409
|
+
# client to its work service, for the same reason.
|
|
1410
|
+
def notify_app(name)
|
|
1411
|
+
@session.spawn_background do
|
|
1412
|
+
yield
|
|
1413
|
+
rescue *PROPAGATE_FROM_HANDLER
|
|
1414
|
+
raise
|
|
1415
|
+
rescue Exception => e # rubocop:disable Lint/RescueException
|
|
1416
|
+
@logger.error("Channel #{@channel_id}: #{name} callback raised #{e.class}: #{e.message}")
|
|
1417
|
+
end
|
|
1418
|
+
end
|
|
1419
|
+
|
|
1420
|
+
# The broker cancels a consumer when it can no longer serve the queue to
|
|
1421
|
+
# it: the queue was deleted, the node holding it failed, a replicated
|
|
1422
|
+
# queue's leader moved. The connection is untouched, so connection
|
|
1423
|
+
# recovery never runs - this frame is the only notice there is.
|
|
1424
|
+
#
|
|
1425
|
+
# The consumer goes from @consumers first, so no delivery is dispatched to
|
|
1426
|
+
# a block that is no longer subscribed, and everything beyond that
|
|
1427
|
+
# bookkeeping happens off this fiber.
|
|
1428
|
+
def handle_broker_cancel(tag)
|
|
1429
|
+
entry = @consumers.delete(tag)
|
|
1430
|
+
topology&.delete_consumer(tag)
|
|
1431
|
+
return wake_each_waiters(tag) unless entry
|
|
1432
|
+
|
|
1433
|
+
@logger.warn("Channel #{@channel_id}: broker cancelled consumer #{tag} on #{entry[:queue_name]}")
|
|
1434
|
+
instrument("consumer.cancelled") do
|
|
1435
|
+
{ channel: @channel_id, consumer_tag: tag, queue: entry[:queue_name], reason: :broker }
|
|
1436
|
+
end
|
|
1437
|
+
notify_app("on_cancel") { @on_cancel.call(tag) } if @on_cancel
|
|
1438
|
+
@session.spawn_background { reconcile_cancelled_consumer(tag, entry) }
|
|
1439
|
+
end
|
|
1440
|
+
|
|
1441
|
+
# Why the broker cancelled is not on the wire, and the two reasons want
|
|
1442
|
+
# opposite handling: a queue that moved or was briefly unavailable should
|
|
1443
|
+
# be consumed from again, while a queue somebody deleted must be left
|
|
1444
|
+
# deleted. A passive declare is the only way to tell them apart. Spring
|
|
1445
|
+
# AMQP's listener container probes the same way, and for the same reason
|
|
1446
|
+
# declares nothing.
|
|
1447
|
+
#
|
|
1448
|
+
# Runs whether or not this channel recovers cancelled consumers, because
|
|
1449
|
+
# forgetting a deleted queue is not a recovery feature: a queue left in the
|
|
1450
|
+
# topology registry is re-declared on the next connection recovery, and
|
|
1451
|
+
# then the deletion never sticks.
|
|
1452
|
+
def reconcile_cancelled_consumer(tag, entry)
|
|
1453
|
+
queue = entry[:queue_name]
|
|
1454
|
+
return forget_deleted_queue(tag, queue) if queue_deleted?(queue)
|
|
1455
|
+
return wake_each_waiters(tag) unless recovers_cancelled_consumers?
|
|
1456
|
+
|
|
1457
|
+
recover_cancelled_consumer(tag, entry)
|
|
1458
|
+
end
|
|
1459
|
+
|
|
1460
|
+
# Re-consume under the same tag, for as long as the queue is there and this
|
|
1461
|
+
# channel can carry a consumer. Nothing is declared and nothing is bound:
|
|
1462
|
+
# whatever the queue is, it is the one that was there before.
|
|
1463
|
+
def recover_cancelled_consumer(tag, entry)
|
|
1464
|
+
queue = entry[:queue_name]
|
|
1465
|
+
delay = RECOVER_CONSUMER_DELAY
|
|
1466
|
+
failures = 0
|
|
1467
|
+
|
|
1468
|
+
loop do
|
|
1469
|
+
# The channel is gone, or the connection is: either way the consumers
|
|
1470
|
+
# on it are somebody else's problem now (#reopen, or recovery).
|
|
1471
|
+
return wake_each_waiters(tag) unless open? && @session.open?
|
|
1472
|
+
|
|
1473
|
+
begin
|
|
1474
|
+
basic_consume(queue, consumer_tag: tag, manual_ack: entry[:manual_ack], exclusive: entry[:exclusive],
|
|
1475
|
+
arguments: entry[:arguments] || {}, on_error: entry[:on_error], &entry[:block])
|
|
1476
|
+
@logger.info("Channel #{@channel_id}: re-subscribed consumer #{tag} to #{queue}")
|
|
1477
|
+
instrument("consumer.recovered") do
|
|
1478
|
+
{ channel: @channel_id, consumer_tag: tag, queue: queue, attempts: failures + 1 }
|
|
1479
|
+
end
|
|
1480
|
+
return
|
|
1481
|
+
rescue *PROPAGATE_FROM_HANDLER
|
|
1482
|
+
raise
|
|
1483
|
+
rescue => e
|
|
1484
|
+
failures += 1
|
|
1485
|
+
# One line per episode at warn, the rest at debug: a broker that is
|
|
1486
|
+
# away for an hour would otherwise write a warning every attempt for
|
|
1487
|
+
# the whole hour, and an application counting warnings would read
|
|
1488
|
+
# one outage as hundreds.
|
|
1489
|
+
if failures == 1
|
|
1490
|
+
@logger.warn("Channel #{@channel_id}: could not re-subscribe #{tag} to #{queue}, " \
|
|
1491
|
+
"retrying: #{e.class}: #{e.message}")
|
|
1492
|
+
instrument("consumer.recovery_failed") do
|
|
1493
|
+
{ channel: @channel_id, consumer_tag: tag, queue: queue, reason: :error, error: e }
|
|
1494
|
+
end
|
|
1495
|
+
else
|
|
1496
|
+
@logger.debug("Channel #{@channel_id}: re-subscribe attempt #{failures} for #{tag} failed: #{e.message}")
|
|
1497
|
+
end
|
|
1498
|
+
end
|
|
1499
|
+
|
|
1500
|
+
sleep delay
|
|
1501
|
+
delay = [delay * 2, RECOVER_CONSUMER_MAX_DELAY].min
|
|
1502
|
+
# Re-checked every round, not just once: a queue deleted while we were
|
|
1503
|
+
# retrying has to stop the loop, and it is also what keeps an endless
|
|
1504
|
+
# retry endless only while there is something to consume from.
|
|
1505
|
+
return forget_deleted_queue(tag, queue) if queue_deleted?(queue)
|
|
1506
|
+
end
|
|
1507
|
+
end
|
|
1508
|
+
|
|
1509
|
+
# The queue behind a cancelled consumer has gone. Drop it from the topology
|
|
1510
|
+
# registry, with its bindings, so a later connection recovery does not
|
|
1511
|
+
# re-declare a queue somebody deleted on purpose - the "deleted queue comes
|
|
1512
|
+
# back" bug the Java client has been fixing instances of for years.
|
|
1513
|
+
def forget_deleted_queue(tag, queue)
|
|
1514
|
+
topology&.delete_queue(queue)
|
|
1515
|
+
@logger.warn("Channel #{@channel_id}: queue #{queue} no longer exists; consumer #{tag} will not be recovered")
|
|
1516
|
+
instrument("consumer.recovery_failed") do
|
|
1517
|
+
{ channel: @channel_id, consumer_tag: tag, queue: queue, reason: :queue_deleted }
|
|
1518
|
+
end
|
|
1519
|
+
wake_each_waiters(tag)
|
|
1520
|
+
end
|
|
1521
|
+
|
|
1522
|
+
# Deleted, as opposed to merely unreachable. A passive declare, on a channel
|
|
1523
|
+
# of its own because a passive declare that fails closes the channel it ran
|
|
1524
|
+
# on - which would take the queue's fellow consumers down with it.
|
|
1525
|
+
#
|
|
1526
|
+
# Only NOT_FOUND counts. A replicated queue mid-election is the very case
|
|
1527
|
+
# this whole path exists for and it does not answer 404, so treating every
|
|
1528
|
+
# channel error as a deletion would forget a queue that is coming back and
|
|
1529
|
+
# stop topology recovery ever re-declaring it. Anything inconclusive - a
|
|
1530
|
+
# connection that went while we were asking, too - reads as "still there",
|
|
1531
|
+
# which costs a retry and never costs a queue.
|
|
1532
|
+
def queue_deleted?(name)
|
|
1533
|
+
@session.with_channel { |probe| probe.queue(name, passive: true) }
|
|
1534
|
+
false
|
|
1535
|
+
rescue ChannelError => e
|
|
1536
|
+
e.code == 404
|
|
1537
|
+
rescue *PROPAGATE_FROM_HANDLER
|
|
1538
|
+
raise
|
|
1539
|
+
rescue => e
|
|
1540
|
+
@logger.debug("Channel #{@channel_id}: could not check whether #{name} still exists: #{e.class}: #{e.message}")
|
|
1541
|
+
false
|
|
1370
1542
|
end
|
|
1371
1543
|
|
|
1372
1544
|
# Release fibers blocked in #each for one consumer, or for all of them.
|
|
@@ -1493,16 +1665,18 @@ module AsyncRabbitMQ
|
|
|
1493
1665
|
|
|
1494
1666
|
# Encode one basic.publish (method + header + body frames) into a single
|
|
1495
1667
|
# byte string, so a message always hits the write queue in one piece.
|
|
1496
|
-
def encode_publish(payload, exchange:, routing_key:, mandatory: false, persistent:
|
|
1497
|
-
content_type:
|
|
1668
|
+
def encode_publish(payload, exchange:, routing_key:, mandatory: false, persistent: true,
|
|
1669
|
+
content_type: DEFAULT_CONTENT_TYPE, content_encoding: nil, headers: nil, priority: 0,
|
|
1498
1670
|
correlation_id: nil, reply_to: nil, expiration: nil, message_id: nil,
|
|
1499
1671
|
timestamp: nil, type: nil, user_id: nil, app_id: nil, properties: {})
|
|
1500
1672
|
payload_bytes = payload.is_a?(String) ? payload.b : payload
|
|
1501
|
-
props = {
|
|
1502
|
-
|
|
1673
|
+
props = {
|
|
1674
|
+
delivery_mode: persistent ? 2 : 1,
|
|
1675
|
+
content_type: content_type || DEFAULT_CONTENT_TYPE,
|
|
1676
|
+
priority: priority || 0
|
|
1677
|
+
}
|
|
1503
1678
|
props[:content_encoding] = content_encoding if content_encoding
|
|
1504
1679
|
props[:headers] = headers if headers
|
|
1505
|
-
props[:priority] = priority if priority
|
|
1506
1680
|
props[:correlation_id] = correlation_id if correlation_id
|
|
1507
1681
|
props[:reply_to] = reply_to if reply_to
|
|
1508
1682
|
props[:expiration] = expiration if expiration
|
|
@@ -161,6 +161,7 @@ module AsyncRabbitMQ
|
|
|
161
161
|
recovery_interval: RECOVERY_INITIAL,
|
|
162
162
|
recovery_max_interval: RECOVERY_MAX,
|
|
163
163
|
recover_topology: true,
|
|
164
|
+
recover_cancelled_consumers: false,
|
|
164
165
|
topology_recovery_filter: nil,
|
|
165
166
|
instrumenter: nil,
|
|
166
167
|
notifier: nil,
|
|
@@ -194,6 +195,9 @@ module AsyncRabbitMQ
|
|
|
194
195
|
@recovery_interval = recovery_interval
|
|
195
196
|
@recovery_max_interval = recovery_max_interval
|
|
196
197
|
@recover_topology = recover_topology
|
|
198
|
+
# The default every channel opened on this session takes; see
|
|
199
|
+
# Channel#recovers_cancelled_consumers? for why it is off.
|
|
200
|
+
@recover_cancelled_consumers = recover_cancelled_consumers
|
|
197
201
|
@topology = TopologyRegistry.new(filter: topology_recovery_filter)
|
|
198
202
|
@logger = logger
|
|
199
203
|
@notifier = notifier || Notifier.new(logger: logger)
|
|
@@ -354,12 +358,15 @@ module AsyncRabbitMQ
|
|
|
354
358
|
# Open a new channel. Returns an AsyncRabbitMQ::Channel.
|
|
355
359
|
# +pool_size+ bounds concurrent consumer-handler fibers on the channel
|
|
356
360
|
# (Bunny-parity: default 1). basic_qos will auto-adjust it to prefetch_count.
|
|
357
|
-
|
|
361
|
+
# +recover_cancelled_consumers+ defaults to the session's setting; see
|
|
362
|
+
# Channel#recovers_cancelled_consumers?.
|
|
363
|
+
def open_channel(pool_size: 1, recover_cancelled_consumers: @recover_cancelled_consumers)
|
|
358
364
|
raise NotOpenError, "Session is not open" unless open?
|
|
359
365
|
channel = @channel_mutex.acquire do
|
|
360
366
|
channel_id = next_channel_id
|
|
361
367
|
Channel.new(channel_id, self, @frame_io, frame_max: @negotiated_fm || @frame_max, logger: @logger,
|
|
362
|
-
pool_size: pool_size, rpc_timeout: @rpc_timeout
|
|
368
|
+
pool_size: pool_size, rpc_timeout: @rpc_timeout,
|
|
369
|
+
recover_cancelled_consumers: recover_cancelled_consumers).tap { |ch| @channels[channel_id] = ch }
|
|
363
370
|
end
|
|
364
371
|
channel.open
|
|
365
372
|
channel
|
data/lib/async_rabbitmq.rb
CHANGED