Skip to main content

AdaptivePeerScheduler

Struct AdaptivePeerScheduler 

Source
pub struct AdaptivePeerScheduler {
    pub config: SchedulerConfig,
    pub peers: HashMap<String, PeerMetrics>,
    pub schedule: HashMap<String, ScheduleSlot>,
    pub global_backpressure: BackpressureSignal,
    pub total_requests_dispatched: u64,
    pub total_requests_succeeded: u64,
}
Expand description

A dynamic peer request scheduler that adapts request rates based on peer performance, network conditions, and backpressure signals.

§Usage

use ipfrs_network::{
    ApsSchedulerConfig, ApsBackpressureSignal, AdaptivePeerScheduler,
};

let config = ApsSchedulerConfig::default();
let mut sched = AdaptivePeerScheduler::new(config);

sched.register_peer("peer-1".to_string(), 0);
sched.record_success("peer-1", 50, 100);
sched.recompute_schedule(200);

if let Some(peer) = sched.next_peer() {
    println!("dispatch to {peer}");
}

Fields§

§config: SchedulerConfig

Scheduler configuration.

§peers: HashMap<String, PeerMetrics>

Per-peer performance metrics.

§schedule: HashMap<String, ScheduleSlot>

Last computed schedule.

§global_backpressure: BackpressureSignal

Current global backpressure signal.

§total_requests_dispatched: u64

Monotonically increasing counter of dispatched requests.

§total_requests_succeeded: u64

Monotonically increasing counter of succeeded requests.

Implementations§

Source§

impl AdaptivePeerScheduler

Source

pub fn new(config: SchedulerConfig) -> Self

Creates a new scheduler with the supplied configuration.

Source

pub fn register_peer(&mut self, peer_id: String, now: u64)

Registers a peer with the scheduler.

If the peer is already known this is a no-op (existing metrics are preserved). When the peer table is full the peer with the oldest last_seen timestamp is evicted to make room.

Source

pub fn remove_peer(&mut self, peer_id: &str) -> bool

Removes a peer from both the metrics table and the schedule.

Returns true if the peer was present and has been removed.

Source

pub fn record_success(&mut self, peer_id: &str, latency_ms: u64, now: u64)

Records a successful request completion for the given peer.

Also increments the global total_requests_succeeded counter. Silently ignores unknown peer IDs.

Source

pub fn record_failure(&mut self, peer_id: &str, now: u64)

Records a request failure for the given peer.

Silently ignores unknown peer IDs.

Source

pub fn set_backpressure(&mut self, signal: BackpressureSignal)

Updates the global backpressure signal.

The new signal takes effect on the next call to recompute_schedule.

Source

pub fn compute_weight(&self, metrics: &PeerMetrics) -> f64

Computes the scheduling weight for a single peer.

Formula:

weight = success_weight  * success_rate
       + latency_weight  * (1 / (1 + avg_latency_ms / 1000))

The result is clamped to [0.01, 1.0].

Additionally, if the peer’s success rate is below config.backpressure_threshold, the weight is further multiplied by config.failure_penalty.

Source

pub fn recompute_schedule(&mut self, now: u64)

Recomputes the entire schedule based on current peer metrics and the active backpressure signal.

For each registered peer:

  1. The weight is computed via compute_weight.
  2. allocated = clamp(round(weight × base × bp_factor), min, max). When backpressure is BackpressureSignal::Overloaded the allocation is forced to 0 regardless of the clamp range.
  3. The ScheduleSlot in self.schedule is updated (or inserted).
Source

pub fn next_peer(&self) -> Option<&str>

Returns the peer ID with the highest allocated_requests in the current schedule (among peers with at least one allocated request).

Returns None when every slot is at zero (or the schedule is empty).

Also increments total_requests_dispatched when a peer is returned.

Source

pub fn mark_dispatched(&mut self)

Increments the total dispatched counter. Callers should call this once they have committed to sending a request to the peer returned by next_peer.

Source

pub fn peek_schedule(&self) -> Vec<(&str, u32, f64)>

Returns a snapshot of the schedule sorted by allocated_requests descending.

Each entry is (peer_id, allocated_requests, weight).

Source

pub fn evict_stale_peers(&mut self, now: u64, max_idle_ms: u64)

Removes all peers that have not been seen within the last max_idle_ms milliseconds (i.e. where now - last_seen > max_idle_ms).

Source

pub fn scheduler_stats(&self) -> SchedulerStats

Returns a statistics snapshot.

Trait Implementations§

Source§

impl Debug for AdaptivePeerScheduler

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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