Merged
Conversation
mkuratczyk
approved these changes
Mar 26, 2026
52cecea to
42a009f
Compare
ansd
approved these changes
Mar 27, 2026
ansd
added a commit
that referenced
this pull request
Mar 27, 2026
Optimise message expiry (backport #15846)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optimise message expiry
Problem
The expire function in rabbit_fifo processed expired messages one at a time in a recursive loop: it would take a single message, call discard_or_dead_letter for that one message, update the state, and then recurse via expire_msgs to check for the next expired message. This meant that if N messages were expired, discard_or_dead_letter was called N times (once per message), and the state record was rebuilt N times.
Similarly, expire_shallow (used by the expire_msgs timer callback) only expired a single returned message from the returns queue before moving on to the messages queue.
Changes
Replace the recursive one-at-a-time expire/expire_msgs loop with a batch-oriented expire_batch that collects all expired messages from both the returns queue and the messages priority queue in one pass, then processes them with a single discard_or_dead_letter call
Extract take_expired_returns/2 to drain all consecutive expired messages from the returns queue (previously only the head was checked)
Extract count_and_size/1 to compute the count and total byte size of expired messages in a single fold
Reuse expire_batch from expire_shallow instead of duplicating the expiry logic
Effect
Reduces the number of discard_or_dead_letter calls from N (one per expired message) to 1 per expiry pass
Reduces intermediate state record allocations from N to 1
Eliminates code duplication between expire_shallow and expire