#[derive(Debug)]
#[non_exhaustive]
#[allow(
clippy::large_enum_variant,
reason = "fires only with `transport-http`; one `Transport` is built per process, and boxing the variant would break `Transport::Http(cfg)`"
)]
pub enum Transport {
Stdio,
#[cfg(feature = "transport-http")]
#[cfg_attr(docsrs, doc(cfg(feature = "transport-http")))]
Http(HttpConfig),
}
#[cfg(feature = "transport-http")]
mod allowlist;
#[cfg(feature = "transport-http")]
mod body;
#[cfg(feature = "transport-http")]
mod config;
#[cfg(feature = "transport-http")]
mod connection_io;
#[cfg(feature = "transport-http")]
mod http;
#[cfg(test)]
#[cfg(feature = "transport-http")]
mod http_tests;
#[cfg(feature = "transport-http")]
mod lease;
#[cfg(feature = "transport-http")]
mod liveness;
#[cfg(feature = "transport-http")]
mod session_manager;
mod shutdown;
mod stdio;
#[cfg(test)]
#[cfg(feature = "transport-http")]
mod test_support;
#[cfg(feature = "transport-http")]
#[cfg_attr(docsrs, doc(cfg(feature = "transport-http")))]
pub use allowlist::{
AllowedHost, AllowedOrigin, HttpPath, InvalidAllowedHost, InvalidAllowedOrigin, InvalidHttpPath,
};
#[cfg(feature = "transport-http")]
#[cfg_attr(docsrs, doc(cfg(feature = "transport-http")))]
pub use config::{
ConnectionLimit, HeaderReadTimeout, HttpConfig, ProbeDeadline, ProbeInterval, RequestBodyLimit,
ResponseStreamDeadline, SessionLimit, StreamLiveness, WriteStallTimeout,
};
#[cfg(feature = "transport-http")]
pub(crate) use http::run_http;
#[cfg(feature = "transport-http")]
pub(crate) use lease::ListenLeaseSlot;
#[cfg(feature = "transport-http")]
#[cfg_attr(docsrs, doc(cfg(feature = "transport-http")))]
pub use lease::{LeaseWindow, ListenLease};
#[cfg(feature = "transport-http")]
#[cfg_attr(docsrs, doc(cfg(feature = "transport-http")))]
pub use session_manager::IdleTimeout;
pub(crate) use shutdown::ShutdownSignal;
pub(crate) use stdio::run_stdio;
#[cfg(test)]
pub(crate) use stdio::serve_until;
#[cfg(feature = "transport-http")]
const FAR_FUTURE: std::time::Duration = std::time::Duration::from_hours(262_800);
#[cfg(feature = "transport-http")]
fn saturating_deadline(
now: tokio::time::Instant,
after: std::time::Duration,
) -> tokio::time::Instant {
now.checked_add(after)
.or_else(|| now.checked_add(FAR_FUTURE))
.unwrap_or(now)
}
#[cfg(test)]
mod tests {
use std::assert_matches;
use super::*;
#[test]
fn test_transport_stdio_variant() {
let t = Transport::Stdio;
assert_matches!(t, Transport::Stdio);
}
#[cfg(feature = "transport-http")]
#[test]
fn test_saturating_deadline_adds_the_delay() {
let now = tokio::time::Instant::now();
let after = std::time::Duration::from_secs(5);
assert_eq!(saturating_deadline(now, after), now + after);
}
#[cfg(feature = "transport-http")]
#[test]
fn test_saturating_deadline_saturates_instead_of_overflowing() {
let now = tokio::time::Instant::now();
let deadline = saturating_deadline(now, std::time::Duration::MAX);
assert_eq!(deadline, now + FAR_FUTURE);
}
#[cfg(feature = "transport-http")]
#[test]
fn test_transport_http_variant() {
let cfg = HttpConfig::new("127.0.0.1:3002".parse().unwrap());
let t = Transport::Http(cfg);
assert_matches!(t, Transport::Http(_));
}
}