Backpressure¶
Set max_pending to bound how many unresolved deliveries a channel allows. When the limit is reached, publish blocks the calling fiber until subscribers ACK and free capacity.
Enabling Backpressure¶
Or via MessageBus:
Or via the configuration cascade:
TypedBus.configure { |c| c.max_pending = 500 }
bus = TypedBus::MessageBus.new
bus.add_channel(:work) # inherits max_pending=500
How It Works¶
sequenceDiagram
participant P as Publisher Fiber
participant C as Channel
participant S as Subscriber Fiber
P->>C: publish(msg)
C-->>C: pending_count >= max_pending?
Note over C: YES — block publisher
S->>C: delivery.ack!
C-->>C: pending_count drops
C->>P: resume (signal)
P->>C: publish continues
- Before fan-out, the channel checks
pending_deliveries.size >= max_pending - If at capacity, the publishing fiber waits on an
Async::Condition - When any delivery tracker resolves (all subscribers responded), the condition signals
- The publisher wakes and proceeds
Unbounded Channels¶
Set max_pending: nil (the default) for unbounded channels. Publishers never block.
Interaction with Throttling¶
When both max_pending and throttle are set, throttling runs first. The throttle progressively slows publishers as capacity fills; backpressure is the hard stop at zero remaining capacity.
See Adaptive Throttling for details.