Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what would be an appropriate way to integrate this with a web framework #27

Closed
evnix opened this issue Oct 3, 2018 · 6 comments
Closed

Comments

@evnix
Copy link

evnix commented Oct 3, 2018

I am new to Rust as well as the Actor Pattern on a whole, so I am trying to understand How do i integrate it with an existing web framework.

Should I include the code below within an actor (will it cause any performance issues?)
Should I include the code below within another thread. If so, how do I refer to all the actors.

Basically I am trying to forward all HTTP messages to existing actors that are running.
I did have a look at actix, but it doesn't support named actors(which is big for me) and docs aren't great.

#[macro_use] extern crate nickel;

use nickel::{Nickel, HttpRouter};
fn main() {
    let mut server = Nickel::new();

    server.get("/bar", middleware!("This is the /bar handler"));
    server.get("/user/:userid", middleware! { |request|
        format!("This is user: {:?}", request.param("userid"))
    });
    server.get("/a/*/d", middleware!("matches /a/b/d but not /a/b/c/d"));
    server.get("/a/**/d", middleware!("This matches /a/b/d and also /a/b/c/d"));

    server.listen("127.0.0.1:6767");
}
@leenozara
Copy link
Contributor

Hi @evnix this issue was overlooked, my apologies.

Riker supports selection. That is, you can message one or more actors by actor path (name). You can find examples on the Riker site here: https://riker.rs/selection/

As for your specific case my recommendation is to pass an instance of the ActorRef of the actor to the request handler. This is better than using select for performance reasons.

Here's an example that uses the Warp HTTP server (which is a really nice functional HTTP server btw):

fn main() {
    let model: DefaultModel<String> = DefaultModel::new();
    let sys = ActorSystem::new(&model).unwrap();

    let cfg = AppConfig::from(sys.config());

    let actor = sys.actor_of(MyActor::props(), "actor").unwrap();

    let h_sys = sys.clone();
    let hello = path!("hello" / String)
        .map(move|name| {
            let res = block_on(ask(&h_sys, &actor, name)).unwrap();
            format!("{}", res)
        });

    warp::serve(hello)
        .run(cfg.http_sock);
}

struct MyActor;

impl Actor for MyActor {
    type Msg = String;

    fn receive(&mut self,
                ctx: &Context<Self::Msg>,
                msg: Self::Msg,
                sender: Option<ActorRef<Self::Msg>>) {

        let msg = format!("hello {}", msg);
        let _ = sender.try_tell(msg, Some(ctx.myself()));
    }
}

In this example all requests for /hello/<string> will result in a message of <string> being sent to the actor. It waits for the actor to send a message back. This uses the ask pattern which is a Future, so you can use block_on to get the returned value and form an http response. You can read more on ask here: https://riker.rs/patterns/

Hope that helps!

@evnix
Copy link
Author

evnix commented Oct 19, 2018

Thanks a lot for the in-depth example.

@evnix evnix closed this as completed Oct 19, 2018
@tylerhjones
Copy link

@leenozara I am trying to follow your example here, and have the following setup.

impl Actor for Validator {
    type Msg = ValidationMsg;


    fn recv(&mut self,
        ctx: &Context<Self::Msg>,
        msg: Self::Msg,
        sender: Sender) {

        println!("Received: {:?}", msg);
        println!("Sender is: {:?}", sender);
        let res = sender.as_ref()
                .unwrap()
                .try_tell("valid!", Some(ctx.myself().into()));
        
        match res {
            Ok(r)=>{
                println!("Result: {:?}", r);
            },
            Err(e)=>{
                println!("Error: {:?}", e);
            }
        }
    }
}

#[tokio::main]
async fn main() {
    let sys = ActorSystem::new().unwrap();

    let validator = sys.sys_actor_of::<Validator>("validator").unwrap();

    let cloned_sys = sys.clone();

    let verify = warp::post()
        .and(warp::path("verify"))
        .and(warp::header::header("Authorization"))
        .and(warp::body::json())
        .map(move |auth_header: String, verify_req: VerifyRequest| {
            let msg = ValidationMsg { 
                token: verify_req.token, 
                auth_header
            };

            let res:RemoteHandle<String> = ask(&cloned_sys, &validator, msg);
            let answer = block_on(res);
            println!("{:?}", answer);
            format!("bingo!!")
        });

    let hello = warp::path!("hello" / String)
        .map(|name| format!("Hello, {}!", name));

    let routes = hello.or(verify);

    warp::serve(routes)
        .run(([127, 0, 0, 1], 3030))
        .await
}

However, the try_tell fails without any message, and the sever never responds. What setup am I missing for the actor to have it successfully reply to the temporary actor?

@tylerhjones
Copy link

[dependencies]
riker = "0.4.2"
riker-patterns = "0.4.2"
futures = "0.3.14"
tokio = { version = "1", features = ["full"] }
warp = "0.3"

@tylerhjones
Copy link

I have updated my actor and resolved some of the issues, however the actor_ref.try_tell("valid!", myself); is failing with no error message or trace.

impl Actor for Validator {
    type Msg = ValidationMsg;


    fn recv(&mut self,
        ctx: &Context<Self::Msg>,
        msg: Self::Msg,
        sender: Sender) {

        println!("Received: {:?}", msg);
        println!("Sender is: {:?}", sender);

        match sender {
            Some(actor_ref) => {
                println!("pulling context");
                let myself = Some(ctx.myself().into());
                println!("attempting to send");
                let res = actor_ref.try_tell("valid!", myself);

                match res {
                    Ok(_x) => {
                        println!("all good");
                    },
                    Err(e) => {
                        println!("error {:?}", e);
                    }
                }
            },
            None => {}
        }
    }
}

@tylerhjones
Copy link

tylerhjones commented May 20, 2021

I think it is something to do with my ask macro and msg types.

        .map(move |auth_header: String, verify_req: VerifyRequest| {
            let msg = ValidationMsg { 
                token: verify_req.token, 
                auth_header
            };

            let res:RemoteHandle<String> = ask(&cloned_sys, &validator, msg);
            let answer = block_on(res);
            println!("{:?}", answer);
            format!("bingo!!")
        });

Maybe the ask is creating an actor but the msg types dont line up and no answer is delivered?

@evnix Did you get this working? I only see your rFrame repo, which doesnt have a dep. on riker.
https://github.com/evnix?tab=repositories&q=&type=&language=rust&sort=

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants