pub struct AdaptiveChunking {
pub min_size: usize,
pub max_size: usize,
pub adjustment_window: usize,
pub measurements: VecDeque<Occupancy>,
pub last_adjustment_time: Option<Instant>,
/* private fields */
}Expand description
Tracks channel occupancy and automatically adjusts chunk size based on backpressure
§Examples
use fraiseql_wire::stream::AdaptiveChunking;
let mut adaptive = AdaptiveChunking::new();
let (buffered_items, channel_capacity) = (50usize, 256usize);
// Periodically observe channel occupancy
for _chunk_sent in 0..100 {
if let Some(new_size) = adaptive.observe(buffered_items, channel_capacity) {
println!("Adjusted chunk size to {}", new_size);
}
}Fields§
§min_size: usizeAbsolute minimum chunk size (never decrease below this)
max_size: usizeAbsolute maximum chunk size (never increase beyond this)
adjustment_window: usizeNumber of measurements to collect before making adjustment decision
measurements: VecDeque<Occupancy>Rolling window of recent occupancy observations
last_adjustment_time: Option<Instant>Timestamp of last chunk size adjustment (for rate limiting)
Implementations§
Source§impl AdaptiveChunking
impl AdaptiveChunking
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new adaptive chunking controller with default bounds
Defaults:
- Initial chunk size: 256 items
- Min size: 16 items
- Max size: 1024 items
- Adjustment window: 50 observations
- Min adjustment interval: 1 second
§Examples
use fraiseql_wire::stream::AdaptiveChunking;
let adaptive = AdaptiveChunking::new();
assert_eq!(adaptive.current_size(), 256);Sourcepub fn observe(
&mut self,
items_buffered: usize,
capacity: usize,
) -> Option<usize>
pub fn observe( &mut self, items_buffered: usize, capacity: usize, ) -> Option<usize>
Record an occupancy observation and check if chunk size adjustment is warranted
Call this method after each chunk is sent to the channel.
Returns Some(new_size) if an adjustment should be applied, None otherwise.
§Arguments
items_buffered- Number of items currently in the channelcapacity- Total capacity of the channel (usually equal tochunk_size)
§Examples
use fraiseql_wire::stream::AdaptiveChunking;
let mut adaptive = AdaptiveChunking::new();
// Simulate high occupancy (90%)
for _ in 0..50 {
adaptive.observe(230, 256); // ~90% occupancy
}
// On the 51st observation, should trigger adjustment
if let Some(new_size) = adaptive.observe(230, 256) {
println!("Adjusted to {}", new_size); // Will be < 256
}Sourcepub const fn current_size(&self) -> usize
pub const fn current_size(&self) -> usize
Get the current chunk size
§Examples
use fraiseql_wire::stream::AdaptiveChunking;
let adaptive = AdaptiveChunking::new();
assert_eq!(adaptive.current_size(), 256);Sourcepub fn with_bounds(self, min_size: usize, max_size: usize) -> Self
pub fn with_bounds(self, min_size: usize, max_size: usize) -> Self
Set custom min/max bounds for chunk size adjustments
Allows overriding the default bounds (16-1024) with custom limits. The current chunk size will be clamped to the new bounds.
§Arguments
min_size- Minimum chunk size (must be > 0)max_size- Maximum chunk size (must be >=min_size)
§Examples
use fraiseql_wire::stream::AdaptiveChunking;
let mut adaptive = AdaptiveChunking::new();
adaptive = adaptive.with_bounds(32, 512); // Custom range 32-512
assert!(adaptive.current_size() >= 32);
assert!(adaptive.current_size() <= 512);Sourcepub fn average_occupancy(&self) -> usize
pub fn average_occupancy(&self) -> usize
Calculate average occupancy percentage over the measurement window