Skip to content

web search done right

remove retry strategy from ampc and add the missing features to the one from sonic so it can be re-used

Mikkel Denker0b57dbfe619e
diff --git a/crates/core/src/ampc/coordinator.rs b/crates/core/src/ampc/coordinator.rs
index 3880f382..ad1b6b4a 100644
--- a/crates/core/src/ampc/coordinator.rs
+++ b/crates/core/src/ampc/coordinator.rs
@@ -20,11 +20,8 @@ use std::{
time::Duration,
};
-use super::{
- DhtConn, ExponentialBackoff, Finisher, Job, JobScheduled, RemoteWorker, Setup, Worker,
- WorkerRef,
-};
-use crate::Result;
+use super::{DhtConn, Finisher, Job, JobScheduled, RemoteWorker, Setup, Worker, WorkerRef};
+use crate::{distributed::retry_strategy::ExponentialBackoff, Result};
use anyhow::anyhow;
pub struct Coordinator<J>
@@ -108,8 +105,7 @@ where
mapper: J::Mapper,
) -> Result<()> {
tracing::debug!("Awaiting scheduled jobs");
- let mut sleeper =
- ExponentialBackoff::new(Duration::from_millis(100), Duration::from_secs(10), 2.0);
+ let mut sleeper = ExponentialBackoff::from_millis(100).with_limit(Duration::from_secs(10));
loop {
let worker_jobs = self
@@ -144,7 +140,7 @@ where
break;
}
- std::thread::sleep(sleeper.next());
+ std::thread::sleep(sleeper.next().expect("sleeper should not be exhausted"));
}
Ok(())
@@ -168,11 +164,8 @@ where
for mapper in &self.mappers {
// run round
let mut remaining_jobs: VecDeque<_> = jobs.clone().into_iter().collect();
- let mut sleeper = ExponentialBackoff::new(
- Duration::from_millis(100),
- Duration::from_secs(10),
- 2.0,
- );
+ let mut sleeper =
+ ExponentialBackoff::from_millis(100).with_limit(Duration::from_secs(10));
let mut scheduled_jobs: BTreeMap<WorkerRef, J> = BTreeMap::new();
@@ -201,7 +194,8 @@ where
}
JobScheduled::NoAvailableWorkers => {
remaining_jobs.push_front(job);
- let sleep_duration = sleeper.next();
+ let sleep_duration =
+ sleeper.next().expect("sleeper should not be exhausted");
std::thread::sleep(sleep_duration);
}
}
diff --git a/crates/core/src/ampc/mod.rs b/crates/core/src/ampc/mod.rs
index 9add597b..8b58a223 100644
--- a/crates/core/src/ampc/mod.rs
+++ b/crates/core/src/ampc/mod.rs
@@ -16,7 +16,6 @@
use self::{job::Job, worker::WorkerRef};
use crate::distributed::sonic;
-use std::time::Duration;
mod coordinator;
pub mod dht;
@@ -76,33 +75,3 @@ enum JobScheduled {
Success(WorkerRef),
NoAvailableWorkers,
}
-
-struct ExponentialBackoff {
- min: Duration,
- max: Duration,
- factor: f64,
- attempts: usize,
-}
-
-impl ExponentialBackoff {
- fn new(min: Duration, max: Duration, factor: f64) -> Self {
- Self {
- min,
- max,
- factor,
- attempts: 0,
- }
- }
-}
-
-impl ExponentialBackoff {
- fn next(&mut self) -> Duration {
- let duration = self.min.mul_f64(self.factor.powi(self.attempts as i32));
- self.attempts += 1;
- duration.min(self.max)
- }
-
- fn success(&mut self) {
- self.attempts = 0;
- }
-}
diff --git a/crates/core/src/distributed/retry_strategy.rs b/crates/core/src/distributed/retry_strategy.rs
index 93f920ce..3aa933ed 100644
--- a/crates/core/src/distributed/retry_strategy.rs
+++ b/crates/core/src/distributed/retry_strategy.rs
@@ -33,6 +33,10 @@ impl ExponentialBackoff {
self.max_delay = Some(limit);
self
}
+
+ pub fn success(&mut self) {
+ self.current = self.base;
+ }
}
impl Iterator for ExponentialBackoff {