RiteRaft - 一个用于普通人使用的浮桥框架
This is an attempt to create a layer on top of tikv/raft-rs, that is easier to use and implement. This is not supposed to be the most featureful raft, but instead a convenient interface to get started quickly, and have a working raft in no time.
The interface is strongly inspired by the one used by canonical/raft.
In order to "raft" storage, we need to implement the Storage trait for it.
Bellow is an example with HashStore, which is a thread-safe wrapper around an
HashMap:
…Only 3 methods need to be implemented for the Store:
Store::apply: applies a commited entry to the store.Store::snapshot: returns snapshot data for the store.Store::restore: applies the snapshot passed as argument.#[tokio::main]
fn main() {
let store = HashStore::new();
let raft = Raft::new(options.raft_addr, store.clone());
let mailbox = Arc::new(raft.mailbox());
let (raft_handle, mailbox) = match options.peer_addr {
Some(addr) => {
info!("running in follower mode");
let handle = tokio::spawn(raft.join(addr));
(handle, mailbox)
}
None => {
info!("running in leader mode");
let handle = tokio::spawn(raft.lead());
(handle, mailbox)
}
};
tokio::join!(raft);
}
The mailbox gives you a way to interact with the raft, for sending a message, or leaving the cluster for example.
This work is based on raft-frp, but more adjustments and improvements have been made to the code .
This library is licensed under either of:
at your option.
暂无开放 Issues,或尚未同步最近议题。