From 8d42d8c3e8e65ff1892effa2d1058d9f2422ce2b Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 15 Jul 2025 11:05:44 +0100 Subject: feat: add git timeout to improve reliability --- src/lib/git/utils.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/lib/git/utils.rs') diff --git a/src/lib/git/utils.rs b/src/lib/git/utils.rs index 4e8f153..c5f8850 100644 --- a/src/lib/git/utils.rs +++ b/src/lib/git/utils.rs @@ -1,6 +1,8 @@ use std::path::Path; +use anyhow::{Context, Result}; use directories::UserDirs; +use git2::opts::{set_server_connect_timeout_in_milliseconds, set_server_timeout_in_milliseconds}; pub fn check_ssh_keys() -> bool { // Get the user's home directory using the directories crate @@ -23,3 +25,24 @@ pub fn check_ssh_keys() -> bool { } false // No keys found } + +pub fn set_git_timeout() -> Result<()> { + unsafe { + // Set a 3 000 ms timeout for establishing the TCP connection (default: 60 000 + // ms). + set_server_connect_timeout_in_milliseconds(3_000) + .context("failed to set libgit2 connect timeout")?; + + // The server timeout applies per socket send()/recv() call rather than + // to the entire fetch or push. libgit2 transfers data in ~16 KiB chunks, + // so each chunk’s transfer is subject to this timeout instead of the + // overall command. + // + // We set it to 15 000 ms (instead of the 300 000 ms default) to quickly + // abort any stalled ~16 KiB chunk transfer—enabling fast failover across + // redundant Git servers—while still accommodating transient hiccups. + set_server_timeout_in_milliseconds(15_000).context("failed to set libgit2 I/O timeout")?; + + Ok(()) + } +} -- cgit v1.2.3