2
0
mirror of https://github.com/Revertron/Alfis synced 2024-11-15 06:12:52 +00:00

Optimized network connections (added write timeouts).

This commit is contained in:
Revertron 2023-01-30 14:40:59 +01:00
parent da7db8dfa4
commit c199a62a04
5 changed files with 112 additions and 26 deletions

89
Cargo.lock generated
View File

@ -84,7 +84,7 @@ dependencies = [
[[package]]
name = "alfis"
version = "0.8.3"
version = "0.8.4"
dependencies = [
"base64",
"bincode",
@ -158,9 +158,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "base64"
version = "0.13.0"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "bincode"
@ -792,14 +792,14 @@ dependencies = [
[[package]]
name = "mio"
version = "0.8.4"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57ee1c23c7c63b0c9250c339ffdc69255f110b298b901b9f6c82547b7b87caaf"
checksum = "e5d732bc30207a6423068df043e3d02e0735b155ad7ce1a6f76fe2baa5b158de"
dependencies = [
"libc",
"log",
"wasi 0.11.0+wasi-snapshot-preview1",
"windows-sys",
"windows-sys 0.42.0",
]
[[package]]
@ -870,7 +870,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4a3100141f1733ea40b53381b0ae3117330735ef22309a190ac57b9576ea716"
dependencies = [
"pathdiff",
"windows-sys",
"windows-sys 0.36.1",
]
[[package]]
@ -1319,9 +1319,9 @@ dependencies = [
[[package]]
name = "thread-priority"
version = "0.9.2"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a8a950b52fd40d98ac6ed41c7fa9e8dd62b131f48b74f418e810476b01a7460"
checksum = "978519ae4c6891352f964b88da4ab5a3a9b74a40247cda5baee145dae6cd3b71"
dependencies = [
"cfg-if",
"libc",
@ -1484,9 +1484,9 @@ checksum = "5a1f0175e03a0973cf4afd476bef05c26e228520400eb1fd473ad417b1c00ffb"
[[package]]
name = "uuid"
version = "1.1.2"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd6469f4314d5f1ffec476e05f17cc9a78bc7a27a6a857842170bdf8d6f98d2f"
checksum = "feb41e78f93363bb2df8b0e86a2ca30eed7806ea16ea0c790d757cf93f79be83"
dependencies = [
"getrandom 0.2.3",
"serde",
@ -1692,43 +1692,100 @@ version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2"
dependencies = [
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_msvc",
"windows_aarch64_msvc 0.36.1",
"windows_i686_gnu 0.36.1",
"windows_i686_msvc 0.36.1",
"windows_x86_64_gnu 0.36.1",
"windows_x86_64_msvc 0.36.1",
]
[[package]]
name = "windows-sys"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc 0.42.0",
"windows_i686_gnu 0.42.0",
"windows_i686_msvc 0.42.0",
"windows_x86_64_gnu 0.42.0",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc 0.42.0",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e"
[[package]]
name = "windows_aarch64_msvc"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47"
[[package]]
name = "windows_aarch64_msvc"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4"
[[package]]
name = "windows_i686_gnu"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6"
[[package]]
name = "windows_i686_gnu"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7"
[[package]]
name = "windows_i686_msvc"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024"
[[package]]
name = "windows_i686_msvc"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246"
[[package]]
name = "windows_x86_64_gnu"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1"
[[package]]
name = "windows_x86_64_gnu"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028"
[[package]]
name = "windows_x86_64_msvc"
version = "0.36.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680"
[[package]]
name = "windows_x86_64_msvc"
version = "0.42.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5"
[[package]]
name = "winres"
version = "0.1.12"

View File

@ -29,7 +29,7 @@ serde = { version = "1.0.144", features = ["derive"] }
serde_json = "1.0.85"
bincode = "1.3.3"
serde_cbor = "0.11.2"
base64 = "0.13.0"
base64 = "0.13.1"
num-bigint = "0.4.3"
num-traits = "0.2.15"
chrono = { version = "0.4.20", features = ["serde"] }
@ -37,8 +37,8 @@ time = "0.3.14"
rand = { version = "0.8.5", package = "rand" }
rand-old = { package = "rand", version = "0.7.0" } # For ed25519-dalek
sqlite = "0.26.0"
uuid = { version = "1.1.2", features = ["serde", "v4"] }
mio = { version = "0.8.4", features = ["os-poll", "net"] }
uuid = { version = "1.2.1", features = ["serde", "v4"] }
mio = { version = "0.8.5", features = ["os-poll", "net"] }
ureq = { version = "2.5", optional = true }
lru = "0.8.1"
derive_more = "0.99.17"
@ -52,10 +52,10 @@ open = { version = "3.0.3", optional = true }
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.9", features = ["impl-default", "wincon", "shellscalingapi"] }
thread-priority = "0.9.2"
thread-priority = "0.10.0"
[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd"))'.dependencies]
thread-priority = "0.9.2"
thread-priority = "0.10.0"
[build-dependencies]
winres = "0.1.12"

View File

@ -430,7 +430,7 @@ impl Network {
peer.set_state(State::idle());
}
State::Idle { from } => {
debug!("Odd version of pings :)");
debug!("Odd version of pings for {}", peer.get_addr().ip());
if from.elapsed().as_secs() >= 30 {
let data: Vec<u8> = {
let c = self.context.lock().unwrap();
@ -470,7 +470,8 @@ impl Network {
let answer = match message {
Message::Hand { app_version, origin, version, public, rand_id } => {
if !version_compatible(&app_version) {
info!("Banning peer with version {}", &app_version);
let peer = self.peers.get_peer(token).unwrap();
info!("Banning peer with version {}, at {}", &app_version, peer.get_addr().ip());
return State::Banned;
}
if self.peers.is_our_own_connect(&rand_id) {
@ -505,7 +506,8 @@ impl Network {
return State::Twin;
}
if !version_compatible(&app_version) {
info!("Banning peer with version {}", &app_version);
let peer = self.peers.get_peer(token).unwrap();
info!("Banning peer with version {} at {}", &app_version, peer.get_addr().ip());
return State::Banned;
}
let nodes = self.peers.get_peers_active_count();
@ -893,7 +895,7 @@ fn send_message(connection: &mut TcpStream, data: &[u8]) -> io::Result<()> {
let mut buf: Vec<u8> = Vec::with_capacity(data.len() + 2);
buf.write_u16::<BigEndian>(data_len ^ 0xAAAA)?;
buf.write_all(data)?;
connection.write_all(&buf)?;
write_all(connection, &buf)?;
connection.flush()
}
@ -926,6 +928,29 @@ fn interrupted(err: &io::Error) -> bool {
err.kind() == io::ErrorKind::Interrupted
}
fn write_all(connection: &mut TcpStream, mut buf: &[u8]) -> io::Result<()> {
let start = Instant::now();
let timeout = Duration::from_secs(3);
let delay = Duration::from_millis(2);
while !buf.is_empty() {
match connection.write(buf) {
Ok(0) => {
return Err(io::Error::from(ErrorKind::WriteZero));
}
Ok(n) => buf = &buf[n..],
Err(ref e) if e.kind() == ErrorKind::Interrupted => thread::sleep(delay),
Err(e) => return Err(e),
}
if start.elapsed() > timeout {
warn!("Error writing data to {}", connection.peer_addr().unwrap());
return Err(io::Error::from(ErrorKind::BrokenPipe));
} else {
thread::sleep(delay);
}
}
Ok(())
}
fn version_compatible(version: &str) -> bool {
let my_version = env!("CARGO_PKG_VERSION");
let parts = my_version.split('.').collect::<Vec<&str>>();

View File

@ -141,6 +141,10 @@ impl Peer {
self.active && self.last_active.elapsed().as_secs() < 120
}
pub fn active_recently(&self) -> bool {
self.active && self.last_active.elapsed().as_secs() < 10
}
pub fn reconnects(&self) -> u32 {
self.reconnects
}

View File

@ -303,7 +303,7 @@ impl Peers {
let mut rng = rand::thread_rng();
match self.peers
.iter_mut()
.filter_map(|(token, peer)| if peer.is_lower(height) && peer.get_state().is_idle() && peer.get_sent_height() < height { Some((token, peer)) } else { None })
.filter_map(|(token, peer)| if peer.is_lower(height) && peer.get_state().is_idle() && !peer.active_recently() && peer.get_sent_height() < height { Some((token, peer)) } else { None })
.choose(&mut rng) {
None => {}
Some((token, peer)) => {