Skip to content

Commit f419034

Browse files
committed
feat: election timer
1 parent 84d025c commit f419034

File tree

4 files changed

+148
-74
lines changed

4 files changed

+148
-74
lines changed

Cargo.lock

+91
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ axum = { version = "0.6.20", features = ["macros"] }
1010
axum-macros = "0.4.1"
1111
clap = "4.4.2"
1212
color-eyre = "0.6.2"
13+
rand = "0.8.5"
1314
reqwest = { version = "0.11.20", features = ["serde_json", "json"] }
1415
serde = { version = "1.0.188", features = ["serde_derive"] }
1516
serde_json = "1.0.105"
1617
tokio = { version = "1.32.0", features = ["full"] }
18+
tracing = "0.1.40"

src/bin/main.rs

-74
This file was deleted.

src/main.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use color_eyre::Result;
2+
use rand::Rng;
3+
use tokio::{
4+
net::{
5+
unix::SocketAddr,
6+
TcpListener,
7+
TcpSocket,
8+
ToSocketAddrs,
9+
},
10+
select,
11+
sync::{
12+
mpsc,
13+
oneshot,
14+
},
15+
};
16+
use tracing::warn;
17+
18+
#[tokio::main]
19+
async fn main() -> Result<()> {
20+
Ok(())
21+
}
22+
23+
async fn start_network(addr: impl ToSocketAddrs) -> Result<()> {
24+
let listener = TcpListener::bind(addr).await?;
25+
26+
while let Ok((sock, addr)) = listener.accept().await {
27+
todo!()
28+
}
29+
30+
todo!()
31+
}
32+
33+
async fn election_timeout(
34+
mut msg_alert: mpsc::Receiver<()>,
35+
election_trigger: oneshot::Sender<()>,
36+
) {
37+
let mut rng = rand::thread_rng();
38+
loop {
39+
let rand_timeout = rng.gen_range(150..300);
40+
select! {
41+
_ = msg_alert.recv() => (),
42+
_ = tokio::time::sleep(std::time::Duration::from_millis(rand_timeout)) => break,
43+
}
44+
}
45+
if let Err(_) = election_trigger.send(()) {
46+
warn!("Election timeout triggered but failed to send warning via oneshot");
47+
}
48+
}
49+
50+
/*
51+
* Who fires the first ever message?
52+
* - Broadcast (but each has a retry for failures)
53+
* Streams are only initiated on the first dial
54+
*
55+
*/

0 commit comments

Comments
 (0)