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
impl StreamPriorityScheduler
Sourcepub fn new(config: SpsSchedulerConfig) -> Self
pub fn new(config: SpsSchedulerConfig) -> Self
Create a new scheduler with the supplied configuration.
Sourcepub fn add_stream(
&mut self,
id: SpsStreamId,
priority: u32,
weight: u32,
) -> Result<(), SpsError>
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.
Sourcepub fn remove_stream(&mut self, id: SpsStreamId) -> Result<SpsStream, SpsError>
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.
Sourcepub fn block_stream(&mut self, id: SpsStreamId) -> Result<(), SpsError>
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.
Sourcepub fn unblock_stream(&mut self, id: SpsStreamId) -> Result<(), SpsError>
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.
Sourcepub fn enqueue_bytes(
&mut self,
stream_id: SpsStreamId,
bytes: u64,
) -> Result<(), SpsError>
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.
Sourcepub fn schedule_next(
&mut self,
policy: &SpsSchedulingPolicy,
) -> Option<(SpsStreamId, u64)>
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.
Sourcepub fn schedule_batch(
&mut self,
policy: &SpsSchedulingPolicy,
n: usize,
) -> Vec<(SpsStreamId, u64)>
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.
Sourcepub fn run_drr_round(&mut self) -> Vec<(SpsStreamId, u64)>
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.
Sourcepub fn compute_fairness(&self) -> f64
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.
Sourcepub fn scheduler_stats(&mut self) -> SpsSchedulerStats
pub fn scheduler_stats(&mut self) -> SpsSchedulerStats
Return a snapshot of current scheduler statistics.
Sourcepub fn get_stream(&self, id: SpsStreamId) -> Option<&SpsStream>
pub fn get_stream(&self, id: SpsStreamId) -> Option<&SpsStream>
Return a reference to a stream, or None.
Sourcepub fn get_stream_mut(&mut self, id: SpsStreamId) -> Option<&mut SpsStream>
pub fn get_stream_mut(&mut self, id: SpsStreamId) -> Option<&mut SpsStream>
Return a mutable reference to a stream, or None.
Sourcepub fn set_deadline(
&mut self,
id: SpsStreamId,
deadline: u64,
) -> Result<(), SpsError>
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.
Sourcepub fn set_htb_params(
&mut self,
id: SpsStreamId,
rate: u64,
burst: u64,
) -> Result<(), SpsError>
pub fn set_htb_params( &mut self, id: SpsStreamId, rate: u64, burst: u64, ) -> Result<(), SpsError>
Configure HTB token-bucket parameters for a stream.
Sourcepub fn htb_refill(&mut self, ticks: u64)
pub fn htb_refill(&mut self, ticks: u64)
Refill HTB token buckets for all streams by ticks time units.
Sourcepub fn stream_count(&self) -> usize
pub fn stream_count(&self) -> usize
Return the number of currently registered streams.
Sourcepub fn eligible_count(&self) -> usize
pub fn eligible_count(&self) -> usize
Return the number of eligible (non-blocked, non-empty) streams.
Sourcepub fn drain_stream(&mut self, id: SpsStreamId) -> Result<u64, SpsError>
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.
Sourcepub fn update_priority(
&mut self,
id: SpsStreamId,
new_priority: u32,
) -> Result<(), SpsError>
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§
impl Freeze for StreamPriorityScheduler
impl RefUnwindSafe for StreamPriorityScheduler
impl Send for StreamPriorityScheduler
impl Sync for StreamPriorityScheduler
impl Unpin for StreamPriorityScheduler
impl UnsafeUnpin for StreamPriorityScheduler
impl UnwindSafe for StreamPriorityScheduler
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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