cookie_agent_engine/
actor.rs1use std::{future::Future, thread};
8
9use tokio::sync::mpsc;
10
11#[derive(Debug)]
17pub struct SessionActor<M> {
18 sender: mpsc::Sender<M>,
19}
20
21impl<M: Send + 'static> Clone for SessionActor<M> {
22 fn clone(&self) -> Self {
23 Self {
24 sender: self.sender.clone(),
25 }
26 }
27}
28
29impl<M: Send + 'static> SessionActor<M> {
30 #[must_use]
31 pub fn spawn<F, Fut>(capacity: usize, mut handler: F) -> Self
32 where
33 F: FnMut(M) -> Fut + Send + 'static,
34 Fut: Future<Output = ()> + Send + 'static,
35 {
36 let (sender, mut receiver) = mpsc::channel(capacity);
37 thread::Builder::new()
38 .name("cookie_agent_session_actor".into())
39 .spawn(move || {
40 let runtime = tokio::runtime::Builder::new_current_thread()
41 .enable_all()
42 .build()
43 .expect("create session actor runtime");
44 runtime.block_on(async move {
45 while let Some(message) = receiver.recv().await {
46 handler(message).await;
47 }
48 });
49 })
50 .expect("spawn session actor thread");
51 Self { sender }
52 }
53
54 pub async fn send(&self, message: M) -> Result<(), mpsc::error::SendError<M>> {
55 self.sender.send(message).await
56 }
57
58 pub fn blocking_send(&self, message: M) -> Result<(), mpsc::error::SendError<M>> {
60 self.sender.blocking_send(message)
61 }
62}