From dd7fb60064ee77913e1400f86f82f27e991ac065 Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Thu, 31 Aug 2023 13:12:18 +0100 Subject: [PATCH] Add timeouts to client. --- reqwest-jni/src/lib.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/reqwest-jni/src/lib.rs b/reqwest-jni/src/lib.rs index c490f28..04b3bd5 100644 --- a/reqwest-jni/src/lib.rs +++ b/reqwest-jni/src/lib.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::sync::OnceLock; +use std::time::Duration; use jni::objects::{JByteArray, JClass, JMap, JObject, JString}; use jni::sys::jobject; @@ -39,7 +40,13 @@ pub extern "system" fn Java_rocks_kavin_reqwest4j_ReqwestUtils_init( Err(_) => builder, }; - let client = builder.build().unwrap(); + let client = builder + // timeout for establishing connection + .connect_timeout(Duration::from_secs(10)) + // timeout for entire request, till body is read + .timeout(Duration::from_secs(30)) + .build() + .unwrap(); CLIENT.set(client).unwrap(); RUNTIME.set(Runtime::new().unwrap()).unwrap(); }