Technical detail · AMQP Reliability: Testing and Pitfalls
Adapt prefetch within a tested ceiling
This optional consumer example collects 100 processing times before adjusting its prefetch. The default target latency is 100 ms. The calculated reserve has a minimum of one and a ceiling of 20, so adaptation cannot create an unlimited window. Changing that window does not increase a sequential worker’s processing capacity. Test fairness with mixed consumer speeds before using it.
# OPTIONAL: Adaptive prefetch, with a configured ceiling and fairness test
class AdaptiveConsumer:
def __init__(self, channel, target_latency_ms=100):
self.channel = channel
self.target_latency = target_latency_ms / 1000
self.processing_times = []
self.current_prefetch = 1
def adjust_prefetch(self, processing_time):
self.processing_times.append(processing_time)
if len(self.processing_times) >= 100:
avg_time = sum(self.processing_times) / len(self.processing_times)
# This bounds the reserve; it does not increase sequential worker capacity.
optimal = min(20, max(1, int(self.target_latency / avg_time * 2)))
if optimal != self.current_prefetch:
self.channel.basic_qos(prefetch_count=optimal)
self.current_prefetch = optimal
self.processing_times = []