Skip to content

Commit 1b32767

Browse files
Add tokio sleep backend and use it when reqwest is used
1 parent f29a3b2 commit 1b32767

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ thiserror = "2.0.12"
2525
meilisearch-index-setting-macro = { path = "meilisearch-index-setting-macro", version = "0.29.1" }
2626
pin-project-lite = { version = "0.2.16", optional = true }
2727
reqwest = { version = "0.12.22", optional = true, default-features = false, features = ["rustls-tls", "http2", "stream"] }
28+
tokio = { version = "1.38", optional = true, features = ["time"] }
2829
bytes = { version = "1.10.1", optional = true }
2930
uuid = { version = "1.17.0", features = ["v4"] }
3031
futures-core = "0.3.31"
@@ -42,7 +43,7 @@ wasm-bindgen-futures = "0.4"
4243

4344
[features]
4445
default = ["reqwest"]
45-
reqwest = ["dep:reqwest", "pin-project-lite", "bytes"]
46+
reqwest = ["dep:reqwest", "dep:tokio", "pin-project-lite", "bytes"]
4647
futures-unsend = []
4748

4849
[dev-dependencies]

src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ impl<Http: HttpClient> Client<Http> {
11461146
}
11471147

11481148
fn sleep_backend(&self) -> SleepBackend {
1149-
SleepBackend::infer()
1149+
SleepBackend::infer(self.http_client.is_tokio())
11501150
}
11511151
}
11521152

src/request.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ pub trait HttpClient: Clone + Send + Sync {
101101
content_type: &str,
102102
expected_status_code: u16,
103103
) -> Result<Output, Error>;
104+
105+
fn is_tokio(&self) -> bool {
106+
false
107+
}
104108
}
105109

106110
pub fn parse_response<Output: DeserializeOwned>(

src/reqwest.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ impl HttpClient for ReqwestClient {
112112

113113
parse_response(status, expected_status_code, &body, url.to_string())
114114
}
115+
116+
fn is_tokio(&self) -> bool {
117+
true
118+
}
115119
}
116120

117121
fn verb<Q, B>(method: &Method<Q, B>) -> reqwest::Method {

src/utils.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@ use std::time::Duration;
22

33
#[derive(Debug, Copy, Clone)]
44
pub(crate) enum SleepBackend {
5+
#[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))]
6+
Tokio,
57
#[cfg(not(target_arch = "wasm32"))]
68
Thread,
79
#[cfg(target_arch = "wasm32")]
810
Javascript,
911
}
1012

1113
impl SleepBackend {
12-
pub(crate) fn infer() -> Self {
14+
pub(crate) fn infer(is_tokio: bool) -> Self {
15+
#[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))]
16+
if is_tokio {
17+
return Self::Tokio;
18+
}
19+
#[cfg(any(target_arch = "wasm32", not(feature = "reqwest")))]
20+
let _ = is_tokio;
21+
1322
#[cfg(not(target_arch = "wasm32"))]
1423
return Self::Thread;
1524

@@ -19,6 +28,10 @@ impl SleepBackend {
1928

2029
pub(crate) async fn sleep(self, interval: Duration) {
2130
match self {
31+
#[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))]
32+
Self::Tokio => {
33+
tokio::time::sleep(interval).await;
34+
}
2235
#[cfg(not(target_arch = "wasm32"))]
2336
Self::Thread => {
2437
let (sender, receiver) = futures_channel::oneshot::channel::<()>();
@@ -54,6 +67,17 @@ mod test {
5467
use super::*;
5568
use meilisearch_test_macro::meilisearch_test;
5669

70+
#[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))]
71+
#[meilisearch_test]
72+
async fn sleep_tokio() {
73+
let sleep_duration = Duration::from_millis(10);
74+
let now = std::time::Instant::now();
75+
76+
SleepBackend::Tokio.sleep(sleep_duration).await;
77+
78+
assert!(now.elapsed() >= sleep_duration);
79+
}
80+
5781
#[cfg(not(target_arch = "wasm32"))]
5882
#[meilisearch_test]
5983
async fn sleep_thread() {

0 commit comments

Comments
 (0)