From 5f0038cd6dc7f5dfc4da76725ac65f92bfc8f973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 23 Oct 2026 06:46:24 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20perf:=20stop=20the=20slow=20e2e=20t?= =?UTF-8?q?ests=20waiting=20on=20delays=20they=20do=20not=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Where the time went (isolated runs, before -> after): - ssh_e2e clone_push_clone_over_ssh: 16.9 s -> 0.5 s Every SSH connection's first `none` auth probe was rejected, and russh answered each rejection after a fixed 1 s delay. The delay is now the `ssh_auth_rejection_secs` setting (default 1, production unchanged); the e2e harness and the SSH test set 0. - mirrors_e2e pull_and_push_mirrors_sync_over_smart_http: 12.9 s -> 3.5 s A push mirror waited the hard-coded 10 s push debounce. The debounce is now `[mirrors] push_debounce_secs` (default 10, production unchanged); the test sets 0. - git_e2e git_end_to_end: 9.5 s -> 2.9 s Its SSH operations paid the same 1 s auth delay as above. - ci::api::tests::runner_binary_streams_the_running_binary_with_its_sha: 32 s -> 9.2 s (shared run). The binary body was streamed in 4 KiB frames (ReaderStream default), about 350k frames for a 1.4 GB debug binary. Now 64 KiB frames. The served bytes and assertions are unchanged. Also: debug builds now optimize argon2, blake2, ml-kem, keccak, curve25519-dalek, ed25519-dalek and chacha20 (same pattern as sha2), so password hashing and SSH crypto are fast in tests. Release is unaffected. --- Cargo.toml | 19 ++++++++++++- config/forge.example.toml | 7 +++++ crates/forge-core/src/config.rs | 27 +++++++++++++++++++ crates/forge-ssh/src/server.rs | 6 ++--- crates/forge-ssh/tests/ssh_e2e.rs | 3 +++ crates/forge-web/src/boot.rs | 2 ++ crates/forge-web/src/mirrors/mod.rs | 8 +++--- .../forge-web/src/pages/settings_security.rs | 2 ++ crates/forge-web/src/self_update.rs | 5 +++- crates/forge-web/src/test_support.rs | 2 ++ crates/forge-web/tests/common/mod.rs | 3 ++- crates/forge-web/tests/mirrors_e2e.rs | 6 ++--- 12 files changed, 76 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4a93e2c..8f2aa98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -127,11 +127,28 @@ opt-level = 3 opt-level = 3 [profile.dev.package.turso_parser] opt-level = 3 +# Password hashing (argon2id, default cost) and the SSH key exchange and ciphers run in every e2e +# test's login and connection. Unoptimized they take hundreds of ms each. Release is unaffected. [profile.dev.package.argon2] -# password hashing runs unoptimized in tests and dominates the login tests. opt-level = 3 [profile.dev.package.blake2] opt-level = 3 +[profile.dev.package.ml-kem] +opt-level = 3 +[profile.dev.package.module-lattice] +opt-level = 3 +[profile.dev.package.keccak] +opt-level = 3 +[profile.dev.package.curve25519-dalek] +opt-level = 3 +[profile.dev.package.ed25519-dalek] +opt-level = 3 +[profile.dev.package.chacha20poly1305] +opt-level = 3 +[profile.dev.package.chacha20] +opt-level = 3 +[profile.dev.package.poly1305] +opt-level = 3 [profile.release] lto = true diff --git a/config/forge.example.toml b/config/forge.example.toml index c8fef1e..4952d5f 100644 --- a/config/forge.example.toml +++ b/config/forge.example.toml @@ -33,6 +33,13 @@ trusted_proxies = [] # Exact hostnames or CIDR networks that may be reached even if they resolve to internal addresses. # allowed_hosts = ["hooks.internal.example", "10.20.0.0/16"] +# Mirror sync timing. A push mirror runs this many seconds after the last ref update. +# [mirrors] +# push_debounce_secs = 10 + +# Seconds a failed SSH login waits before it is refused (brute-force damping). Default 1. +# ssh_auth_rejection_secs = 1 + # Scheduled backups (see docs/backup.md). Empty dir disables the scheduler. # [backup] # Directory that receives the backup sets. Ship it off-site with restic or rclone. diff --git a/crates/forge-core/src/config.rs b/crates/forge-core/src/config.rs index 069c3e7..ecd1630 100644 --- a/crates/forge-core/src/config.rs +++ b/crates/forge-core/src/config.rs @@ -35,6 +35,10 @@ fn deserialize_trusted_proxies<'de, D: Deserializer<'de>>( .collect() } +fn default_ssh_auth_rejection_secs() -> u64 { + 1 +} + fn default_user_content_listen() -> SocketAddr { "127.0.0.1:7661" .parse() @@ -67,6 +71,10 @@ pub struct Config { pub ssh_listen: SocketAddr, pub ssh_public_host: String, pub ssh_public_port: u16, + /// Seconds a failed SSH authentication waits before it is answered (brute-force damping). + /// Every SSH connection's first `none` probe fails, so this is paid per connection. + #[serde(default = "default_ssh_auth_rejection_secs")] + pub ssh_auth_rejection_secs: u64, pub registration: Registration, /// Peers whose `X-Forwarded-For` header is trusted, such as the reverse proxy in front of forge. /// Entries are addresses or CIDR networks. Empty by default, so the TCP peer is the client. @@ -111,6 +119,25 @@ pub struct Config { /// Alerts to site admins, from the `[alerts]` section. #[serde(default)] pub alerts: AlertsConfig, + /// Mirror sync timing, from the `[mirrors]` section. + #[serde(default)] + pub mirrors: MirrorsConfig, +} + +/// Mirror sync timing. The default is what production runs with; the e2e tests shorten it. +#[derive(Debug, Clone, Deserialize)] +#[serde(deny_unknown_fields, default)] +pub struct MirrorsConfig { + /// Seconds a push mirror waits after the last ref update before it is sent. + pub push_debounce_secs: u64, +} + +impl Default for MirrorsConfig { + fn default() -> Self { + MirrorsConfig { + push_debounce_secs: 10, + } + } } /// Admin alerts: what the forge tells the site admins about before something breaks. diff --git a/crates/forge-ssh/src/server.rs b/crates/forge-ssh/src/server.rs index 767d3f3..c964d77 100644 --- a/crates/forge-ssh/src/server.rs +++ b/crates/forge-ssh/src/server.rs @@ -33,8 +33,6 @@ const PUMP_CHUNK: usize = 32 * 1024; /// Client input chunks buffered in front of the child's stdin. This bounds memory and /// propagates backpressure to the SSH read loop. const STDIN_QUEUE: usize = 32; -/// Constant delay applied to failed authentication attempts. -const AUTH_REJECTION_TIME: Duration = Duration::from_secs(1); /// Failed publickey attempts before the connection is dropped. const MAX_AUTH_ATTEMPTS: usize = 6; @@ -44,6 +42,8 @@ pub struct SshConfig { pub host_key_path: PathBuf, pub auth_timeout: Duration, pub inactivity_timeout: Duration, + /// Delay before a failed authentication attempt is answered (forge-web: `ssh_auth_rejection_secs`). + pub auth_rejection_time: Duration, } /// Runs the SSH server until it fails. Creates the ed25519 host key on first boot. @@ -52,7 +52,7 @@ pub async fn run(config: SshConfig, backend: Arc) -> Result<() let russh_config = Arc::new(Config { keys: vec![host_key], methods: publickey_only(), - auth_rejection_time: AUTH_REJECTION_TIME, + auth_rejection_time: config.auth_rejection_time, max_auth_attempts: MAX_AUTH_ATTEMPTS, inactivity_timeout: Some(config.inactivity_timeout), preferred: preferred_algorithms(), diff --git a/crates/forge-ssh/tests/ssh_e2e.rs b/crates/forge-ssh/tests/ssh_e2e.rs index e50b67b..5e5780b 100644 --- a/crates/forge-ssh/tests/ssh_e2e.rs +++ b/crates/forge-ssh/tests/ssh_e2e.rs @@ -193,6 +193,9 @@ async fn clone_push_clone_over_ssh() { host_key_path: host_key.clone(), auth_timeout: Duration::from_secs(10), inactivity_timeout: Duration::from_secs(60), + // Production rejects a failed auth after 1s. OpenSSH's `none` probe fails on every + // connection, so the test would wait that second for each of its ~20 connections. + auth_rejection_time: Duration::ZERO, }; tokio::spawn(forge_ssh::run(config, std::sync::Arc::new(backend))); wait_for_port(port).await; diff --git a/crates/forge-web/src/boot.rs b/crates/forge-web/src/boot.rs index f6fc13e..eacbeba 100644 --- a/crates/forge-web/src/boot.rs +++ b/crates/forge-web/src/boot.rs @@ -41,6 +41,7 @@ pub async fn serve( let listen = config.listen; let user_content_listen = config.user_content_listen; let ssh_listen = config.ssh_listen; + let ssh_auth_rejection_time = Duration::from_secs(config.ssh_auth_rejection_secs); std::fs::create_dir_all(&config.data_dir)?; // The data dir holds the instance key and the internal socket. Keep it private to this user. @@ -211,6 +212,7 @@ pub async fn serve( host_key_path: data_dir.join("ssh_host_ed25519_key"), auth_timeout: Duration::from_secs(30), inactivity_timeout: Duration::from_secs(10 * 60), + auth_rejection_time: ssh_auth_rejection_time, }; let ssh_backend = Arc::new(SshBackend { state: state.clone(), diff --git a/crates/forge-web/src/mirrors/mod.rs b/crates/forge-web/src/mirrors/mod.rs index f5d52dc..2200df5 100644 --- a/crates/forge-web/src/mirrors/mod.rs +++ b/crates/forge-web/src/mirrors/mod.rs @@ -27,8 +27,6 @@ use crate::post_update::after_ref_update; use crate::state::AppState; use crate::webhooks::client::{WebhookPolicy, is_forbidden_ip, validate_url}; -/// How long a push waits after the last ref update before it is sent. -pub const PUSH_DEBOUNCE: Duration = Duration::from_secs(10); /// Hard limit on one sync run. The git process is killed when it passes. pub const RUN_TIMEOUT: Duration = Duration::from_secs(10 * 60); /// The all-zero object id git uses for a ref that does not exist on one side. @@ -36,10 +34,10 @@ pub const ZERO_OID: &str = "0000000000000000000000000000000000000000"; /// Next run for a mirror whose interval is `0` (manual only). const FAR_FUTURE: &str = "9999-12-31T23:59:59.999999999Z"; -/// Queues every enabled push mirror of a repo for a sync `PUSH_DEBOUNCE` from now, and wakes the -/// worker. Called after a branch or tag update. +/// Queues every enabled push mirror of a repo for a sync after the `[mirrors]` push debounce, and +/// wakes the worker. Called after a branch or tag update. pub async fn enqueue_push_mirrors(state: &AppState, repo_id: &str) -> forge_core::Result<()> { - let due = stamp_after(PUSH_DEBOUNCE); + let due = stamp_after(Duration::from_secs(state.config.mirrors.push_debounce_secs)); let mut queued = false; for mirror in state.store.list_mirrors(repo_id).await? { if mirror.direction == MirrorDirection::Push && mirror.enabled { diff --git a/crates/forge-web/src/pages/settings_security.rs b/crates/forge-web/src/pages/settings_security.rs index 0e3653f..397ab2a 100644 --- a/crates/forge-web/src/pages/settings_security.rs +++ b/crates/forge-web/src/pages/settings_security.rs @@ -1076,6 +1076,8 @@ mod tests { self_update: Default::default(), banner: Default::default(), alerts: Default::default(), + mirrors: Default::default(), + ssh_auth_rejection_secs: 1, }; let config = Arc::new(config); let state = AppState { diff --git a/crates/forge-web/src/self_update.rs b/crates/forge-web/src/self_update.rs index 2b0b95a..7c359ea 100644 --- a/crates/forge-web/src/self_update.rs +++ b/crates/forge-web/src/self_update.rs @@ -370,7 +370,10 @@ pub async fn binary_response() -> Result { .await .map_err(|e| e.to_string())?; let len = file.metadata().await.map_err(|e| e.to_string())?.len(); - let frames = ReaderStream::new(file).map(|chunk| chunk.map(http_body::Frame::data)); + // 64 KiB frames, not ReaderStream's 4 KiB default: the binary is ~1 GB, and 4 KiB frames mean + // ~250k body frames to push through hyper for every download. + let frames = + ReaderStream::with_capacity(file, 64 * 1024).map(|chunk| chunk.map(http_body::Frame::data)); let mut res = Response::new(Body::new(StreamBody::new(frames))); let headers = res.headers_mut(); headers.insert( diff --git a/crates/forge-web/src/test_support.rs b/crates/forge-web/src/test_support.rs index a0ebf23..cb3fc1e 100644 --- a/crates/forge-web/src/test_support.rs +++ b/crates/forge-web/src/test_support.rs @@ -60,6 +60,8 @@ pub fn test_config(dir: &Path) -> Config { self_update: Default::default(), banner: Default::default(), alerts: Default::default(), + mirrors: Default::default(), + ssh_auth_rejection_secs: 1, } } diff --git a/crates/forge-web/tests/common/mod.rs b/crates/forge-web/tests/common/mod.rs index 04c262b..6052649 100644 --- a/crates/forge-web/tests/common/mod.rs +++ b/crates/forge-web/tests/common/mod.rs @@ -97,7 +97,8 @@ pub fn write_config_with(dir: &Path, ports: Ports, extra: &str) -> PathBuf { ssh_listen = \"127.0.0.1:{ssh_port}\"\n\ ssh_public_host = \"127.0.0.1\"\n\ ssh_public_port = {ssh_port}\n\ - registration = \"closed\"\n{extra}" + registration = \"closed\"\n\ + ssh_auth_rejection_secs = 0\n{extra}" ); let path = dir.join("forge.toml"); fs::write(&path, text).expect("write config"); diff --git a/crates/forge-web/tests/mirrors_e2e.rs b/crates/forge-web/tests/mirrors_e2e.rs index 73eda39..d09dd25 100644 --- a/crates/forge-web/tests/mirrors_e2e.rs +++ b/crates/forge-web/tests/mirrors_e2e.rs @@ -20,10 +20,10 @@ use std::time::{Duration, Instant}; use reqwest::header::{LOCATION, SET_COOKIE}; const TAHINLI_PW: &str = "tahinli-password-123"; -const WEBHOOK_TOML: &str = "\n[webhooks]\nallow_http = true\nallowed_hosts = [\"127.0.0.1\"]\n"; +const WEBHOOK_TOML: &str = "\n[webhooks]\nallow_http = true\nallowed_hosts = [\"127.0.0.1\"]\n\n[mirrors]\npush_debounce_secs = 0\n"; const SYNC_TIMEOUT: Duration = Duration::from_secs(30); -/// Push mirrors run after the 10s debounce, but the worker only re-checks on its 60s poll (or a -/// wake-up, which fires before the debounce expires). Allow poll plus debounce plus slack. +/// Push mirrors run after the push debounce, which this test sets to 0 (production: 10s). The +/// worker wakes on the push itself, so this only needs room for the git sync. const PUSH_TIMEOUT: Duration = Duration::from_secs(90); struct Resp {