This commit is contained in:
Mateusz Gruszczyński committed 2026-11-15 12:21:12 +01:00
1 parent 5e57e48c6a
commit df0e39b899
25 files changed
+319 -97

No files matched your search

+34 -14
View File
@@ -4,6 +4,17 @@ pub(crate) fn compile_backend_with_timeout(
bindings: &[SocketAddr],
keepalive: &crate::balance::Keepalive,
connect_timeout: std::time::Duration,
) -> Result<CompiledBackend> {
compile_backend_reusing(backend, policy, bindings, keepalive, connect_timeout, None)
}
pub(crate) fn compile_backend_reusing(
backend: &Backend,
policy: &ValidationPolicy,
bindings: &[SocketAddr],
keepalive: &crate::balance::Keepalive,
connect_timeout: std::time::Duration,
reuse: Option<&CompiledBackend>,
) -> Result<CompiledBackend> {
ensure!(valid_id(&backend.id), "invalid backend id");
ensure!(backend.url.len() <= 512, "backend URL too long");
@@ -70,23 +81,32 @@ pub(crate) fn compile_backend_with_timeout(
.as_deref()
.map(|name| format!("{name}:{port}"))
.unwrap_or_else(|| authority.clone());
let client = crate::upstream::client_with_timeout(
backend,
address_policy.clone(),
false,
keepalive,
connect_timeout,
)?;
let build_client = |previous: Option<&crate::upstream::ProxyClient>, websocket: bool| {
let reusable = previous.filter(|client| {
client.built_with(
backend,
&address_policy,
websocket,
keepalive,
connect_timeout,
)
});
match reusable {
Some(client) => Ok(client.clone()),
None => crate::upstream::client_with_timeout(
backend,
address_policy.clone(),
websocket,
keepalive,
connect_timeout,
),
}
};
let client = build_client(reuse.map(|item| &item.client), false)?;
let websocket_client = if backend.protocol == UpstreamProtocol::Http1 {
client.clone()
} else {
crate::upstream::client_with_timeout(
backend,
address_policy.clone(),
true,
keepalive,
connect_timeout,
)?
build_client(reuse.map(|item| &item.websocket_client), true)?
};
let request_origin = format!("{}://{request_authority}", url.scheme());
let request_origin_uri: http::Uri = request_origin.parse()?;
+1 -1
View File
@@ -353,7 +353,7 @@ pub fn compile_with_active(
&web_services,
previous,
&effective_tls,
&tls,
(&tls, &tls_http1),
)?;
let quic_tls = tls
.as_ref()
+45 -6
View File
@@ -7,8 +7,18 @@ fn compile_routing(
web_services: &HashMap<String, Arc<crate::web_server::CompiledService>>,
previous: Option<&Snapshot>,
effective_tls: &crate::tls::Settings,
tls: &Option<Arc<rustls::ServerConfig>>,
(tls, tls_http1): (
&Option<Arc<rustls::ServerConfig>>,
&Option<Arc<rustls::ServerConfig>>,
),
) -> Result<RoutingBuild> {
let mut tls_variants: Vec<(crate::tls::ProtocolPolicy, bool, Arc<rustls::ServerConfig>)> =
Vec::new();
for (config, http2) in [(tls, true), (tls_http1, false)] {
if let Some(config) = config {
tls_variants.push((effective_tls.policy.clone(), http2, config.clone()));
}
}
let mut routes: HashMap<String, Vec<Arc<CompiledRoute>>> = HashMap::new();
let mut fallbacks: HashMap<String, Vec<Arc<CompiledRoute>>> = HashMap::new();
let mut frontends: HashMap<String, Vec<Arc<CompiledFrontend>>> = HashMap::new();
@@ -104,31 +114,43 @@ fn compile_routing(
&& route.source.is_reverse_proxy()
});
let mut compiled_members = Vec::new();
let previous_pool = previous.and_then(|s| s.pools.iter().find(|p| p.id == host.id));
for definition in host.backends.iter().filter(|_| needs_proxy_pool) {
let connect_timeout = host
.proxy_timeouts
.connect_timeout()
.unwrap_or(std::time::Duration::from_secs(10));
let mut backend = compile_backend_with_timeout(
let member_id = format!("{}/{}", host.id, definition.id);
let previous_backend = previous_pool.and_then(|pool| {
pool.members
.iter()
.find(|member| member.backend.id == member_id)
.map(|member| member.backend.clone())
});
let mut backend = compile_backend_reusing(
&definition.backend(),
desired_policy,
guarded_bindings,
&host.load_balancing.keepalive,
connect_timeout,
previous_backend.as_deref(),
)
.with_context(|| format!("vhost {} backend {}", host.id, definition.id))?;
backend.id = format!("{}/{}", host.id, definition.id);
backend.id = member_id;
let health = host.load_balancing.health_for(definition);
if health.mode != crate::balance::HealthMode::Off {
if let Some(probe) =
health_probe_backend(&definition.backend(), health.port)?
{
let mut probe = compile_backend_with_timeout(
let mut probe = compile_backend_reusing(
&probe,
desired_policy,
guarded_bindings,
&host.load_balancing.keepalive,
connect_timeout,
previous_backend
.as_ref()
.and_then(|item| item.health_probe.as_deref()),
)
.with_context(|| {
format!("vhost {} backend {} health check port", host.id, definition.id)
@@ -193,11 +215,11 @@ fn compile_routing(
}
let frontend_tls = tls
.as_ref()
.map(|base| crate::tls::variant(base, &tls_policy, true))
.map(|base| shared_tls_variant(&mut tls_variants, base, &tls_policy, true))
.transpose()?;
let frontend_tls_http1 = tls
.as_ref()
.map(|base| crate::tls::variant(base, &tls_policy, false))
.map(|base| shared_tls_variant(&mut tls_variants, base, &tls_policy, false))
.transpose()?;
let profile_policies: Vec<crate::acl::ProfileAccess> =
crate::acl::profile_ids(host.acl_profile_id.as_deref(), &host.acl_profile_ids)
@@ -498,3 +520,20 @@ fn compile_routing(
vhost_metrics,
})
}
fn shared_tls_variant(
variants: &mut Vec<(crate::tls::ProtocolPolicy, bool, Arc<rustls::ServerConfig>)>,
base: &Arc<rustls::ServerConfig>,
policy: &crate::tls::ProtocolPolicy,
http2: bool,
) -> Result<Arc<rustls::ServerConfig>> {
if let Some((_, _, config)) = variants
.iter()
.find(|(known, known_http2, _)| *known_http2 == http2 && known == policy)
{
return Ok(config.clone());
}
let config = crate::tls::variant(base, policy, http2)?;
variants.push((policy.clone(), http2, config.clone()));
Ok(config)
}