I was wondering if anyone has a working example for channels in actix rust.
I know what they do it's a buffer that streams between two actors. usually I read the unitests to get a better understanding how to get it to work.
github.com/actix/actix/blob/master/src/address/ch…
didn't help much because basically it's just the test if you can fetch something from the channel not how the channel work together with the other parts.
the only real implementation example I found was the Mailbox: github.com/actix/actix/blob/master/src/mailbox.rs
I actually want to do something like:
use actix::{Actor, SyncContext, Addr, Message, Handler, SyncArbiter};
use std::sync::{Arc, Mutex};
use std::cell::RefCell;
use std::rc::Rc;
use core::borrow::Borrow;
use actix::dev::channel::{channel, AddressSender, AddressReceiver};
use std::{thread, time};
use std::sync::atomic::Ordering::SeqCst;
pub struct ActorA;
impl Actor for ActorA {
type Context = SyncContext<Self>;
}
pub struct Hello {
pub message: String
}
impl Message for Hello {
type Result = Result<(), ()>;
}
impl Handler<Hello> for ActorA {
type Result = Result<(), ()>;
fn handle(&mut self, msg: Hello, _: &mut Self::Context) -> Self::Result {
println!("{}", msg.message);
Ok(())
}
}
pub struct ActorB {
pub sender_a: AddressSender<ActorA>
}
impl Actor for ActorB{
type Context = SyncContext<Self>;
}
pub struct Start;
impl Message for Start {
type Result = Result<bool, ()>;
}
impl Handler<Start> for ActorB {
type Result = Result<bool, ()>;
fn handle(&mut self, _: Start, _: &mut Self::Context) -> Self::Result {
self.sender_a.send(Hello {
message: "world".parse().unwrap()
});
Ok(true)
}
}
fn main() {
actix::System::run( || {
let (sender_a, mut receiver_a) = channel::<ActorA>(1);
let actor_a= SyncArbiter::start(3, || {
ActorA
});
let actor_b= SyncArbiter::start(1, move || {
ActorB {
sender_a: sender_a.clone()
}
});
actor_b.do_send(Start);
actor_a.do_send(Hello{ message: ("start").parse().unwrap() });
});
}
The idea is that I have a datastructure actor and different server actors (CoAP, HTTP, ...).
And I want my server actors to ask the datastructure actor to perform certain operations.
Mark Marco any of you guys ever did something like that?
anybody else? ;D or is this going to be a
question ;D
j
stuff ;)
I got some answers of the rust community @twitter :) but still the channels remain a mystery :) I am not sure most likely I have to change the whole structure and architecture of the application.
The core issue I got is that even if it take the Addr<ActorA> I have to most likely do some Box magic because I have to pass ownership without a specific lifetime which means ... 'heap time' and lifteime issues.
Usually you could just 'move' the ownership but since the actor contains Hashmaps in the state and hashmaps are on the heap. Which implies they cannot implement the Copy-Trait because the Copy-Trait is restricted to the stack.
And I need the Copy-Trait because esp in concurrency there most likely should not be shared state .... anyhow this is currently a bit frustrating but a good learning experience.
So I think I will exhaust myself for ~ 8 more hours and after that I will likely go to my whiteboard and try the feynman approach of solving problems.
^^ ... I guess this will be my sad logbook of why actix hate me anyways ;D ...