Skip to content

Commit caf5972

Browse files
khaliqgantclaude
andcommitted
fix(scheduler): add 32KiB coalescing body size limit
Without a body-size bound, a burst of large messages from the same sender within the coalesce window would concatenate without limit, potentially causing large memory allocations. Add MAX_COALESCED_BODY_SIZE (32 KiB) check that flushes the current group when exceeded. Cherry-picked from PR #412. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 40ee244 commit caf5972

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

relay-broker/src/scheduler.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ use std::{
55

66
use crate::types::{InjectRequest, RelayPriority};
77

8+
/// Maximum coalesced message body size (32 KiB). When exceeded, the current
9+
/// group is flushed and the new message starts a fresh coalesce window.
10+
const MAX_COALESCED_BODY_SIZE: usize = 32 * 1024;
11+
812
#[derive(Debug, Clone)]
913
struct CoalesceState {
1014
request: InjectRequest,
@@ -54,7 +58,9 @@ impl Scheduler {
5458
if let Some(state) = self.pending.get_mut(&key) {
5559
let within_window = now.duration_since(state.last_seen) <= self.coalesce_window;
5660
let within_hold = now.duration_since(state.first_seen) <= self.max_hold;
57-
if within_window && within_hold {
61+
let within_size =
62+
state.request.body.len() + 1 + req.body.len() <= MAX_COALESCED_BODY_SIZE;
63+
if within_window && within_hold && within_size {
5864
state.request.body.push('\n');
5965
state.request.body.push_str(&req.body);
6066
state.last_seen = now;

0 commit comments

Comments
 (0)