Skip to main content

AdaptiveChunking

Struct AdaptiveChunking 

Source
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

Source

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);
Source

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 channel
  • capacity - Total capacity of the channel (usually equal to chunk_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
}
Source

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);
Source

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§

Source§

impl Default for AdaptiveChunking

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more