Skip to content

Commit 8bd3fd7

Browse files
authored
fix: support ExponentialBackoffTimed (#77)
1 parent 093c7b3 commit 8bd3fd7

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

typesense/src/client/mod.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ mod multi_search;
117117
mod operations;
118118
mod preset;
119119
mod presets;
120+
mod retry_policy;
120121
mod stemming;
121122
mod stopword;
122123
mod stopwords;
@@ -137,6 +138,7 @@ use keys::Keys;
137138
use operations::Operations;
138139
use preset::Preset;
139140
use presets::Presets;
141+
use retry_policy::ClientRetryPolicy;
140142
use stemming::Stemming;
141143
use stopword::Stopword;
142144
use stopwords::Stopwords;
@@ -373,9 +375,9 @@ impl Client {
373375
#[builder(default = Duration::from_secs(60))]
374376
/// The duration after which an unhealthy node will be retried for requests.
375377
healthcheck_interval: Duration,
376-
#[builder(default = ExponentialBackoff::builder().build_with_max_retries(3))]
378+
#[builder(into, default)]
377379
/// The retry policy for transient network errors on a *single* node.
378-
retry_policy: ExponentialBackoff,
380+
retry_policy: ClientRetryPolicy,
379381
) -> Result<Self, &'static str> {
380382
let is_nearest_node_set = nearest_node.is_some();
381383

@@ -397,11 +399,19 @@ impl Client {
397399
let http_client = builder.build().expect("Failed to build reqwest client");
398400

399401
#[cfg(not(target_arch = "wasm32"))]
400-
let http_client = ReqwestMiddlewareClientBuilder::new(
402+
let mw_builder = ReqwestMiddlewareClientBuilder::new(
401403
builder.build().expect("Failed to build reqwest client"),
402-
)
403-
.with(RetryTransientMiddleware::new_with_policy(retry_policy))
404-
.build();
404+
);
405+
406+
#[cfg(not(target_arch = "wasm32"))]
407+
let http_client = match retry_policy {
408+
ClientRetryPolicy::Default(policy) => mw_builder
409+
.with(RetryTransientMiddleware::new_with_policy(policy))
410+
.build(),
411+
ClientRetryPolicy::Timed(policy) => mw_builder
412+
.with(RetryTransientMiddleware::new_with_policy(policy))
413+
.build(),
414+
};
405415

406416
let mut url = node_config.url;
407417
if url.len() > 1 && matches!(url.chars().last(), Some('/')) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pub use reqwest_retry::policies::{ExponentialBackoff, ExponentialBackoffTimed};
2+
3+
#[derive(Clone, Debug)]
4+
pub enum ClientRetryPolicy {
5+
Default(ExponentialBackoff),
6+
Timed(ExponentialBackoffTimed),
7+
}
8+
9+
impl Default for ClientRetryPolicy {
10+
fn default() -> Self {
11+
Self::Default(ExponentialBackoff::builder().build_with_max_retries(3))
12+
}
13+
}
14+
15+
impl From<ExponentialBackoff> for ClientRetryPolicy {
16+
fn from(p: ExponentialBackoff) -> Self {
17+
Self::Default(p)
18+
}
19+
}
20+
21+
impl From<ExponentialBackoffTimed> for ClientRetryPolicy {
22+
fn from(p: ExponentialBackoffTimed) -> Self {
23+
Self::Timed(p)
24+
}
25+
}

0 commit comments

Comments
 (0)