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/bin/git_remote_nostr/main.rs | 4 +++- src/lib/git/utils.rs | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/bin/git_remote_nostr/main.rs b/src/bin/git_remote_nostr/main.rs index 29731e2..cf47a30 100644 --- a/src/bin/git_remote_nostr/main.rs +++ b/src/bin/git_remote_nostr/main.rs @@ -16,7 +16,7 @@ use client::{Connect, consolidate_fetch_reports, get_repo_ref_from_cache}; use git::{RepoActions, nostr_url::NostrUrlDecoded}; use ngit::{ client::{self, Params}, - git, + git::{self, utils::set_git_timeout}, login::existing::load_existing_login, }; use nostr::nips::nip19::Nip19Coordinate; @@ -62,6 +62,8 @@ async fn main() -> Result<()> { repo_ref.set_nostr_git_url(decoded_nostr_url.clone()); + let _ = set_git_timeout(); + let stdin = io::stdin(); let mut line = String::new(); 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