sraft

simple raft implementation
git clone https://git.parazyd.org/sraft
Log | Files | Refs | README

peer.rs (1285B)


      1 use std::net::SocketAddr;
      2 
      3 use async_executor::Executor;
      4 use async_std::sync::Arc;
      5 use clap::Parser;
      6 use easy_parallel::Parallel;
      7 use simplelog::{ColorChoice, Config, LevelFilter, TermLogger, TerminalMode};
      8 
      9 use sraft::{Raft, RaftRpc};
     10 
     11 #[derive(Parser)]
     12 struct Args {
     13     #[clap(long, short)]
     14     peer: Vec<SocketAddr>,
     15 
     16     #[clap(long, short)]
     17     id: u64,
     18 
     19     #[clap(long, short)]
     20     listen: SocketAddr,
     21 }
     22 
     23 #[async_std::main]
     24 async fn main() {
     25     let args = Args::parse();
     26 
     27     TermLogger::init(LevelFilter::Debug, Config::default(), TerminalMode::Mixed, ColorChoice::Auto)
     28         .unwrap();
     29 
     30     let mut raft = Raft::new(args.id);
     31     for (k, v) in args.peer.iter().enumerate() {
     32         raft.peers.insert(k as u64, *v);
     33     }
     34 
     35     let raft_rpc = RaftRpc(args.listen);
     36 
     37     let ex = Arc::new(Executor::new());
     38     let (_signal, shutdown) = async_channel::unbounded::<()>();
     39 
     40     Parallel::new()
     41         .each(0..4, |_| smol::future::block_on(ex.run(shutdown.recv())))
     42         //
     43         .add(|| {
     44             smol::future::block_on(async move {
     45                 raft_rpc.start().await;
     46             });
     47             Ok(())
     48         })
     49         //
     50         .finish(|| {
     51             smol::future::block_on(async move {
     52                 raft.start().await;
     53             })
     54         });
     55 }