Skip to main content

rama/cli/
connection.rs

1//! The scope every connection a CLI service serves runs in.
2
3use crate::{graceful::WeakScope, layer::ScopeLayer};
4use std::time::Duration;
5
6/// How long a connection may still run once it drains, unless configured otherwise.
7pub const DEFAULT_CONNECTION_GRACE: Duration = Duration::from_secs(5);
8
9/// The share either way by which a connection's max age is spread.
10const MAX_AGE_JITTER: f64 = 0.1;
11
12/// Runs each served connection in a scope below `parent` that starts draining once it is
13/// `max_age` old, give or take a tenth so connections opened together do not drain together,
14/// and is aborted at most `grace` after it starts draining for any reason, sooner when
15/// `parent` reaches its own hard phase first; zero `max_age` sets no age.
16#[must_use]
17pub fn connection_scope(parent: &WeakScope, max_age: Duration, grace: Duration) -> ScopeLayer {
18    ScopeLayer::new()
19        .with_parent(parent.clone())
20        .maybe_with_max_age((!max_age.is_zero()).then_some(max_age))
21        .with_max_age_jitter(MAX_AGE_JITTER)
22        .with_grace(grace)
23}