subspace_networking/utils/
rate_limiter.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::num::NonZeroUsize;
use std::sync::Arc;
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use tracing::debug;

/// Defines the minimum size of the "connection limit semaphore".
const MINIMUM_CONNECTIONS_SEMAPHORE_SIZE: usize = 3;

/// Empiric parameter for connection timeout and retry parameters (total retries and backoff time).
const CONNECTION_TIMEOUT_PARAMETER: usize = 2;

#[derive(Debug)]
pub(crate) struct RateLimiter {
    connections_semaphore: Arc<Semaphore>,
}

impl RateLimiter {
    pub(crate) fn new(out_connections: u32, pending_out_connections: u32) -> Self {
        let permits = Self::calculate_connection_semaphore_size(
            out_connections as usize,
            pending_out_connections as usize,
        );

        debug!(%out_connections, %pending_out_connections, %permits, "Rate limiter was instantiated.");

        Self {
            connections_semaphore: Arc::new(Semaphore::new(permits.get())),
        }
    }

    /// Calculates an empiric formula for the semaphore size based on the connection parameters and
    /// existing constants.
    fn calculate_connection_semaphore_size(
        out_connections: usize,
        pending_out_connections: usize,
    ) -> NonZeroUsize {
        let connections = out_connections.min(pending_out_connections);

        // Number of "in-flight" parallel requests for each query
        let kademlia_parallelism_level = libp2p::kad::ALPHA_VALUE.get();

        let permits_number =
            (connections / (kademlia_parallelism_level * CONNECTION_TIMEOUT_PARAMETER)).max(1);

        let minimum_semaphore_size =
            NonZeroUsize::new(MINIMUM_CONNECTIONS_SEMAPHORE_SIZE).expect("Manual setting");

        NonZeroUsize::new(permits_number)
            .expect("The value is at least 1")
            .max(minimum_semaphore_size)
    }

    pub(crate) async fn acquire_permit(&self) -> OwnedSemaphorePermit {
        self.connections_semaphore
            .clone()
            .acquire_owned()
            .await
            .expect("We never close semaphore.")
    }
}