1个不稳定版本
0.1.0 | 2023年4月23日 |
---|
206 in 模拟
1.5MB
483 代码行
aelbreaker
aelstrom的Rust运行时库
特性
- 将消息反序列化为强类型枚举
- 发送和RPC支持
- 灵活和可扩展的消息
- 解耦输入/输出线程
示例: Echo
用于解决Gossip Glomers挑战的第一个示例(更多示例在/examples)
// main.rs
use maelbreaker::{
network::Network,
node::Node,
payload,
runtime::Runtime,
types::{Message, Try},
};
// We derive all the traits needed for our message types
// using the `payload!` macro.
payload!(
enum Payload {
Echo { echo: String },
EchoOk { echo: String },
}
);
struct EchoNode {
// Network is used to send messages from our node
network: Network<Payload>,
}
impl Node<Payload> for EchoNode {
// We are also passed the nodes ID, as well as a list of all node IDs in the cluster,
// but we don't use them for this challenge.
fn from_init(network: Network<Payload>, _node_id: String, _node_ids: Vec<String>) -> Self {
EchoNode { network }
}
fn handle_message(&mut self, msg: Message<Payload>) -> Try {
let Payload::Echo { echo } = &msg.body.payload else {
bail!("expected echo");
};
let echo = echo.clone();
// we use our handy into_reply method to consume a request
// and produce a reply with the correct src, dest, and in_reply_to fields
let reply = msg.into_reply(Payload::EchoOk { echo });
// we send our response onto the network!
self.network.send(reply)
}
}
fn main() -> Try {
// we start the runtime with our Node and Payload implementations
Runtime::<Payload, EchoNode>::run()
}
致谢
- Jespen, Maelstrom
- Gossip Glomers, fly.io
- 其他Rust实现
依赖
~1.5–7.5MB
~54K SLoC