Skip to content

Commit

Permalink
Add global opts related to server timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
mzottola committed May 17, 2024
1 parent c3454fe commit 006324a
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
7 changes: 7 additions & 0 deletions libgit2-sys/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ git_enum! {
GIT_EINDEXDIRTY = -34,
GIT_EAPPLYFAIL = -35,
GIT_EOWNER = -36,
GIT_TIMEOUT = -37,
}
}

Expand Down Expand Up @@ -1947,6 +1948,12 @@ git_enum! {
GIT_OPT_SET_EXTENSIONS,
GIT_OPT_GET_OWNER_VALIDATION,
GIT_OPT_SET_OWNER_VALIDATION,
GIT_OPT_GET_HOMEDIR,
GIT_OPT_SET_HOMEDIR,
GIT_OPT_SET_SERVER_CONNECT_TIMEOUT,
GIT_OPT_GET_SERVER_CONNECT_TIMEOUT,
GIT_OPT_SET_SERVER_TIMEOUT,
GIT_OPT_GET_SERVER_TIMEOUT,
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl Error {
raw::GIT_EINDEXDIRTY => super::ErrorCode::IndexDirty,
raw::GIT_EAPPLYFAIL => super::ErrorCode::ApplyFail,
raw::GIT_EOWNER => super::ErrorCode::Owner,
raw::GIT_TIMEOUT => super::ErrorCode::Timeout,
_ => super::ErrorCode::GenericError,
}
}
Expand Down Expand Up @@ -165,6 +166,7 @@ impl Error {
ErrorCode::IndexDirty => raw::GIT_EINDEXDIRTY,
ErrorCode::ApplyFail => raw::GIT_EAPPLYFAIL,
ErrorCode::Owner => raw::GIT_EOWNER,
ErrorCode::Timeout => raw::GIT_TIMEOUT,
};
}

Expand Down Expand Up @@ -296,6 +298,7 @@ impl Error {
GIT_EINDEXDIRTY,
GIT_EAPPLYFAIL,
GIT_EOWNER,
GIT_TIMEOUT,
)
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ pub enum ErrorCode {
ApplyFail,
/// The object is not owned by the current user
Owner,
/// Timeout
Timeout,
}

/// An enumeration of possible categories of things that can have
Expand Down
93 changes: 93 additions & 0 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,83 @@ pub unsafe fn set_mwindow_file_limit(limit: libc::size_t) -> Result<(), Error> {
Ok(())
}

/// Get server connect timeout in milliseconds
///
/// # Safety
/// This function is modifying a C global without synchronization, so it is not
/// thread safe, and should only be called before any thread is spawned.
pub unsafe fn get_server_connect_timeout_in_milliseconds() -> Result<libc::c_int, Error> {
crate::init();

let mut server_connect_timeout = 0;

try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_GET_SERVER_CONNECT_TIMEOUT as libc::c_int,
&mut server_connect_timeout
));
println!("result = {}", server_connect_timeout);

Ok(server_connect_timeout)
}

/// Set server connect timeout in milliseconds
///
/// # Safety
/// This function is modifying a C global without synchronization, so it is not
/// thread safe, and should only be called before any thread is spawned.
pub unsafe fn set_server_connect_timeout_in_milliseconds(
timeout: libc::c_int,
) -> Result<(), Error> {
crate::init();

let error = raw::git_libgit2_opts(
raw::GIT_OPT_SET_SERVER_CONNECT_TIMEOUT as libc::c_int,
timeout,
);
// This function cannot actually fail, but the function has an error return
// for other options that can.
debug_assert!(error >= 0);

Ok(())
}

/// Set server timeout in milliseconds
///
/// # Safety
/// This function is modifying a C global without synchronization, so it is not
/// thread safe, and should only be called before any thread is spawned.
pub unsafe fn get_server_timeout_in_milliseconds() -> Result<libc::c_int, Error> {
crate::init();

let mut server_timeout = 0;

try_call!(raw::git_libgit2_opts(
raw::GIT_OPT_GET_SERVER_TIMEOUT as libc::c_int,
&mut server_timeout
));

Ok(server_timeout)
}

/// Set server timeout in milliseconds
///
/// # Safety
/// This function is modifying a C global without synchronization, so it is not
/// thread safe, and should only be called before any thread is spawned.
pub unsafe fn set_server_timeout_in_milliseconds(timeout: libc::c_int) -> Result<(), Error> {
crate::init();

let error = raw::git_libgit2_opts(
raw::GIT_OPT_SET_SERVER_TIMEOUT as libc::c_int,
timeout as libc::c_int,
);
// This function cannot actually fail, but the function has an error return
// for other options that can.
debug_assert!(error >= 0);

Ok(())
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -370,4 +447,20 @@ mod test {
assert!(get_mwindow_file_limit().unwrap() == 1024);
}
}

#[test]
fn server_connect_timeout() {
unsafe {
assert!(set_server_connect_timeout_in_milliseconds(5000).is_ok());
assert!(get_server_connect_timeout_in_milliseconds().unwrap() == 5000);
}
}

#[test]
fn server_timeout() {
unsafe {
assert!(set_server_timeout_in_milliseconds(10_000).is_ok());
assert!(get_server_timeout_in_milliseconds().unwrap() == 10_000);
}
}
}

0 comments on commit 006324a

Please sign in to comment.