Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support global opts related to server timeouts #1052

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
92 changes: 92 additions & 0 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,82 @@ 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
));

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(())
}

/// Get 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 +446,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);
}
}
}