-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Logger, refactor threads using new StatefulThread type
- Loading branch information
1 parent
4b445cf
commit afa93a7
Showing
2 changed files
with
119 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::{ | ||
fmt::Debug, | ||
future::Future, | ||
pin::pin, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
use tokio::{ | ||
sync::{mpsc, oneshot}, | ||
task::JoinHandle, | ||
}; | ||
|
||
pub trait StatefulFunction: Send + 'static { | ||
type Input: Send; | ||
type Output: Send + Debug; | ||
fn call(&mut self, input: Self::Input) -> impl Future<Output = Self::Output> + Send; | ||
} | ||
|
||
type Payload<F> = ( | ||
<F as StatefulFunction>::Input, | ||
oneshot::Sender<Option<<F as StatefulFunction>::Output>>, | ||
); | ||
|
||
pub struct StatefulThread<F: StatefulFunction> { | ||
_handle: JoinHandle<()>, | ||
input_tx: mpsc::Sender<Payload<F>>, | ||
cancel_tx: mpsc::Sender<()>, | ||
} | ||
|
||
impl<F: StatefulFunction> StatefulThread<F> { | ||
pub fn new(mut func: F) -> Self { | ||
let (input_tx, mut input_rx) = mpsc::channel::<Payload<F>>(1024); | ||
let (cancel_tx, mut cancel_rx) = mpsc::channel::<()>(1); | ||
let _handle = tokio::spawn(async move { | ||
while let Some((input, responder)) = input_rx.recv().await { | ||
let mut output_fut = pin!(func.call(input)); | ||
let mut cancel_fut = pin!(cancel_rx.recv()); | ||
let start = Instant::now(); | ||
loop { | ||
let log_fut = tokio::time::sleep(Duration::from_secs(1)); | ||
tokio::select! { | ||
response = &mut output_fut => { | ||
responder.send(Some(response)).unwrap(); | ||
break; | ||
} | ||
_ = &mut cancel_fut => { | ||
responder.send(None).unwrap(); | ||
break; | ||
} | ||
_ = log_fut => { | ||
println!("Waiting for {} seconds", start.elapsed().as_secs()); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
StatefulThread { | ||
_handle, | ||
input_tx, | ||
cancel_tx, | ||
} | ||
} | ||
|
||
pub async fn call(&self, input: F::Input) -> Option<F::Output> { | ||
let (tx, rx) = oneshot::channel(); | ||
self.input_tx.send((input, tx)).await.unwrap(); | ||
rx.await.unwrap() | ||
} | ||
|
||
pub async fn cancel(&self) { | ||
self.cancel_tx.send(()).await.unwrap(); | ||
} | ||
} |