multi threaded workers

This commit is contained in:
JMARyA 2025-03-08 18:01:16 +01:00
parent 0c190df5d7
commit e827faaa3f
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 208 additions and 45 deletions

View file

@ -1,22 +1,23 @@
use crossbeam::channel::{Receiver, Sender};
use rand::Rng;
use std::sync::mpsc;
#[derive(Clone)]
/// A generic job dispatcher struct that allows sending jobs of type `T` and receiving results of type `V` using message passing.
pub struct JobDispatcher<T: Send + 'static, V: Send + 'static> {
sender: Sender<JobOrder<T, V>>,
/// A generic job dispatcher struct that allows sending jobs of type `I` and receiving results of type `O` using message passing.
pub struct JobDispatcher<I: Send + 'static, O: Send + 'static> {
sender: Sender<JobOrder<I, O>>,
}
pub struct JobResult<V>(std::sync::mpsc::Receiver<V>);
pub struct JobResult<O>(std::sync::mpsc::Receiver<O>);
impl<V> JobResult<V> {
impl<O> JobResult<O> {
/// Wait for the Result of a Job.
pub fn wait(self) -> V {
pub fn wait(self) -> O {
self.0.recv().unwrap()
}
}
impl<T: Send + 'static, V: Send + 'static> JobDispatcher<T, V> {
impl<I: Send + 'static, O: Send + 'static> JobDispatcher<I, O> {
/// Creates a new instance of `JobDispatcher` and returns a tuple that contains it and a receiver end for `JobOrder`s.
/// # Example:
/// ```
@ -37,7 +38,7 @@ impl<T: Send + 'static, V: Send + 'static> JobDispatcher<T, V> {
/// assert_eq!(result, 4);
/// ```
#[must_use]
pub fn new() -> (Self, Receiver<JobOrder<T, V>>) {
pub fn new() -> (Self, Receiver<JobOrder<I, O>>) {
let (sender, receiver) = crossbeam::channel::bounded(12);
(Self { sender: sender }, receiver)
@ -48,7 +49,7 @@ impl<T: Send + 'static, V: Send + 'static> JobDispatcher<T, V> {
/// # Panics
/// This function panics when the `JobOrder` struct gets out of scope without returning a finished result.
/// Additionally if the internal `Mutex` is poisoned, this function will panic as well.
pub fn send(&self, param: T) -> V {
pub fn send(&self, param: I) -> O {
let (tx, rx) = mpsc::channel();
let job_order = JobOrder::new(param, move |ret| {
tx.send(ret).unwrap();
@ -57,7 +58,7 @@ impl<T: Send + 'static, V: Send + 'static> JobDispatcher<T, V> {
rx.recv().unwrap()
}
pub fn send_async(&self, param: T) -> JobResult<V> {
pub fn send_async(&self, param: I) -> JobResult<O> {
let (tx, rx) = mpsc::channel();
let job_order = JobOrder::new(param, move |ret| {
tx.send(ret).unwrap();
@ -68,7 +69,7 @@ impl<T: Send + 'static, V: Send + 'static> JobDispatcher<T, V> {
/// Sends a job of type `T` to the job dispatcher and waits for its result of type `V`.
/// Returns `Some(V)` when the job returns an result, `None` if somehow nothing was returned or the internal `Mutex` is poisoned.
pub fn try_send(&self, param: T) -> Option<V> {
pub fn try_send(&self, param: I) -> Option<O> {
let (tx, rx) = mpsc::channel();
let job_order = JobOrder::new(param, move |ret| {
tx.send(ret).unwrap();
@ -78,17 +79,17 @@ impl<T: Send + 'static, V: Send + 'static> JobDispatcher<T, V> {
}
}
/// A struct that represents a job order that encapsulates a job of type `T` and its result of type `V`, along with a callback function that will send the result back to the job origin.
pub struct JobOrder<T, V> {
/// A struct that represents a job order that encapsulates a job of type `I` and its result of type `O`, along with a callback function that will send the result back to the job origin.
pub struct JobOrder<I, O> {
/// The job parameter of type `T`.
pub param: T,
callback: Box<dyn FnOnce(V) + Send>,
pub param: I,
callback: Box<dyn FnOnce(O) + Send>,
}
impl<T, V> JobOrder<T, V> {
/// Creates a new `JobOrder` instance with the specified job parameter `param` of type `T` and a callback function that takes the job result of type `V` as an argument.
impl<I, O> JobOrder<I, O> {
/// Creates a new `JobOrder` instance with the specified job parameter `param` of type `I` and a callback function that takes the job result of type `O` as an argument.
#[must_use]
fn new(param: T, callback: impl FnOnce(V) + Send + 'static) -> Self {
fn new(param: I, callback: impl FnOnce(O) + Send + 'static) -> Self {
Self {
param,
callback: Box::new(callback),
@ -96,7 +97,44 @@ impl<T, V> JobOrder<T, V> {
}
/// Send the result of the `JobOrder` back to it's origin
pub fn done(self, val: V) {
pub fn done(self, val: O) {
(self.callback)(val);
}
}
pub struct JobMultiplexer<I: Send + 'static, O: Send + 'static> {
dispatchers: Vec<JobDispatcher<I, O>>,
}
fn get_random_item<T>(list: &[T]) -> Option<&T> {
if list.is_empty() {
return None;
}
let mut rng = rand::rng();
let index = rng.random_range(0..list.len());
list.get(index)
}
impl<I: Send + 'static, O: Send + 'static> JobMultiplexer<I, O> {
pub fn from(dispatchers: Vec<JobDispatcher<I, O>>) -> Self {
Self { dispatchers }
}
pub fn send(&self, param: I) -> O {
let d = get_random_item(&self.dispatchers).unwrap();
d.send(param)
}
pub fn send_async(&self, param: I) -> JobResult<O> {
let d = get_random_item(&self.dispatchers).unwrap();
d.send_async(param)
}
}
impl<I: Clone + Send + 'static, O: Send + 'static> JobMultiplexer<I, O> {
pub fn send_all(&self, param: I) {
for d in &self.dispatchers {
let _ = d.send(param.clone());
}
}
}

View file

@ -12,7 +12,7 @@ pub use serde_json;
// TODO : refactor dispatcher backends (memory, valkey)
pub static UNION: Lazy<
DashMap<&'static str, job::JobDispatcher<serde_json::Value, serde_json::Value>>,
DashMap<&'static str, job::JobMultiplexer<serde_json::Value, serde_json::Value>>,
> = Lazy::new(DashMap::new);
/// Rally Function