pub struct AdaptiveChunking { /* 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);
}
}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);Trait Implementations§
Auto Trait Implementations§
impl Freeze for AdaptiveChunking
impl RefUnwindSafe for AdaptiveChunking
impl Send for AdaptiveChunking
impl Sync for AdaptiveChunking
impl Unpin for AdaptiveChunking
impl UnsafeUnpin for AdaptiveChunking
impl UnwindSafe for AdaptiveChunking
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more