pub mod bench;
pub mod billing;
pub mod capture;
pub mod churn;
pub mod emit;
pub mod mock;
pub mod preflight;
pub mod proxy;
pub mod retention;
pub mod session;
pub mod tokenize;
pub mod transforms;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use axum::extract::DefaultBodyLimit;
use axum::routing::get;
use axum::Router;
use tokio::net::TcpListener;
use tokio::sync::mpsc::Sender;
use tokio::sync::Semaphore;
use crate::cloud::CloudEvent;
use crate::core::policy::{PolicyHandle, ResidentBundle};
use crate::error::{OlError, ERR_BOUNDARY_SERVE};
use crate::privacy::PrivacyFilter;
use session::SessionRegistry;
use tokenize::Estimator;
pub const DEFAULT_BOUNDARY_PORT: u16 = 7600;
pub const DEFAULT_INFLIGHT: usize = 16;
pub const MAX_MATERIALIZE_BYTES: usize = 32 * 1024 * 1024;
pub const HEADER_TIMEOUT: Duration = Duration::from_secs(60);
pub const ANTHROPIC_BASE: &str = "https://api.anthropic.com";
pub struct BoundaryState {
pub client: reqwest::Client,
pub upstream_base: reqwest::Url,
pub inflight: Arc<Semaphore>,
pub privacy: PrivacyFilter,
pub started_at: std::time::Instant,
pub port: u16,
pub header_timeout: Duration,
pub registry: Arc<SessionRegistry>,
pub cloud_tx: Option<Sender<CloudEvent>>,
pub tokenizer: Estimator,
pub churn: Arc<churn::ChurnTracker>,
pub policy: Option<PolicyHandle>,
pub wiring: Arc<preflight::WiringState>,
}
impl BoundaryState {
pub fn new(
upstream_base: reqwest::Url,
port: u16,
inflight: usize,
extra_patterns: &[String],
) -> Self {
Self {
client: build_boundary_client(),
upstream_base,
inflight: Arc::new(Semaphore::new(inflight.max(1))),
privacy: PrivacyFilter::new(extra_patterns),
started_at: std::time::Instant::now(),
port,
header_timeout: HEADER_TIMEOUT,
registry: Arc::new(SessionRegistry::default()),
cloud_tx: None,
tokenizer: Estimator,
churn: Arc::new(churn::ChurnTracker::default()),
policy: None,
wiring: Arc::new(preflight::WiringState::default()),
}
}
pub fn with_header_timeout(mut self, timeout: Duration) -> Self {
self.header_timeout = timeout;
self
}
pub fn with_measurement(
mut self,
registry: Arc<SessionRegistry>,
cloud_tx: Option<Sender<CloudEvent>>,
) -> Self {
self.registry = registry;
self.cloud_tx = cloud_tx;
self
}
pub fn with_policy(mut self, policy: Option<PolicyHandle>) -> Self {
self.policy = policy;
self
}
pub fn with_wiring(mut self, wiring: Arc<preflight::WiringState>) -> Self {
self.wiring = wiring;
self
}
pub fn resident_request_rules(&self) -> Option<arc_swap::Guard<Arc<Option<ResidentBundle>>>> {
self.policy.as_ref().map(|handle| handle.load())
}
}
pub fn default_upstream() -> reqwest::Url {
reqwest::Url::parse(ANTHROPIC_BASE).expect("ANTHROPIC_BASE is a valid URL")
}
pub fn resolve_boundary_port() -> u16 {
default_boundary_port()
}
pub fn default_boundary_port() -> u16 {
match std::env::var("OPENLATCH_BOUNDARY_DEFAULT_PORT") {
Ok(v) => v.trim().parse::<u16>().unwrap_or(DEFAULT_BOUNDARY_PORT),
Err(_) => DEFAULT_BOUNDARY_PORT,
}
}
pub fn build_boundary_client() -> reqwest::Client {
reqwest::Client::builder()
.connect_timeout(Duration::from_secs(10))
.pool_max_idle_per_host(8)
.use_rustls_tls()
.build()
.expect("failed to build boundary reqwest client")
}
pub fn router(state: Arc<BoundaryState>) -> Router {
Router::new()
.route("/admin/boundary/status", get(proxy::boundary_status))
.fallback(proxy::proxy_any)
.layer(DefaultBodyLimit::max(MAX_MATERIALIZE_BYTES))
.with_state(state)
}
pub async fn serve_ephemeral(state: Arc<BoundaryState>) -> u16 {
let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
let port = listener.local_addr().unwrap().port();
let app = router(state);
tokio::spawn(async move {
let _ = axum::serve(listener, app).await;
});
port
}
pub async fn bind_pinned(port: u16) -> Result<TcpListener, OlError> {
let addr = SocketAddr::from(([127, 0, 0, 1], port));
TcpListener::bind(addr)
.await
.map_err(|e| OlError::port_occupied(port, e))
}
pub async fn serve_bound(
listener: TcpListener,
state: Arc<BoundaryState>,
shutdown_rx: tokio::sync::watch::Receiver<bool>,
) -> Result<(), OlError> {
tracing::info!(port = state.port, upstream = %state.upstream_base, "boundary serving (loopback only)");
serve_until_shutdown(listener, state, shutdown_rx).await
}
pub async fn serve_attempt(
pre_bound: Arc<tokio::sync::Mutex<Option<TcpListener>>>,
state: Arc<BoundaryState>,
shutdown_rx: tokio::sync::watch::Receiver<bool>,
) -> Result<(), OlError> {
let listener = match pre_bound.lock().await.take() {
Some(l) => l,
None => bind_pinned(state.port).await?,
};
serve_bound(listener, state, shutdown_rx).await
}
async fn serve_until_shutdown(
listener: TcpListener,
state: Arc<BoundaryState>,
mut shutdown_rx: tokio::sync::watch::Receiver<bool>,
) -> Result<(), OlError> {
let app = router(state);
axum::serve(listener, app)
.with_graceful_shutdown(async move {
let _ = shutdown_rx.wait_for(|stop| *stop).await;
})
.await
.map_err(|e| OlError::new(ERR_BOUNDARY_SERVE, format!("boundary serve failed: {e}")))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_upstream_parses() {
let u = default_upstream();
assert_eq!(u.as_str(), "https://api.anthropic.com/");
}
#[test]
fn resolve_boundary_port_is_deterministic() {
assert_eq!(resolve_boundary_port(), DEFAULT_BOUNDARY_PORT);
assert_eq!(resolve_boundary_port(), DEFAULT_BOUNDARY_PORT);
}
#[tokio::test]
async fn boundary_listener_terminates_on_shutdown_signal() {
let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
let port = listener.local_addr().unwrap().port();
let state = Arc::new(BoundaryState::new(default_upstream(), port, 8, &[]));
let serve = tokio::spawn(serve_bound(listener, state, shutdown_rx));
assert!(
bind_pinned(port).await.is_err(),
"port must be held while the boundary is serving"
);
shutdown_tx.send(true).unwrap();
let joined = tokio::time::timeout(Duration::from_secs(5), serve)
.await
.expect("boundary listener did not terminate after shutdown signal")
.expect("boundary serve task panicked");
assert!(joined.is_ok(), "graceful shutdown should return Ok");
assert!(
bind_pinned(port).await.is_ok(),
"port must be free after graceful shutdown"
);
}
#[tokio::test]
async fn supervised_boundary_retries_a_failed_bind_and_recovers() {
use crate::core::supervision::task::{
spawn_supervised, Backoff, HealthRegistry, RestartPolicy, TaskSpec,
};
let squatter = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
let port = squatter.local_addr().unwrap().port();
let registry = Arc::new(HealthRegistry::new());
let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
let spec = TaskSpec::new("boundary", RestartPolicy::Always).with_backoff(Backoff::new(
Duration::from_millis(20),
Duration::from_millis(60),
));
let pre_bound = Arc::new(tokio::sync::Mutex::new(None));
let task_shutdown_rx = shutdown_rx.clone();
let handle = spawn_supervised(®istry, spec, shutdown_rx, move || {
let state = Arc::new(BoundaryState::new(default_upstream(), port, 4, &[]));
serve_attempt(pre_bound.clone(), state, task_shutdown_rx.clone())
});
tokio::time::sleep(Duration::from_millis(250)).await;
let health = registry.tasks()[0].clone();
assert!(
health.restarts() >= 2,
"an occupied port must be retried, saw {} restarts",
health.restarts()
);
let recorded = health.last_error().unwrap_or_default();
assert!(
recorded.contains(&port.to_string()),
"the bind failure must be recorded on the health entry, got {recorded:?}"
);
assert!(
registry.is_degraded(),
"a boundary that cannot bind must read as degraded, not ok"
);
drop(squatter);
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(2))
.build()
.expect("client");
let url = format!("http://127.0.0.1:{port}/admin/boundary/status");
let deadline = std::time::Instant::now() + Duration::from_secs(10);
let mut served = false;
while std::time::Instant::now() < deadline {
if let Ok(r) = client.get(&url).send().await {
if r.status().is_success() {
served = true;
break;
}
}
tokio::time::sleep(Duration::from_millis(25)).await;
}
assert!(
served,
"the boundary must bind and serve once the port is released"
);
shutdown_tx.send(true).expect("shutdown send");
let _ = tokio::time::timeout(Duration::from_secs(5), handle).await;
}
#[tokio::test]
async fn first_attempt_serves_the_prebound_listener_without_rebinding() {
let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
let port = listener.local_addr().unwrap().port();
let pre_bound = Arc::new(tokio::sync::Mutex::new(Some(listener)));
let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
let state = Arc::new(BoundaryState::new(default_upstream(), port, 4, &[]));
let serve = tokio::spawn(serve_attempt(pre_bound.clone(), state, shutdown_rx));
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(2))
.build()
.expect("client");
let url = format!("http://127.0.0.1:{port}/admin/boundary/status");
let deadline = std::time::Instant::now() + Duration::from_secs(5);
let mut served = false;
while std::time::Instant::now() < deadline {
if let Ok(r) = client.get(&url).send().await {
if r.status().is_success() {
served = true;
break;
}
}
tokio::time::sleep(Duration::from_millis(25)).await;
}
assert!(served, "the pre-bound listener must be served as-is");
assert!(
pre_bound.lock().await.is_none(),
"the pre-bound listener is consumed by the first attempt only"
);
shutdown_tx.send(true).expect("shutdown send");
let _ = tokio::time::timeout(Duration::from_secs(5), serve).await;
}
#[tokio::test]
async fn bind_pinned_is_loud_when_occupied() {
let probe = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
let port = probe.local_addr().unwrap().port();
drop(probe);
let held = bind_pinned(port).await.expect("first bind succeeds");
let occupied = bind_pinned(port).await;
assert!(occupied.is_err(), "second bind of a held port must fail");
assert_eq!(
occupied.unwrap_err().code,
crate::error::ERR_BOUNDARY_PORT_IN_USE
);
drop(held);
}
}