Skip to content

Commit 9598e97

Browse files
authored
Merge pull request #1052 from Qovery/feat/support-server-timeouts-master
Support global opts related to server timeouts
2 parents c9b5f81 + e17e77c commit 9598e97

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl Error {
128128
raw::GIT_EINDEXDIRTY => super::ErrorCode::IndexDirty,
129129
raw::GIT_EAPPLYFAIL => super::ErrorCode::ApplyFail,
130130
raw::GIT_EOWNER => super::ErrorCode::Owner,
131+
raw::GIT_TIMEOUT => super::ErrorCode::Timeout,
131132
_ => super::ErrorCode::GenericError,
132133
}
133134
}
@@ -165,6 +166,7 @@ impl Error {
165166
ErrorCode::IndexDirty => raw::GIT_EINDEXDIRTY,
166167
ErrorCode::ApplyFail => raw::GIT_EAPPLYFAIL,
167168
ErrorCode::Owner => raw::GIT_EOWNER,
169+
ErrorCode::Timeout => raw::GIT_TIMEOUT,
168170
};
169171
}
170172

@@ -296,6 +298,7 @@ impl Error {
296298
GIT_EINDEXDIRTY,
297299
GIT_EAPPLYFAIL,
298300
GIT_EOWNER,
301+
GIT_TIMEOUT,
299302
)
300303
}
301304

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ pub enum ErrorCode {
218218
ApplyFail,
219219
/// The object is not owned by the current user
220220
Owner,
221+
/// Timeout
222+
Timeout,
221223
}
222224

223225
/// An enumeration of possible categories of things that can have

src/opts.rs

+92
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,82 @@ pub unsafe fn set_mwindow_file_limit(limit: libc::size_t) -> Result<(), Error> {
338338
Ok(())
339339
}
340340

341+
/// Get server connect timeout in milliseconds
342+
///
343+
/// # Safety
344+
/// This function is modifying a C global without synchronization, so it is not
345+
/// thread safe, and should only be called before any thread is spawned.
346+
pub unsafe fn get_server_connect_timeout_in_milliseconds() -> Result<libc::c_int, Error> {
347+
crate::init();
348+
349+
let mut server_connect_timeout = 0;
350+
351+
try_call!(raw::git_libgit2_opts(
352+
raw::GIT_OPT_GET_SERVER_CONNECT_TIMEOUT as libc::c_int,
353+
&mut server_connect_timeout
354+
));
355+
356+
Ok(server_connect_timeout)
357+
}
358+
359+
/// Set server connect timeout in milliseconds
360+
///
361+
/// # Safety
362+
/// This function is modifying a C global without synchronization, so it is not
363+
/// thread safe, and should only be called before any thread is spawned.
364+
pub unsafe fn set_server_connect_timeout_in_milliseconds(
365+
timeout: libc::c_int,
366+
) -> Result<(), Error> {
367+
crate::init();
368+
369+
let error = raw::git_libgit2_opts(
370+
raw::GIT_OPT_SET_SERVER_CONNECT_TIMEOUT as libc::c_int,
371+
timeout,
372+
);
373+
// This function cannot actually fail, but the function has an error return
374+
// for other options that can.
375+
debug_assert!(error >= 0);
376+
377+
Ok(())
378+
}
379+
380+
/// Get server timeout in milliseconds
381+
///
382+
/// # Safety
383+
/// This function is modifying a C global without synchronization, so it is not
384+
/// thread safe, and should only be called before any thread is spawned.
385+
pub unsafe fn get_server_timeout_in_milliseconds() -> Result<libc::c_int, Error> {
386+
crate::init();
387+
388+
let mut server_timeout = 0;
389+
390+
try_call!(raw::git_libgit2_opts(
391+
raw::GIT_OPT_GET_SERVER_TIMEOUT as libc::c_int,
392+
&mut server_timeout
393+
));
394+
395+
Ok(server_timeout)
396+
}
397+
398+
/// Set server timeout in milliseconds
399+
///
400+
/// # Safety
401+
/// This function is modifying a C global without synchronization, so it is not
402+
/// thread safe, and should only be called before any thread is spawned.
403+
pub unsafe fn set_server_timeout_in_milliseconds(timeout: libc::c_int) -> Result<(), Error> {
404+
crate::init();
405+
406+
let error = raw::git_libgit2_opts(
407+
raw::GIT_OPT_SET_SERVER_TIMEOUT as libc::c_int,
408+
timeout as libc::c_int,
409+
);
410+
// This function cannot actually fail, but the function has an error return
411+
// for other options that can.
412+
debug_assert!(error >= 0);
413+
414+
Ok(())
415+
}
416+
341417
#[cfg(test)]
342418
mod test {
343419
use super::*;
@@ -370,4 +446,20 @@ mod test {
370446
assert!(get_mwindow_file_limit().unwrap() == 1024);
371447
}
372448
}
449+
450+
#[test]
451+
fn server_connect_timeout() {
452+
unsafe {
453+
assert!(set_server_connect_timeout_in_milliseconds(5000).is_ok());
454+
assert!(get_server_connect_timeout_in_milliseconds().unwrap() == 5000);
455+
}
456+
}
457+
458+
#[test]
459+
fn server_timeout() {
460+
unsafe {
461+
assert!(set_server_timeout_in_milliseconds(10_000).is_ok());
462+
assert!(get_server_timeout_in_milliseconds().unwrap() == 10_000);
463+
}
464+
}
373465
}

0 commit comments

Comments
 (0)