Skip to content

Commit

Permalink
Merge pull request #1052 from Qovery/feat/support-server-timeouts-master
Browse files Browse the repository at this point in the history
Support global opts related to server timeouts
  • Loading branch information
ehuss authored May 24, 2024
2 parents c9b5f81 + e17e77c commit 9598e97
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
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);
}
}
}

0 comments on commit 9598e97

Please sign in to comment.