This commit is contained in:
Mateusz Gruszczyński committed 2026-10-04 11:15:58 +02:00
1 parent 87e571c170
commit fa7abc9e53
308 files changed
+49746 -50237

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
use crate::{
config::{self, Snapshot, Update, ValidationPolicy},
db::Store,
logging::Logs,
metrics::Metrics,
};
use arc_swap::ArcSwap;
use axum::{
http::StatusCode,
response::{IntoResponse, Response},
Json,
};
use serde_json::json;
use sha2::{Digest, Sha256};
use std::{
collections::HashMap,
sync::Arc,
time::{Duration, Instant},
};
use tokio::sync::{Mutex, Semaphore};
pub struct App {
pub basic_workers: Arc<Semaphore>,
pub basic_limits: crate::basic_auth::Limits,
pub active_prometheus: crate::prometheus::Settings,
pub active_administration: crate::settings::Administration,
pub active_administration_tls_policy: crate::tls::ProtocolPolicy,
pub active_administration_certificate_files: Option<(String, String)>,
pub max_inflight: usize,
pub config: ArcSwap<Snapshot>,
pub store: Store,
pub policy: ValidationPolicy,
pub active_listeners: Vec<crate::vhost::Listener>,
pub active_streams: Vec<crate::stream::Host>,
pub stream_metrics: HashMap<String, Arc<crate::stream::Metrics>>,
pub startup_listeners: (std::net::SocketAddr, Option<std::net::SocketAddr>),
pub startup_locks: crate::settings::StartupLocks,
pub auth: Arc<crate::auth::Auth>,
pub viewer_slots: Arc<Semaphore>,
pub certificate_slots: Arc<Semaphore>,
pub metrics: Arc<Metrics>,
pub transform_metrics: Arc<crate::transform::Metrics>,
pub traffic: Arc<crate::traffic::Analytics>,
pub cache: Arc<crate::cache::Cache>,
pub geoip: Arc<crate::geoip::Database>,
pub rate_limiter: Arc<crate::rate_limit::Limiter>,
pub acme: Arc<crate::acme::Manager>,
pub diagnostics: Arc<crate::diagnostics::Diagnostics>,
pub docker: Arc<crate::docker::Manager>,
pub logs: Arc<Logs>,
pub software_update: Arc<crate::update_check::SharedStatus>,
pub shutdown: tokio_util::sync::CancellationToken,
pub tunnels: tokio_util::task::TaskTracker,
pub tunnel_registry: Arc<crate::proxy::websocket::TunnelRegistry>,
pub websocket_idle_timeout: Duration,
pub inflight: Arc<Semaphore>,
pub started: Instant,
pub header_timeout: Duration,
pub body_idle_timeout: Duration,
update_lock: Mutex<()>,
update_slots: Arc<Semaphore>,
}
#[derive(Debug)]
pub struct ApiError(pub StatusCode, pub String);
impl IntoResponse for ApiError {
fn into_response(self) -> Response {
let mut response = (self.0, Json(json!({"error": self.1}))).into_response();
if self.0 == StatusCode::TOO_MANY_REQUESTS {
response
.headers_mut()
.insert("retry-after", http::HeaderValue::from_static("60"));
}
response
}
}
#[derive(Debug)]
enum CacheInvalidation {
None,
All(u64),
}
/// Stable cache compatibility identity. Unlike the configuration revision, this
/// changes only when a setting can alter an origin request or cached response.
/// Keeping it stable across logging/TLS/admin saves lets the persistent cold
/// tier survive a restart after unrelated control-plane edits.
fn cache_revision(config: &config::Config) -> u64 {
// Preset fields are UI intent metadata. Persist them, but do not rotate the
// cache compatibility identity (and purge HOT/COLD) when only the label
// changes while the effective cache limits stay identical.
let mut cache = config.cache.clone();
cache.memory_preset = None;
cache.cold_preset = None;
// Resource-only transform controls decide how much optimization work may
// happen concurrently, but do not change the transformed representation.
// Keep those out of the cache identity while including representation and
// eligibility controls such as enabled kinds, extensions and max body.
let mut transform = config.transform.clone();
transform.preset = None;
transform.memory_budget_bytes = 0;
transform.max_inflight = 0;
let encoded = serde_json::to_vec(&(
&cache,
&transform,
&config.headers,
&config.forwarding,
&config.vhosts,
))
.expect("serializable cache compatibility view");
let digest = Sha256::digest(encoded);
u64::from_be_bytes(digest[..8].try_into().expect("sha256 prefix"))
}
fn cache_invalidation(previous: &config::Config, next: &config::Config) -> CacheInvalidation {
let before = cache_revision(previous);
let after = cache_revision(next);
if before == after {
CacheInvalidation::None
} else {
CacheInvalidation::All(after)
}
}