diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2025-07-15 11:05:44 +0100 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2025-07-15 11:05:44 +0100 |
| commit | 8d42d8c3e8e65ff1892effa2d1058d9f2422ce2b (patch) | |
| tree | d10dd24a781a68095566d9f26da0ffca86b85f51 /src | |
| parent | 78f28284dac5d6521f0308f49c16646b17bad483 (diff) | |
feat: add git timeout
to improve reliability
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/git_remote_nostr/main.rs | 4 | ||||
| -rw-r--r-- | src/lib/git/utils.rs | 23 |
2 files changed, 26 insertions, 1 deletions
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}; | |||
| 16 | use git::{RepoActions, nostr_url::NostrUrlDecoded}; | 16 | use git::{RepoActions, nostr_url::NostrUrlDecoded}; |
| 17 | use ngit::{ | 17 | use ngit::{ |
| 18 | client::{self, Params}, | 18 | client::{self, Params}, |
| 19 | git, | 19 | git::{self, utils::set_git_timeout}, |
| 20 | login::existing::load_existing_login, | 20 | login::existing::load_existing_login, |
| 21 | }; | 21 | }; |
| 22 | use nostr::nips::nip19::Nip19Coordinate; | 22 | use nostr::nips::nip19::Nip19Coordinate; |
| @@ -62,6 +62,8 @@ async fn main() -> Result<()> { | |||
| 62 | 62 | ||
| 63 | repo_ref.set_nostr_git_url(decoded_nostr_url.clone()); | 63 | repo_ref.set_nostr_git_url(decoded_nostr_url.clone()); |
| 64 | 64 | ||
| 65 | let _ = set_git_timeout(); | ||
| 66 | |||
| 65 | let stdin = io::stdin(); | 67 | let stdin = io::stdin(); |
| 66 | let mut line = String::new(); | 68 | let mut line = String::new(); |
| 67 | 69 | ||
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 @@ | |||
| 1 | use std::path::Path; | 1 | use std::path::Path; |
| 2 | 2 | ||
| 3 | use anyhow::{Context, Result}; | ||
| 3 | use directories::UserDirs; | 4 | use directories::UserDirs; |
| 5 | use git2::opts::{set_server_connect_timeout_in_milliseconds, set_server_timeout_in_milliseconds}; | ||
| 4 | 6 | ||
| 5 | pub fn check_ssh_keys() -> bool { | 7 | pub fn check_ssh_keys() -> bool { |
| 6 | // Get the user's home directory using the directories crate | 8 | // Get the user's home directory using the directories crate |
| @@ -23,3 +25,24 @@ pub fn check_ssh_keys() -> bool { | |||
| 23 | } | 25 | } |
| 24 | false // No keys found | 26 | false // No keys found |
| 25 | } | 27 | } |
| 28 | |||
| 29 | pub fn set_git_timeout() -> Result<()> { | ||
| 30 | unsafe { | ||
| 31 | // Set a 3 000 ms timeout for establishing the TCP connection (default: 60 000 | ||
| 32 | // ms). | ||
| 33 | set_server_connect_timeout_in_milliseconds(3_000) | ||
| 34 | .context("failed to set libgit2 connect timeout")?; | ||
| 35 | |||
| 36 | // The server timeout applies per socket send()/recv() call rather than | ||
| 37 | // to the entire fetch or push. libgit2 transfers data in ~16 KiB chunks, | ||
| 38 | // so each chunk’s transfer is subject to this timeout instead of the | ||
| 39 | // overall command. | ||
| 40 | // | ||
| 41 | // We set it to 15 000 ms (instead of the 300 000 ms default) to quickly | ||
| 42 | // abort any stalled ~16 KiB chunk transfer—enabling fast failover across | ||
| 43 | // redundant Git servers—while still accommodating transient hiccups. | ||
| 44 | set_server_timeout_in_milliseconds(15_000).context("failed to set libgit2 I/O timeout")?; | ||
| 45 | |||
| 46 | Ok(()) | ||
| 47 | } | ||
| 48 | } | ||