Compare commits

..

2 commits
iterfn ... main

Author SHA1 Message Date
10e5c77454
update 2025-04-27 19:26:34 +02:00
dcebded8a3
🚑️ rally fn fix 2025-04-01 13:28:04 +02:00
3 changed files with 23 additions and 13 deletions

View file

@ -9,8 +9,9 @@ fn main() {
let (input, output) = rally(items, |item: &_| {
std::thread::sleep(Duration::from_millis(item * 100));
return 0;
});
return Some(0);
})
.unwrap();
println!("RALLY RESULTS: {input:?} -> {output:?}");
}

View file

@ -27,10 +27,12 @@ pub static UNION: Lazy<
/// Rally Function
///
/// This executes a thread for every item executing `f(&item) -> X`. Whatever function returns first is returned while every other thread is killed.
pub fn rally<T: Send + Sync + 'static, F, X: Send + 'static>(items: Vec<T>, f: F) -> (T, X)
pub fn rally<T: Send + Sync + 'static, F, X: Send + 'static>(items: Vec<T>, f: F) -> Option<(T, X)>
where
F: Fn(&T) -> X + Send + Sync + Copy + 'static,
F: Fn(&T) -> Option<X> + Send + Sync + Copy + 'static,
{
let item_len = items.len();
let (tx, rx) = mpsc::channel();
let items_len = items.len();
let mut handles = Vec::new();
@ -51,16 +53,25 @@ where
drop(tx);
let (fastest_item, fastest_result, elapsed) = rx.recv().unwrap();
let mut count = 0;
for handle in handles {
// todo : threads do not get killed here
handle.thread().unpark();
while count < item_len {
let (fastest_item, fastest_result, elapsed) = rx.recv().unwrap();
count += 1;
if fastest_result.is_some() {
for handle in handles {
// todo : threads do not get killed here
handle.thread().unpark();
}
log::info!("Rally ended with {items_len} items in {elapsed:?}");
return Some((fastest_item, fastest_result.unwrap()));
}
}
log::info!("Rally ended with {items_len} items in {elapsed:?}");
(fastest_item, fastest_result)
None
}
pub fn retry<O, F: Fn() -> Option<O>>(f: F) -> O {

View file

@ -61,8 +61,6 @@ pub struct ServiceManager {
pub mode: ServiceMode,
}
// TODO : impl decay mode
/// The mode on which services should operate
pub enum ServiceMode {
/// Behave like a daemon. Services can never die and will come back to life after beeing killed. They will always haunt you.