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

feat: add remote actor support #35

Merged
merged 16 commits into from
Sep 1, 2024
Merged

feat: add remote actor support #35

merged 16 commits into from
Sep 1, 2024

Conversation

tqwewe
Copy link
Owner

@tqwewe tqwewe commented Aug 23, 2024

Adds support for registering/looking up actors in a distributed system, and messaging actors remotely, built on top of libp2p.

The distributed system can be run with:

Actor

#[derive(Actor, RemoteActor)] 
pub struct MyActor { 
    count: i64, 
} 
 
#[derive(Serialize, Deserialize)] 
pub struct Inc { 
    amount: u32, 
} 
 
#[remote_message("3b9128f1-0593-44a0-b83a-f4188baa05bf")] 
impl Message<Inc> for MyActor { 
    type Reply = i64; 
 
    async fn handle(&mut self, msg: Inc, _ctx: Context<'_, Self, Self::Reply>) -> Self::Reply { 
        println!("incrementing"); 
        self.count += msg.amount as i64; 
        self.count 
    } 
}

Node 1

ActorSwarm::bootstrap("127.0.0.1:8000".parse()?)?;

let actor_ref = kameo::spawn(MyActor);
actor_ref.register("my_actor").await?;

Node 2

let swarm = ActorSwarm::bootstrap("127.0.0.1:8001".parse()?)?;
swarm.add_peer_address(PeerId::random(), "127.0.0.1:8000".parse()?).await?;

let actor_ref = RemoteActorRef::<MyActor>::lookup("my_actor").await?;
actor_ref.ask(&Inc { amount: 10 }).send().await?;

@xingsongs
Copy link

👍👍👍

@tqwewe tqwewe self-assigned this Sep 1, 2024
@tqwewe tqwewe added the enhancement New feature or request label Sep 1, 2024
@tqwewe tqwewe merged commit bad2bbe into main Sep 1, 2024
@tqwewe tqwewe deleted the feat/remote branch September 1, 2024 09:01
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

Successfully merging this pull request may close these issues.

2 participants