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/lib/git/utils.rs | |
| parent | 78f28284dac5d6521f0308f49c16646b17bad483 (diff) | |
feat: add git timeout
to improve reliability
Diffstat (limited to 'src/lib/git/utils.rs')
| -rw-r--r-- | src/lib/git/utils.rs | 23 |
1 files changed, 23 insertions, 0 deletions
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 | } | ||