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

[FEATURE] - RemoteActorRef.lookup_or_spawn #94

Closed
cawfeecoder opened this issue Dec 14, 2024 · 3 comments
Closed

[FEATURE] - RemoteActorRef.lookup_or_spawn #94

cawfeecoder opened this issue Dec 14, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@cawfeecoder
Copy link

Feature Description

Instead of performing a lookup and receiving a result or Some/None, I should be able to call lookup_or_spawn which will lookup the given actor or force that actor to spawn (either locally or on a remote node) and give back the RemoteActorRef to the newly spawned actor.

Motivation

Let's suppose we have a order management system where a Shop is represented as an actor and can force an Order (represented by an actor) to spawn on another service that manages these order actors. I should be able to either get an existing Order actor already running or spawn a new actor (with a specific id/name).

Proposed Solution

If you have an idea of how to implement this feature, please provide a high-level overview of the solution.

There should be a message (not sure if it should be broadcast with a lease/claim type setup) propagated over libp2p to connected peers to spawn the actor if it isn't registered in the cluster already. Potentially that occurs based on the initial lookup message?

Alternatives Considered

Have you considered any alternative solutions? If so, please describe them.

I could, on failure to lookup (doesn't exist), send a message out of a band (via either gRPC/message broker/etc.) to have a remote actor spawn the desired actor (or use a local function if trying to spawn locally).

Additional Context

Add any other context, screenshots, or examples that would help illustrate the feature.

@cawfeecoder cawfeecoder added the enhancement New feature or request label Dec 14, 2024
@tqwewe
Copy link
Owner

tqwewe commented Dec 15, 2024

There should be a message (not sure if it should be broadcast with a lease/claim type setup) propagated over libp2p to connected peers to spawn the actor if it isn't registered in the cluster already. Potentially that occurs based on the initial lookup message?

This sounds like it implies that you want the actor to be running on all nodes in the swarm? Currently, there's no way of spawning an actor on a specific remote node as I haven't found much of a reason to add this into the core of kameo, and could be accomplished in user land via messaging.

Is what you're suggesting any different to the following?

let actor_ref = match RemoteActorRef::<MyActor>::lookup("my-actor").await? {
    Some(actor_ref) => actor_ref,
    None => {
        let actor_ref = kameo::actor::spawn(MyActor);
        actor_ref.register("my-actor").await?;
        actor_ref
    }
};

The code snippet above returns ActorRef instead of RemoteActorRef, but perhaps a From<ActorRef<A>> for RemoteActorRef<A> could be added to resolve this.

@cawfeecoder
Copy link
Author

There should be a message (not sure if it should be broadcast with a lease/claim type setup) propagated over libp2p to connected peers to spawn the actor if it isn't registered in the cluster already. Potentially that occurs based on the initial lookup message?

This sounds like it implies that you want the actor to be running on all nodes in the swarm? Currently, there's no way of spawning an actor on a specific remote node as I haven't found much of a reason to add this into the core of kameo, and could be accomplished in user land via messaging.

Is what you're suggesting any different to the following?

let actor_ref = match RemoteActorRef::<MyActor>::lookup("my-actor").await? {
    Some(actor_ref) => actor_ref,
    None => {
        let actor_ref = kameo::actor::spawn(MyActor);
        actor_ref.register("my-actor").await?;
        actor_ref
    }
};

The code snippet above returns ActorRef instead of RemoteActorRef, but perhaps a From<ActorRef<A>> for RemoteActorRef<A> could be added to resolve this.

This is probably fine. It's what I ended up doing. I wrote this ticket after a long week at work and realized afterwards that I was being stupid while working on my own stuff. But thank you for taking the time to reply :)

@tqwewe
Copy link
Owner

tqwewe commented Dec 19, 2024

No worries! I also have just made a PR which should be of use for this use case: #100

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

No branches or pull requests

2 participants