Skip to main content

StreamPriorityScheduler

Struct StreamPriorityScheduler 

Source
pub struct StreamPriorityScheduler { /* private fields */ }
Expand description

A stream-level priority scheduler for multiplexed network streams.

§Scheduling Policies

Use SpsSchedulingPolicy to select the algorithm per call or batch.

§Example

use ipfrs_network::stream_priority_scheduler::{
    StreamPriorityScheduler, SpsSchedulingPolicy, SpsSchedulerConfig,
};

let mut scheduler = StreamPriorityScheduler::new(SpsSchedulerConfig::default());
scheduler.add_stream(1, 100, 10).unwrap();
scheduler.add_stream(2, 50,  5).unwrap();
scheduler.enqueue_bytes(1, 4096).unwrap();
scheduler.enqueue_bytes(2, 2048).unwrap();

let result = scheduler.schedule_next(&SpsSchedulingPolicy::StrictPriority);
assert_eq!(result.map(|(id, _)| id), Some(1));

Implementations§

Source§

impl StreamPriorityScheduler

Source

pub fn new(config: SpsSchedulerConfig) -> Self

Create a new scheduler with the supplied configuration.

Source

pub fn add_stream( &mut self, id: SpsStreamId, priority: u32, weight: u32, ) -> Result<(), SpsError>

Register a new stream.

§Errors

Returns SpsError::MaxStreamsReached if config.max_streams is exceeded, or SpsError::DuplicateStream if the id is already registered.

Source

pub fn remove_stream(&mut self, id: SpsStreamId) -> Result<SpsStream, SpsError>

Remove a stream and all its pending data from the scheduler.

§Errors

Returns SpsError::StreamNotFound if the id is not registered.

Source

pub fn block_stream(&mut self, id: SpsStreamId) -> Result<(), SpsError>

Block a stream — it will be skipped during scheduling.

§Errors

Returns SpsError::StreamNotFound if the id is not registered.

Source

pub fn unblock_stream(&mut self, id: SpsStreamId) -> Result<(), SpsError>

Unblock a previously blocked stream.

§Errors

Returns SpsError::StreamNotFound if the id is not registered.

Source

pub fn enqueue_bytes( &mut self, stream_id: SpsStreamId, bytes: u64, ) -> Result<(), SpsError>

Add bytes of pending data to a stream’s queue.

Also inserts the stream into the appropriate priority queue if it was previously empty.

§Errors

Returns SpsError::StreamNotFound if the id is not registered.

Source

pub fn schedule_next( &mut self, policy: &SpsSchedulingPolicy, ) -> Option<(SpsStreamId, u64)>

Schedule the next stream according to policy.

Returns Some((stream_id, bytes_to_send)) where bytes_to_send is the number of bytes that should be dispatched this round (bounded by config.quantum_bytes for non-strict policies).

Returns None if there are no eligible streams.

Source

pub fn schedule_batch( &mut self, policy: &SpsSchedulingPolicy, n: usize, ) -> Vec<(SpsStreamId, u64)>

Schedule a batch of up to n streams.

Each element of the returned Vec is (stream_id, bytes_to_send).

§Errors (returned via empty vec)

If n == 0 the returned vec is empty.

Source

pub fn run_drr_round(&mut self) -> Vec<(SpsStreamId, u64)>

Run one or more complete Deficit Round Robin rounds.

Each eligible stream receives a quantum of config.quantum_bytes added to its deficit counter. Streams transmit until their deficit is exhausted. The number of rounds is controlled by config.deficit_rounds.

Returns a list of (stream_id, bytes_to_send) decisions from this round.

Source

pub fn compute_fairness(&self) -> f64

Compute Jain’s fairness index over all streams’ bytes_sent.

Returns a value in [0.0, 1.0] where 1.0 is perfectly fair. Returns 1.0 if there are fewer than two streams.

Source

pub fn scheduler_stats(&mut self) -> SpsSchedulerStats

Return a snapshot of current scheduler statistics.

Source

pub fn get_stream(&self, id: SpsStreamId) -> Option<&SpsStream>

Return a reference to a stream, or None.

Source

pub fn get_stream_mut(&mut self, id: SpsStreamId) -> Option<&mut SpsStream>

Return a mutable reference to a stream, or None.

Source

pub fn set_deadline( &mut self, id: SpsStreamId, deadline: u64, ) -> Result<(), SpsError>

Set the deadline for EDF scheduling on a stream.

Smaller values are scheduled earlier.

Source

pub fn set_htb_params( &mut self, id: SpsStreamId, rate: u64, burst: u64, ) -> Result<(), SpsError>

Configure HTB token-bucket parameters for a stream.

Source

pub fn htb_refill(&mut self, ticks: u64)

Refill HTB token buckets for all streams by ticks time units.

Source

pub fn tick(&self) -> u64

Return the current tick counter.

Source

pub fn stream_count(&self) -> usize

Return the number of currently registered streams.

Source

pub fn eligible_count(&self) -> usize

Return the number of eligible (non-blocked, non-empty) streams.

Source

pub fn drain_stream(&mut self, id: SpsStreamId) -> Result<u64, SpsError>

Drain all pending bytes from a stream without scheduling it.

Useful for flow-control / back-pressure scenarios.

Source

pub fn update_priority( &mut self, id: SpsStreamId, new_priority: u32, ) -> Result<(), SpsError>

Update the priority of a stream.

This moves the stream to the new priority queue if it currently has data.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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