Skip to content

Commit f11ec55

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

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ pub trait HttpClient: Clone + Send + Sync {
101101
content_type: &str,
102102
expected_status_code: u16,
103103
) -> Result<Output, Error>;
104+
105+
#[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))]
106+
fn is_tokio(&self) -> bool {
107+
false
108+
}
104109
}
105110

106111
pub fn parse_response<Output: DeserializeOwned>(

src/reqwest.rs

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

113113
parse_response(status, expected_status_code, &body, url.to_string())
114114
}
115+
116+
#[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))]
117+
fn is_tokio(&self) -> bool {
118+
true
119+
}
115120
}
116121

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

src/utils.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ 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 {
1315
#[cfg(not(target_arch = "wasm32"))]
1416
return Self::Thread;
1517

@@ -19,6 +21,10 @@ impl SleepBackend {
1921

2022
pub(crate) async fn sleep(self, interval: Duration) {
2123
match self {
24+
#[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))]
25+
Self::Tokio => {
26+
tokio::time::sleep(interval).await;
27+
}
2228
#[cfg(not(target_arch = "wasm32"))]
2329
Self::Thread => {
2430
let (sender, receiver) = futures_channel::oneshot::channel::<()>();
@@ -54,6 +60,17 @@ mod test {
5460
use super::*;
5561
use meilisearch_test_macro::meilisearch_test;
5662

63+
#[cfg(all(not(target_arch = "wasm32"), feature = "reqwest"))]
64+
#[meilisearch_test]
65+
async fn sleep_tokio() {
66+
let sleep_duration = Duration::from_millis(10);
67+
let now = std::time::Instant::now();
68+
69+
SleepBackend::Tokio.sleep(sleep_duration).await;
70+
71+
assert!(now.elapsed() >= sleep_duration);
72+
}
73+
5774
#[cfg(not(target_arch = "wasm32"))]
5875
#[meilisearch_test]
5976
async fn sleep_thread() {

0 commit comments

Comments
 (0)