Add kameo delegate / forward examples
This commit is contained in:
parent
fb4f7c59fd
commit
841b9ebacb
76
Cargo.lock
generated
76
Cargo.lock
generated
@ -57,6 +57,12 @@ version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.7.2"
|
||||
@ -243,6 +249,16 @@ dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kameo-delegate-forward"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"kameo",
|
||||
"rand",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kameo_macros"
|
||||
version = "0.12.2"
|
||||
@ -356,6 +372,15 @@ version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04"
|
||||
dependencies = [
|
||||
"zerocopy",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.88"
|
||||
@ -374,6 +399,36 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.8.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"rand_chacha",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_chacha"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
|
||||
dependencies = [
|
||||
"ppv-lite86",
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rand_core"
|
||||
version = "0.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.7"
|
||||
@ -625,3 +680,24 @@ name = "windows_x86_64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||
dependencies = [
|
||||
"byteorder",
|
||||
"zerocopy-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zerocopy-derive"
|
||||
version = "0.7.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
@ -1,3 +1,3 @@
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = ["alpha", "futures-unordered", "kameo-actors", "my-actors"]
|
||||
members = ["alpha", "futures-unordered", "kameo-actors", "kameo-delegate-forward", "my-actors"]
|
||||
|
10
kameo-delegate-forward/Cargo.toml
Normal file
10
kameo-delegate-forward/Cargo.toml
Normal file
@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "kameo-delegate-forward"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
futures = "0.3.31"
|
||||
kameo = "0.12.2"
|
||||
rand = "0.8.5"
|
||||
tokio = { version = "1.41.1", features = ["full"] }
|
79
kameo-delegate-forward/src/main.rs
Normal file
79
kameo-delegate-forward/src/main.rs
Normal file
@ -0,0 +1,79 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use futures::{stream::FuturesUnordered, StreamExt};
|
||||
use kameo::{actor::ActorRef, error::Infallible, message::Message, reply::{DelegatedReply, ForwardedReply}, request::MessageSend, Actor};
|
||||
use rand::Rng;
|
||||
|
||||
#[derive(Actor)]
|
||||
struct PrinterServer {
|
||||
printer: ActorRef<Printer>,
|
||||
}
|
||||
|
||||
#[derive(Actor)]
|
||||
struct Printer;
|
||||
|
||||
struct PrintRequest;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum PrintResponse {
|
||||
PrintSuccess,
|
||||
}
|
||||
|
||||
impl PrinterServer {
|
||||
fn new(printer: ActorRef<Printer>) -> Self {
|
||||
Self {
|
||||
printer,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Message<PrintRequest> for PrinterServer {
|
||||
type Reply = ForwardedReply<PrintResponse, PrintRequest, Infallible>;
|
||||
|
||||
async fn handle(
|
||||
&mut self,
|
||||
msg: PrintRequest,
|
||||
mut ctx: kameo::message::Context<'_, Self, Self::Reply>,
|
||||
) -> Self::Reply {
|
||||
ctx.forward(self.printer.clone(), msg)
|
||||
}
|
||||
}
|
||||
|
||||
impl Message<PrintRequest> for Printer {
|
||||
type Reply = DelegatedReply<Result<PrintResponse, Infallible>>;
|
||||
|
||||
async fn handle(
|
||||
&mut self,
|
||||
_: PrintRequest,
|
||||
mut ctx: kameo::message::Context<'_, Self, Self::Reply>,
|
||||
) -> Self::Reply {
|
||||
let (delegated_reply, reply_sender) = ctx.reply_sender();
|
||||
|
||||
if let Some(tx) = reply_sender {
|
||||
tokio::spawn(async {
|
||||
let random_number = rand::thread_rng().gen_range(1..10);
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(random_number)).await;
|
||||
tx.send(Ok(PrintResponse::PrintSuccess));
|
||||
});
|
||||
}
|
||||
|
||||
delegated_reply
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let printer = kameo::spawn(Printer);
|
||||
let printer_server = kameo::spawn(PrinterServer::new(printer));
|
||||
let mut waiter = FuturesUnordered::new();
|
||||
|
||||
for _ in 1..10 {
|
||||
let print_request_delegated = printer_server.ask(PrintRequest).send();
|
||||
waiter.push(print_request_delegated);
|
||||
}
|
||||
|
||||
while let Some(response) = waiter.next().await {
|
||||
println!("{:?}", response);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user