bug_bunny 4.13.0 → 4.16.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d1bcda002a00d8ebc93786a496f8649d43e82d58645aa2b77e3d9b72044e7e07
4
- data.tar.gz: b49f4c724ae39592d6c1a1774873fc8b272cf853688d998cbfb2f22d721d3589
3
+ metadata.gz: 67e58763b6f088c44e17121922ecb618c9ccef8738096d534c578eea021c5f94
4
+ data.tar.gz: 47fe7485dd056e9ffb8a7e5268b97e4d0173e0441984506b5528f5c43643da7a
5
5
  SHA512:
6
- metadata.gz: 3b16b769d198b82f5455294bfca2c9c157f6dc7cb34a4cceedf9441d11677035a51c97b1d29e7cb59994d7d954072062919c24958ac340d9da018dded6c9e199
7
- data.tar.gz: fdea4a6b6d49dd032787c520e5516045812d94afa5ba4dc8a6ff818e7e89bc6263f92877266e82a40a3789907a2aafd9727ecd2770949511b3e8584915cc87ab
6
+ metadata.gz: f0a5927c4a4a450b414ee38ffbc25a0a97a80b2678d32a19d524a51367572f7b5e2935e29a81bf9afbdd2cfb0b634e9b60a322b828ff14ba0f79e60982148723
7
+ data.tar.gz: e66726447226ceb96f2d3b81dd759209fd8e54e7ea67bb85870cc805e44747e81f8121a821fd7a6fa65435ae3ba95e3cd62631ef0687fa0543f29848e21f4682
data/CHANGELOG.md CHANGED
@@ -1,5 +1,52 @@
1
1
  # Changelog
2
2
 
3
+ ## [4.16.0] - 2026-05-13
4
+
5
+ ### Cambios de comportamiento (semi-breaking)
6
+ - **`DEFAULT_QUEUE_OPTIONS` cambió a `{ exclusive: false, durable: true, auto_delete: false }`** (#42). En versiones previas el default era `{ exclusive: false, durable: false, auto_delete: true }` — la combinación `transient_nonexcl_queues` que **RabbitMQ 4.x deprecó por default**: el broker rechaza la declaración matando la conexión. El nuevo default es el patrón "queue compartida duradera": sobrevive restart del broker, múltiples consumers pueden compartirla, no se elimina cuando se desconecta el último consumer. Esto matchea cómo la mayoría de servicios Wispro ya configuran sus queues explícitamente.
7
+
8
+ **Para restaurar el comportamiento anterior** (servicios sobre RabbitMQ 3.x con queues legacy efímeras pre-existentes):
9
+
10
+ ```ruby
11
+ BugBunny.configure do |c|
12
+ c.queue_options = { exclusive: false, durable: false, auto_delete: true }
13
+ end
14
+ ```
15
+
16
+ **Síntoma si se necesita override y no se aplicó:** `Bunny::PreconditionFailed - inequivalent arg 'durable' for queue 'foo'`. Indica que la queue existe en el broker con `durable: false` pero el nuevo default intenta declararla con `durable: true`. Aplicar el override de arriba o borrar manualmente la queue legacy en el broker.
17
+
18
+ ### Documentación
19
+ - README + SKILL.md actualizados con sección "queue_options recomendadas" cubriendo patrones worker-pool (default nuevo) y single-instance (`exclusive: true`).
20
+
21
+ ## [4.15.0] - 2026-05-13
22
+
23
+ ### Nuevas funcionalidades
24
+ - **`return_raise` flag para mandatory + basic.return (#38):** `Producer#confirmed` ahora levanta `BugBunny::PublishUnroutable` cuando el broker retorna un mensaje publicado con `mandatory: true` que no pudo rutearse a ninguna cola. Espejo simétrico de `nack_raise`/`PublishNacked`. La excepción expone `path`, `exchange`, `routing_key`, `reply_code`, `reply_text` y `correlation_id`. Internamente la gema implementa el bridge cross-thread (reader thread → publish thread) que antes cada caller tenía que replicar manualmente con `Concurrent::Map` + lambda. Configurable globalmente vía `BugBunny.configuration.return_raise` (default `true`) y por request via `client.publish(..., return_raise: false)`. El callback global `on_return` se sigue invocando antes del raise. — @Gabriel
25
+
26
+ ### Cambios de comportamiento (semi-breaking)
27
+ - **Default `return_raise: true`:** Publicaciones con `confirmed: true, mandatory: true` que reciben `basic.return` del broker ahora levantan excepción por default. En 4.14.0 el return solo se logueaba (o invocaba el callback `on_return`) y la llamada retornaba 202 silenciosamente — ocultando pérdida de mensajes. Para mantener el comportamiento previo: `BugBunny.configuration.return_raise = false` o `return_raise: false` per request. El flag es **inerte cuando `mandatory: false`** — sin mandatory el broker nunca emite return.
28
+
29
+ ### Detalles internos
30
+ - `Producer#confirmed` auto-asigna `correlation_id` (UUID) cuando falta y `mandatory + return_raise` están activos — la correlación bridge↔return depende del cid.
31
+ - Nuevo bound de espera `Producer::RETURN_RACE_WINDOW_S = 0.05` tras un ack positivo: tolera el race scheduling entre reader thread (donde Bunny invoca `on_return`) y publish thread (donde se devuelve `wait_for_confirms`). AMQP garantiza orden wire (return precede a ack), pero defendemos contra GVL.
32
+ - `Session` ahora mantiene un registry interno `@pending_returns` (`Concurrent::Map` de cid → `{event, info}`). `handle_broker_return` setea el event *antes* de invocar el user_cb global — una excepción del callback no impide el raise en el caller.
33
+ - Nuevo evento de log `producer.publish_unroutable` (WARN) con `path`, `exchange`, `routing_key`, `reply_code`, `reply_text`, `messaging_message_id`. Se emite antes de levantar `PublishUnroutable`.
34
+ - Nuevo evento de log `client.return_raise_ignored` (WARN) cuando se pasa `return_raise: true` sin `confirmed: true` o sin `mandatory: true` — el flag se ignora.
35
+
36
+ ## [4.14.0] - 2026-05-12
37
+
38
+ ### Nuevas funcionalidades
39
+ - **Duraciones medidas internamente en el Producer:** BugBunny ahora emite `duration_s` automáticamente en los eventos del publisher siguiendo las [OpenTelemetry metric semantic conventions](https://opentelemetry.io/docs/specs/semconv/general/metrics/) (`Float` en segundos). El código de aplicación ya no necesita envolver `client.publish` con `Process.clock_gettime`. — @Gabriel
40
+ - `producer.published` (INFO): `duration_s` del `basic_publish` (TCP enqueue al broker, sin esperar ACK).
41
+ - `producer.confirmed` (INFO): tres duraciones desglosadas — `publish_duration_s`, `confirm_duration_s` (espera de `wait_for_confirms`) y `duration_s` total. Útil para distinguir latencia de red vs latencia del confirm policy del broker.
42
+ - `producer.rpc_response_received`: ahora incluye `duration_s` con el round-trip RPC completo (publish + procesamiento remoto + reply).
43
+
44
+ ### Cambios de comportamiento
45
+ - **`producer.rpc_response_received` promovido de DEBUG a INFO.** No es breaking de API pero aumenta el volumen de logs en clientes RPC. Si el cambio impacta tu pipeline de observabilidad, filtralo por nivel.
46
+
47
+ ### Documentación
48
+ - README + `skill/SKILL.md` + `skill/references/client-middleware.md` actualizados con el catálogo completo de eventos de log emitidos por la gema y la tabla de qué mide cada `duration_s`. Mensaje explícito en ambas audiencias (humana + agente) advirtiendo no duplicar la medición en código de aplicación.
49
+
3
50
  ## [4.13.0] - 2026-05-11
4
51
 
5
52
  ### Nuevas funcionalidades
data/README.md CHANGED
@@ -129,7 +129,11 @@ BugBunny.configure do |config|
129
129
  config.read_timeout = 10
130
130
  config.write_timeout = 10
131
131
 
132
- # AMQP defaults applied to all exchanges and queues
132
+ # AMQP defaults applied to all exchanges and queues.
133
+ # Gem defaults (since 4.16):
134
+ # DEFAULT_EXCHANGE_OPTIONS = { durable: false, auto_delete: false }
135
+ # DEFAULT_QUEUE_OPTIONS = { exclusive: false, durable: true, auto_delete: false }
136
+ # Override only if your service needs different infrastructure semantics.
133
137
  config.exchange_options = { durable: true }
134
138
  config.queue_options = { durable: true }
135
139
 
@@ -142,7 +146,13 @@ BugBunny.configure do |config|
142
146
  # Health check file for Kubernetes / Docker Swarm liveness probes
143
147
  config.health_check_file = '/tmp/bug_bunny_health'
144
148
 
149
+ # Publisher Confirms — fail-loud defaults (both flags default to true).
150
+ # Set to false to restore legacy log-only behavior.
151
+ config.nack_raise = true # broker NACK → raise BugBunny::PublishNacked
152
+ config.return_raise = true # broker basic.return (mandatory) → raise BugBunny::PublishUnroutable
153
+
145
154
  # Callback invoked when the broker returns an unroutable mandatory message.
155
+ # Runs BEFORE PublishUnroutable is raised (if return_raise is true).
146
156
  # When nil (default), BugBunny logs the return as `session.broker_return` at :warn.
147
157
  # Signature: ->(return_info, properties, body) { ... }
148
158
  config.on_return = ->(return_info, _props, body) {
@@ -220,11 +230,26 @@ client.publish('acct.start',
220
230
  | `confirmed` | Boolean | `false` | Block until `wait_for_confirms` returns. |
221
231
  | `mandatory` | Boolean | `false` | Broker returns the message if it cannot be routed to any queue. Requires `confirmed: true` to be useful. |
222
232
  | `confirm_timeout` | Float | `nil` | Seconds to wait for the broker ACK. Raises `BugBunny::RequestTimeout` if exceeded. |
223
- | `nack_raise` | Boolean | `nil` | Per-request override of `config.nack_raise`. When `nil`, falls back to the global flag. |
233
+ | `nack_raise` | Boolean | `nil` | Per-request override of `config.nack_raise`. When `nil`, falls back to the global flag (default `true`). |
234
+ | `return_raise` | Boolean | `nil` | Per-request override of `config.return_raise`. When `nil`, falls back to the global flag (default `true`). Requires `confirmed: true` and `mandatory: true` to take effect. |
235
+
236
+ **Two broker signals, two exceptions:**
224
237
 
225
- If the broker NACKs the publish (explicit rejection disk full, internal confirm policy, etc.), the call raises `BugBunny::PublishNacked` by default. The exception exposes `path` and `nacked_count`. Critical publishers (audit, billing, RADIUS accounting) can let it bubble up to translate into HTTP 5xx so upstream systems retry. To restore the legacy "log-only" behaviour, set `BugBunny.configuration.nack_raise = false` globally or pass `nack_raise: false` per request.
238
+ | Broker signal | Default behavior | Exception class | Fields |
239
+ |---|---|---|---|
240
+ | `basic.nack` (explicit rejection) | Raises | `BugBunny::PublishNacked` | `path`, `nacked_count` |
241
+ | `basic.return` (unroutable + `mandatory: true`) | Raises | `BugBunny::PublishUnroutable` | `path`, `exchange`, `routing_key`, `reply_code`, `reply_text`, `correlation_id` |
226
242
 
227
- Unroutable returned messages are handled by a single global callback (see `config.on_return` below). The default handler logs them as `session.broker_return` at `warn` level nothing is dropped silently.
243
+ Both exceptions translate naturally into HTTP 5xx in critical publishers (audit, billing, RADIUS accounting) so upstream systems retry. The `config.on_return` callback (if defined) still runs before `PublishUnroutable` is raiseduseful for alerting/metrics. To restore the legacy "log-only" behaviour:
244
+
245
+ ```ruby
246
+ BugBunny.configure do |c|
247
+ c.nack_raise = false # or pass `nack_raise: false` per request
248
+ c.return_raise = false # or pass `return_raise: false` per request
249
+ end
250
+ ```
251
+
252
+ When `mandatory: false` (the default), `return_raise` is inert — the broker never emits `basic.return` without mandatory.
228
253
 
229
254
  ---
230
255
 
@@ -252,11 +277,27 @@ BugBunny implementa de forma nativa las [OpenTelemetry semantic conventions for
252
277
  Todos los eventos internos se emiten como logs `key=value` compatibles con Datadog, CloudWatch, ELK y ExisRay.
253
278
 
254
279
  ```
280
+ component=bug_bunny event=producer.publish method=POST path=acct/publish messaging_destination_name=acct_x messaging_routing_key=acct.start.42
281
+ component=bug_bunny event=producer.published method=POST path=acct/publish routing_key=acct.start.42 messaging_message_id=corr-1 duration_s=0.000812
282
+ component=bug_bunny event=producer.confirmed method=POST path=acct/publish routing_key=acct.start.42 publish_duration_s=0.000812 confirm_duration_s=0.012 duration_s=0.013
283
+ component=bug_bunny event=producer.rpc_response_received method=GET path=users/42 duration_s=0.034 messaging_operation=receive
255
284
  component=bug_bunny event=consumer.message_processed status=200 duration_s=0.012 messaging_operation=process controller=NodesController action=show
256
285
  component=bug_bunny event=consumer.execution_error error_class=RuntimeError error_message="..." duration_s=0.003
257
286
  component=bug_bunny event=consumer.connection_error attempt_count=2 retry_in_s=10 error_message="..."
258
287
  ```
259
288
 
289
+ ### Duraciones medidas internamente
290
+
291
+ BugBunny mide y emite duraciones automáticamente — **no es necesario envolver llamadas a `client.publish` con `Process.clock_gettime` en el código de aplicación**. Las unidades siguen las [OpenTelemetry metric semantic conventions](https://opentelemetry.io/docs/specs/semconv/general/metrics/) (`s`, segundos como `Float`).
292
+
293
+ | Evento | Duración | Mide |
294
+ |---|---|---|
295
+ | `producer.published` | `duration_s` | Solo el `basic_publish` (TCP enqueue al broker). |
296
+ | `producer.confirmed` | `publish_duration_s` + `confirm_duration_s` + `duration_s` (total) | Publish + espera de ACK del broker. |
297
+ | `producer.rpc_response_received` | `duration_s` | Round-trip RPC completo (publish + procesamiento remoto + reply). |
298
+ | `consumer.message_processed` | `duration_s` | Procesamiento del mensaje (router + controller + reply). |
299
+ | `consumer.execution_error` | `duration_s` | Tiempo transcurrido hasta el error. |
300
+
260
301
  Las claves sensibles (`password`, `token`, `secret`, `api_key`, `authorization`, etc.) se filtran automáticamente a `[FILTERED]` en toda la salida de logs.
261
302
 
262
303
  ---
@@ -269,9 +310,15 @@ BugBunny maps RabbitMQ responses to a semantic exception hierarchy, similar to h
269
310
 
270
311
  ```
271
312
  BugBunny::Error
313
+ ├── CommunicationError (network / channel failure)
314
+ ├── ConfigurationError (invalid config attribute)
315
+ ├── SecurityError (unauthorized controller resolution)
316
+ ├── PublishNacked (broker basic.nack on :confirmed publish)
317
+ ├── PublishUnroutable (broker basic.return on mandatory + :confirmed)
272
318
  ├── ClientError (4xx)
273
319
  │ ├── BadRequest (400)
274
320
  │ ├── NotFound (404)
321
+ │ │ └── RoutingError (consumer-side: no route for verb + path)
275
322
  │ ├── NotAcceptable (406)
276
323
  │ ├── RequestTimeout (408)
277
324
  │ ├── Conflict (409)
@@ -95,10 +95,16 @@ module BugBunny
95
95
  # @option args [Float] :confirm_timeout Segundos a esperar el confirm. `nil` espera indefinidamente.
96
96
  # @option args [Boolean] :nack_raise Override per-request del flag
97
97
  # `BugBunny.configuration.nack_raise`. Si `nil` (default), se usa la configuración global.
98
+ # @option args [Boolean] :return_raise Override per-request del flag
99
+ # `BugBunny.configuration.return_raise`. Si `nil` (default), se usa la configuración global.
100
+ # Requiere `mandatory: true` y `confirmed: true` para tener efecto — sino se emite un
101
+ # warning y el flag se ignora.
98
102
  # @yield [req] Bloque para configurar el objeto Request.
99
103
  # @return [Hash] `{ 'status' => 202, 'body' => nil }`.
100
104
  # @raise [BugBunny::RequestTimeout] Si `confirmed: true` y el broker no confirma a tiempo.
101
105
  # @raise [BugBunny::PublishNacked] Si `confirmed: true`, el broker NACKea, y `nack_raise` resuelto es true.
106
+ # @raise [BugBunny::PublishUnroutable] Si `confirmed: true`, `mandatory: true`, el broker retorna el
107
+ # mensaje como no-ruteable, y `return_raise` resuelto es true.
102
108
  def publish(url, **args)
103
109
  send(url, **args) do |req|
104
110
  req.delivery_mode = args[:confirmed] ? :confirmed : :publish
@@ -120,6 +126,10 @@ module BugBunny
120
126
  # Configuración del usuario (bloque específico por request)
121
127
  yield req if block_given?
122
128
 
129
+ # Check post-block: el block API puede setear delivery_mode/mandatory después
130
+ # de los keyword args. Evaluamos el warning sobre el estado final del Request.
131
+ warn_return_raise_misuse(req)
132
+
123
133
  # Ejecución dentro del Pool.
124
134
  # Session y Producer se reutilizan por slot de conexión (ver #session_for / #producer_for).
125
135
  @pool.with do |conn|
@@ -179,6 +189,33 @@ module BugBunny
179
189
  req.mandatory = args[:mandatory] if args.key?(:mandatory)
180
190
  req.confirm_timeout = args[:confirm_timeout] if args.key?(:confirm_timeout)
181
191
  req.nack_raise = args[:nack_raise] if args.key?(:nack_raise)
192
+ req.return_raise = args[:return_raise] if args.key?(:return_raise)
193
+ end
194
+
195
+ # Emite un warning si el Request final tiene `return_raise: true` pero le falta
196
+ # `delivery_mode == :confirmed` o `mandatory: true`. El flag requiere ambos para
197
+ # tener efecto: sin confirmed no hay synchronization point sobre el cual levantar,
198
+ # y sin mandatory el broker nunca retorna.
199
+ #
200
+ # Se evalúa sobre el Request post-block (no sobre args) para no producir falsos
201
+ # positivos cuando el caller usa el block API para setear `req.delivery_mode` o
202
+ # `req.mandatory` después de los keyword args.
203
+ #
204
+ # Solo warnea cuando `request.return_raise` fue explícitamente `true` por request —
205
+ # ignora el default global (que también puede ser `true`) para no inundar logs en
206
+ # publishes regulares sin mandatory.
207
+ #
208
+ # @param request [BugBunny::Request]
209
+ # @return [void]
210
+ def warn_return_raise_misuse(request)
211
+ return unless request.return_raise == true
212
+ return if request.delivery_mode == :confirmed && request.mandatory
213
+
214
+ BugBunny.configuration.logger&.warn do
215
+ 'component=bug_bunny event=client.return_raise_ignored ' \
216
+ 'reason=requires_confirmed_and_mandatory ' \
217
+ "delivery_mode=#{request.delivery_mode} mandatory=#{!!request.mandatory}"
218
+ end
182
219
  end
183
220
 
184
221
  # Recupera o crea la Session asociada al slot de conexión dado.
@@ -162,6 +162,19 @@ module BugBunny
162
162
  # `Client#publish`.
163
163
  attr_accessor :nack_raise
164
164
 
165
+ # @return [Boolean] Si `true` (default), {BugBunny::Producer#confirmed} levanta
166
+ # {BugBunny::PublishUnroutable} cuando el broker retorna un mensaje publicado
167
+ # con `mandatory: true` que no pudo rutearse. Si `false`, el return solo se
168
+ # procesa via {#on_return} callback (modo legacy) y la llamada retorna
169
+ # `{ 'status' => 202 }`.
170
+ #
171
+ # El flag es inerte cuando `mandatory: false` (sin mandatory, el broker nunca
172
+ # retorna). El callback {#on_return} se sigue invocando antes del raise.
173
+ #
174
+ # El valor puede sobreescribirse por request pasando `return_raise:` en
175
+ # `Client#publish`.
176
+ attr_accessor :return_raise
177
+
165
178
  # @!endgroup
166
179
 
167
180
  # Inicializa la configuración con valores por defecto seguros.
@@ -239,6 +252,7 @@ module BugBunny
239
252
  @on_rpc_reply = nil
240
253
  @on_return = nil
241
254
  @nack_raise = true
255
+ @return_raise = true
242
256
  end
243
257
 
244
258
  def validate_required!(attr, value, rules)
@@ -50,6 +50,67 @@ module BugBunny
50
50
  end
51
51
  end
52
52
 
53
+ # Error lanzado cuando el broker retorna un mensaje publicado con `mandatory: true`
54
+ # que no pudo rutearse a ninguna cola en modo `:confirmed`.
55
+ #
56
+ # Un return implica que el publish llegó al broker pero ninguna binding aceptó la
57
+ # routing key — el mensaje se considera no entregado desde la perspectiva del
58
+ # publisher. Espejo simétrico de {PublishNacked} pero para la señal `basic.return`
59
+ # en lugar de `basic.nack`.
60
+ #
61
+ # Se levanta por default desde {BugBunny::Producer#confirmed} cuando el request
62
+ # tiene `mandatory: true`. Para opt-out, configurar
63
+ # `BugBunny.configuration.return_raise = false` o pasar `return_raise: false`
64
+ # por request. El callback `BugBunny.configuration.on_return` se sigue invocando
65
+ # antes del raise (orden: registro interno → user_cb → raise en el caller).
66
+ #
67
+ # @example
68
+ # rescue BugBunny::PublishUnroutable => e
69
+ # e.path # => 'acct.start'
70
+ # e.exchange # => 'acct_x'
71
+ # e.routing_key # => 'acct.unbound'
72
+ # e.reply_code # => 312
73
+ # e.reply_text # => 'NO_ROUTE'
74
+ # e.correlation_id # => 'corr-uuid-...'
75
+ class PublishUnroutable < Error
76
+ # @return [String] Ruta lógica del request cuyo publish fue retornado.
77
+ attr_reader :path
78
+
79
+ # @return [String] Nombre del exchange destino.
80
+ attr_reader :exchange
81
+
82
+ # @return [String] Routing key utilizada en el publish.
83
+ attr_reader :routing_key
84
+
85
+ # @return [Integer, nil] Código AMQP de la razón (ej: 312 = NO_ROUTE).
86
+ attr_reader :reply_code
87
+
88
+ # @return [String, nil] Texto humano-legible que describe la razón.
89
+ attr_reader :reply_text
90
+
91
+ # @return [String, nil] Correlation ID del request retornado.
92
+ attr_reader :correlation_id
93
+
94
+ # @param path [String] Ruta lógica del request (ej: 'acct.start').
95
+ # @param exchange [String] Nombre del exchange destino.
96
+ # @param routing_key [String] Routing key del publish.
97
+ # @param reply_code [Integer, nil] Código AMQP del return.
98
+ # @param reply_text [String, nil] Texto del return.
99
+ # @param correlation_id [String, nil] Correlation ID del mensaje retornado.
100
+ # rubocop:disable Metrics/ParameterLists
101
+ def initialize(path:, exchange:, routing_key:, reply_code: nil, reply_text: nil, correlation_id: nil)
102
+ @path = path
103
+ @exchange = exchange
104
+ @routing_key = routing_key
105
+ @reply_code = reply_code
106
+ @reply_text = reply_text
107
+ @correlation_id = correlation_id
108
+ super("broker returned unroutable message on path=#{path} exchange=#{exchange} " \
109
+ "routing_key=#{routing_key} reply_code=#{reply_code} reply_text=#{reply_text}")
110
+ end
111
+ # rubocop:enable Metrics/ParameterLists
112
+ end
113
+
53
114
  # ==========================================
54
115
  # Categoría: Errores del Cliente (4xx)
55
116
  # ==========================================
@@ -15,6 +15,13 @@ module BugBunny
15
15
  class Producer
16
16
  include BugBunny::Observability
17
17
 
18
+ # Bound de espera (segundos) que el publish thread aplica tras un ack positivo para
19
+ # tolerar el scheduling race entre reader thread (donde Bunny invoca `on_return`) y
20
+ # publish thread (donde se devuelve `wait_for_confirms`). AMQP garantiza que el
21
+ # `basic.return` precede al `basic.ack` en la wire, así que en la práctica el event
22
+ # ya está seteado al llegar acá; este wait es defensa contra GVL.
23
+ RETURN_RACE_WINDOW_S = 0.05
24
+
18
25
  # Inicializa el productor.
19
26
  #
20
27
  # Prepara las estructuras de concurrencia necesarias para manejar múltiples
@@ -58,16 +65,31 @@ module BugBunny
58
65
  # @raise [BugBunny::RequestTimeout] Si el broker no confirma dentro de `confirm_timeout` segundos.
59
66
  # @raise [BugBunny::PublishNacked] Si el broker NACKea la publicación y `nack_raise` está activo
60
67
  # (default `true` — ver {BugBunny::Configuration#nack_raise}).
68
+ # @raise [BugBunny::PublishUnroutable] Si `mandatory: true` y el broker retorna el mensaje como
69
+ # no-ruteable, con `return_raise` activo (default `true` — ver
70
+ # {BugBunny::Configuration#return_raise}).
61
71
  # @raise [BugBunny::CommunicationError] Si el canal AMQP falla durante la publicación o confirm.
62
72
  def confirmed(request)
63
- publish_message(request)
73
+ return_listener = nil
74
+ return_listener = setup_return_listener(request)
75
+ started_at = monotonic_now
76
+ publish_duration = publish_message(request)
77
+
78
+ confirm_started_at = monotonic_now
64
79
  acked = wait_for_confirms!(request)
80
+ confirm_duration = duration_s(confirm_started_at)
81
+
65
82
  handle_confirm_result(request, acked)
83
+ handle_return_result(request, return_listener)
84
+ log_confirmed(request, publish_duration, confirm_duration, started_at)
85
+
66
86
  { 'status' => 202, 'body' => nil }
67
87
  rescue BugBunny::Error
68
88
  raise
69
89
  rescue StandardError => e
70
90
  raise BugBunny::CommunicationError, "Publisher confirms failed: #{e.message}"
91
+ ensure
92
+ teardown_return_listener(request, return_listener)
71
93
  end
72
94
 
73
95
  # Envía un mensaje y bloquea el hilo actual esperando una respuesta (RPC).
@@ -90,6 +112,7 @@ module BugBunny
90
112
  future = Concurrent::IVar.new
91
113
  @pending_requests[cid] = future
92
114
 
115
+ started_at = monotonic_now
93
116
  begin
94
117
  fire(request)
95
118
 
@@ -101,11 +124,7 @@ module BugBunny
101
124
  raise BugBunny::RequestTimeout, "Timeout waiting for RPC: #{request.path} [#{request.method}]" if result.nil?
102
125
 
103
126
  BugBunny.configuration.on_rpc_reply&.call(result[:headers])
104
-
105
- safe_log(:debug, 'producer.rpc_response_received',
106
- messaging_system: 'rabbitmq', messaging_operation: 'receive', messaging_message_id: cid,
107
- response_body: result[:body]&.truncate(500),
108
- response_headers: result[:headers]&.to_json&.truncate(300))
127
+ log_rpc_response_received(request, cid, result, started_at)
109
128
 
110
129
  parse_response(result[:body])
111
130
  ensure
@@ -119,8 +138,11 @@ module BugBunny
119
138
  # Resuelve exchange, serializa payload, logea y publica el mensaje.
120
139
  # Compartido por {#fire} y {#confirmed}.
121
140
  #
141
+ # Emite `producer.publish` antes y `producer.published` después con `duration_s`
142
+ # midiendo solo el `basic_publish` (TCP enqueue al broker, sin esperar ACK).
143
+ #
122
144
  # @param request [BugBunny::Request]
123
- # @return [void]
145
+ # @return [Float] duración del publish en segundos.
124
146
  def publish_message(request)
125
147
  x = @session.exchange(
126
148
  name: request.exchange,
@@ -129,7 +151,43 @@ module BugBunny
129
151
  )
130
152
  payload = serialize_message(request.body)
131
153
  log_request(request, payload)
154
+
155
+ started_at = monotonic_now
132
156
  x.publish(payload, request.amqp_options.merge(routing_key: request.final_routing_key))
157
+ publish_duration = duration_s(started_at)
158
+
159
+ log_published(request, publish_duration)
160
+ publish_duration
161
+ end
162
+
163
+ # @api private
164
+ def log_published(request, publish_duration_s)
165
+ safe_log(:info, 'producer.published',
166
+ method: request.method.to_s.upcase, path: request.path,
167
+ routing_key: request.final_routing_key,
168
+ messaging_message_id: request.correlation_id,
169
+ duration_s: publish_duration_s)
170
+ end
171
+
172
+ # @api private
173
+ def log_rpc_response_received(request, cid, result, started_at)
174
+ safe_log(:info, 'producer.rpc_response_received',
175
+ method: request.method.to_s.upcase, path: request.path,
176
+ messaging_system: 'rabbitmq', messaging_operation: 'receive', messaging_message_id: cid,
177
+ duration_s: duration_s(started_at),
178
+ response_body: result[:body]&.truncate(500),
179
+ response_headers: result[:headers]&.to_json&.truncate(300))
180
+ end
181
+
182
+ # @api private
183
+ def log_confirmed(request, publish_duration_s, confirm_duration_s, started_at)
184
+ safe_log(:info, 'producer.confirmed',
185
+ method: request.method.to_s.upcase, path: request.path,
186
+ routing_key: request.final_routing_key,
187
+ messaging_message_id: request.correlation_id,
188
+ publish_duration_s: publish_duration_s,
189
+ confirm_duration_s: confirm_duration_s,
190
+ duration_s: duration_s(started_at))
133
191
  end
134
192
 
135
193
  # Espera la confirmación del broker con timeout opcional.
@@ -197,6 +255,95 @@ module BugBunny
197
255
  BugBunny.configuration.nack_raise
198
256
  end
199
257
 
258
+ # Resuelve el flag `return_raise` con prioridad request > configuración global.
259
+ # El flag solo tiene efecto cuando `mandatory: true` — sin mandatory el broker
260
+ # nunca emite `basic.return`.
261
+ #
262
+ # @param request [BugBunny::Request]
263
+ # @return [Boolean]
264
+ def return_raise?(request)
265
+ return false unless request.mandatory
266
+
267
+ return request.return_raise unless request.return_raise.nil?
268
+
269
+ BugBunny.configuration.return_raise
270
+ end
271
+
272
+ # Registra un listener de `basic.return` en la Session asociada al `correlation_id`
273
+ # del request. Si return_raise? es false (mandatory off o flag desactivado), retorna
274
+ # `nil` y el resto del flow ignora la lógica de unroutable.
275
+ #
276
+ # Auto-asigna `correlation_id` si falta — sin cid no hay clave de correlación.
277
+ #
278
+ # @param request [BugBunny::Request]
279
+ # @return [Hash, nil] Hash con `:cid` y `:slot` (el slot expone `:event` y `:info`),
280
+ # o `nil` si no aplica.
281
+ def setup_return_listener(request)
282
+ return nil unless return_raise?(request)
283
+
284
+ request.correlation_id ||= SecureRandom.uuid
285
+ cid = request.correlation_id.to_s
286
+ _event, slot = @session.register_return_listener(cid)
287
+ { cid: cid, slot: slot }
288
+ end
289
+
290
+ # Tras un ack positivo, espera brevemente al event del listener para tolerar el
291
+ # scheduling race entre reader thread (donde corre `on_return`) y publish thread.
292
+ # En AMQP el `basic.return` precede al `basic.ack`, así que normalmente el event
293
+ # ya está seteado al llegar acá; el bounded wait defiende contra GVL/scheduling.
294
+ #
295
+ # @param request [BugBunny::Request]
296
+ # @param return_listener [Hash, nil] Resultado de {#setup_return_listener}.
297
+ # @return [void]
298
+ # @raise [BugBunny::PublishUnroutable] Si el listener recibió un return.
299
+ def handle_return_result(request, return_listener)
300
+ return unless return_listener
301
+
302
+ slot = return_listener[:slot]
303
+ slot[:event].wait(RETURN_RACE_WINDOW_S)
304
+ info = slot[:info]
305
+ return if info.nil?
306
+
307
+ raise_unroutable!(request, return_listener[:cid], info)
308
+ end
309
+
310
+ # Logea y levanta {BugBunny::PublishUnroutable} con los datos del return.
311
+ #
312
+ # @param request [BugBunny::Request]
313
+ # @param cid [String]
314
+ # @param info [Bunny::ReturnInfo, #exchange, #routing_key, #reply_code, #reply_text]
315
+ # @raise [BugBunny::PublishUnroutable]
316
+ def raise_unroutable!(request, cid, info)
317
+ safe_log(:warn, 'producer.publish_unroutable',
318
+ path: request.path,
319
+ exchange: info.exchange,
320
+ routing_key: info.routing_key,
321
+ reply_code: info.reply_code,
322
+ reply_text: info.reply_text,
323
+ messaging_message_id: cid)
324
+
325
+ raise BugBunny::PublishUnroutable.new(
326
+ path: request.path,
327
+ exchange: info.exchange,
328
+ routing_key: info.routing_key,
329
+ reply_code: info.reply_code,
330
+ reply_text: info.reply_text,
331
+ correlation_id: cid
332
+ )
333
+ end
334
+
335
+ # Cleanup del listener en la Session. Siempre se llama desde el `ensure` de
336
+ # {#confirmed}, sea cual sea el outcome (ack, nack, timeout, return).
337
+ #
338
+ # @param request [BugBunny::Request]
339
+ # @param return_listener [Hash, nil]
340
+ # @return [void]
341
+ def teardown_return_listener(_request, return_listener)
342
+ return unless return_listener
343
+
344
+ @session.unregister_return_listener(return_listener[:cid])
345
+ end
346
+
200
347
  # Registra la petición en el log calculando las opciones de infraestructura.
201
348
  #
202
349
  # @param request [BugBunny::Request] Objeto Request que se está enviando.
@@ -29,6 +29,9 @@ module BugBunny
29
29
  # `nil` espera indefinidamente.
30
30
  # @attr nack_raise [Boolean, nil] Override per-request del flag global
31
31
  # `BugBunny.configuration.nack_raise`. `nil` (default) delega a la configuración global.
32
+ # @attr return_raise [Boolean, nil] Override per-request del flag global
33
+ # `BugBunny.configuration.return_raise`. `nil` (default) delega a la configuración global.
34
+ # Requiere `mandatory: true` y `delivery_mode = :confirmed` para tener efecto.
32
35
  class Request
33
36
  attr_accessor :body, :headers, :params, :path, :method, :exchange, :exchange_type, :routing_key, :timeout,
34
37
  :delivery_mode, :queue_options
@@ -37,7 +40,7 @@ module BugBunny
37
40
  attr_accessor :exchange_options
38
41
 
39
42
  # Publisher Confirms (delivery_mode = :confirmed)
40
- attr_accessor :mandatory, :confirm_timeout, :nack_raise
43
+ attr_accessor :mandatory, :confirm_timeout, :nack_raise, :return_raise
41
44
 
42
45
  # Metadatos AMQP Estándar
43
46
  attr_accessor :app_id, :content_type, :content_encoding, :priority,
@@ -66,6 +69,7 @@ module BugBunny
66
69
  @mandatory = false
67
70
  @confirm_timeout = nil
68
71
  @nack_raise = nil
72
+ @return_raise = nil
69
73
  end
70
74
 
71
75
  # Combina el path con los params como query string.