Skip to main content

linera_core/client/requests_scheduler/
mod.rs

1// Copyright (c) Zefchain Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4//! This module manages communication with validator nodes, including
5//! load balancing, request deduplication, caching, and performance tracking.
6
7mod cache;
8mod in_flight_tracker;
9mod node_info;
10mod request;
11mod scheduler;
12mod scoring;
13
14pub use scheduler::RequestsScheduler;
15pub use scoring::ScoringWeights;
16
17// Module constants - default values for RequestsSchedulerConfig
18/// Default maximum number of requests allowed to be in flight at once.
19pub const MAX_IN_FLIGHT_REQUESTS: usize = 100;
20/// Default maximum expected latency in milliseconds, used for score normalization.
21pub const MAX_ACCEPTED_LATENCY_MS: f64 = 5000.0;
22/// Default time-to-live for cached responses, in milliseconds.
23pub const CACHE_TTL_MS: u64 = 2000;
24/// Default maximum number of entries in the cache.
25pub const CACHE_MAX_SIZE: usize = 1000;
26/// Default maximum latency for an in-flight request before we stop deduplicating it, in milliseconds.
27pub const MAX_REQUEST_TTL_MS: u64 = 200;
28/// Default smoothing factor for the Exponential Moving Averages of latency.
29pub const ALPHA_SMOOTHING_FACTOR: f64 = 0.1;
30/// Default delay in milliseconds between starting requests to different peers.
31pub const STAGGERED_DELAY_MS: u64 = 150;
32
33/// Configuration for the `RequestsScheduler`.
34#[derive(Debug, Clone)]
35pub struct RequestsSchedulerConfig {
36    /// Maximum expected latency in milliseconds for score normalization
37    pub max_accepted_latency_ms: f64,
38    /// Time-to-live for cached responses in milliseconds
39    pub cache_ttl_ms: u64,
40    /// Maximum number of entries in the cache
41    pub cache_max_size: usize,
42    /// Maximum latency for an in-flight request before we stop deduplicating it (in milliseconds)
43    pub max_request_ttl_ms: u64,
44    /// Smoothing factor for Exponential Moving Averages (0 < alpha < 1)
45    pub alpha: f64,
46    /// Delay in milliseconds between starting requests to different peers.
47    pub retry_delay_ms: u64,
48}
49
50impl Default for RequestsSchedulerConfig {
51    fn default() -> Self {
52        Self {
53            max_accepted_latency_ms: MAX_ACCEPTED_LATENCY_MS,
54            cache_ttl_ms: CACHE_TTL_MS,
55            cache_max_size: CACHE_MAX_SIZE,
56            max_request_ttl_ms: MAX_REQUEST_TTL_MS,
57            alpha: ALPHA_SMOOTHING_FACTOR,
58            retry_delay_ms: STAGGERED_DELAY_MS,
59        }
60    }
61}
62
63#[cfg(with_metrics)]
64pub(crate) fn init_metrics() {
65    scheduler::metrics::init_metrics();
66}