Compare commits
No commits in common. "main" and "iterfn" have entirely different histories.
3 changed files with 13 additions and 23 deletions
|
@ -9,9 +9,8 @@ fn main() {
|
||||||
|
|
||||||
let (input, output) = rally(items, |item: &_| {
|
let (input, output) = rally(items, |item: &_| {
|
||||||
std::thread::sleep(Duration::from_millis(item * 100));
|
std::thread::sleep(Duration::from_millis(item * 100));
|
||||||
return Some(0);
|
return 0;
|
||||||
})
|
});
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
println!("RALLY RESULTS: {input:?} -> {output:?}");
|
println!("RALLY RESULTS: {input:?} -> {output:?}");
|
||||||
}
|
}
|
||||||
|
|
29
src/lib.rs
29
src/lib.rs
|
@ -27,12 +27,10 @@ pub static UNION: Lazy<
|
||||||
/// Rally Function
|
/// 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.
|
/// 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) -> Option<(T, X)>
|
pub fn rally<T: Send + Sync + 'static, F, X: Send + 'static>(items: Vec<T>, f: F) -> (T, X)
|
||||||
where
|
where
|
||||||
F: Fn(&T) -> Option<X> + Send + Sync + Copy + 'static,
|
F: Fn(&T) -> X + Send + Sync + Copy + 'static,
|
||||||
{
|
{
|
||||||
let item_len = items.len();
|
|
||||||
|
|
||||||
let (tx, rx) = mpsc::channel();
|
let (tx, rx) = mpsc::channel();
|
||||||
let items_len = items.len();
|
let items_len = items.len();
|
||||||
let mut handles = Vec::new();
|
let mut handles = Vec::new();
|
||||||
|
@ -53,25 +51,16 @@ where
|
||||||
|
|
||||||
drop(tx);
|
drop(tx);
|
||||||
|
|
||||||
let mut count = 0;
|
let (fastest_item, fastest_result, elapsed) = rx.recv().unwrap();
|
||||||
|
|
||||||
while count < item_len {
|
for handle in handles {
|
||||||
let (fastest_item, fastest_result, elapsed) = rx.recv().unwrap();
|
// todo : threads do not get killed here
|
||||||
count += 1;
|
handle.thread().unpark();
|
||||||
|
|
||||||
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()));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
None
|
log::info!("Rally ended with {items_len} items in {elapsed:?}");
|
||||||
|
|
||||||
|
(fastest_item, fastest_result)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn retry<O, F: Fn() -> Option<O>>(f: F) -> O {
|
pub fn retry<O, F: Fn() -> Option<O>>(f: F) -> O {
|
||||||
|
|
|
@ -61,6 +61,8 @@ pub struct ServiceManager {
|
||||||
pub mode: ServiceMode,
|
pub mode: ServiceMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO : impl decay mode
|
||||||
|
|
||||||
/// The mode on which services should operate
|
/// The mode on which services should operate
|
||||||
pub enum ServiceMode {
|
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.
|
/// Behave like a daemon. Services can never die and will come back to life after beeing killed. They will always haunt you.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue