-
Hi! Another of my weird questions coming from actix. In actix an Addr is only a wrapped channel use actix::dev::channel;
use actix::{Actor, Addr, ArbiterHandle, Context};
use crate::MAILBOX_CAPACITY;
pub trait ActixExt
where
Self: Actor<Context = Context<Self>> + Send + 'static,
{
fn start_in_arbiter(self, arbiter: &ArbiterHandle) -> Addr<Self> {
let (tx, rx) = channel::channel(MAILBOX_CAPACITY);
// create actor
arbiter.spawn_fn(move || {
let ctx = Context::with_receiver(rx);
let fut = ctx.into_future(self);
actix_rt::spawn(fut);
});
Addr::new(tx)
}
}
impl<A: Actor<Context = Context<Self>> + Send> ActixExt for A {} Essentially that allows you to fix a chicken-and-egg problem when you have two actors that need to reference each other. struct ServiceA {
other_state: Mutex<()>,
worker_ref: Addr<ServiceAWorker>
}
#[derive(Actor)]
struct ServiceAWorker {
service: Arc<dyn ServiceADef>
}
impl ServiceA {
pub fn init() -> Arc<dyn ServiceADef> {
let svc = Arc::new(ServiceA { .. });
// spawn worker actor here, give it a clone of svc
svc
}
} Any solution to that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
You might be looking for |
Beta Was this translation helpful? Give feedback.
Hum I still need to get a clone of my service Arc outside of that spawn so not really. Not too bad, I will add an option on the service side, I was trying to avoid it but it doesn't seem possible at the moment.
So I will do