diff --git a/crates/shadowsocks-service/src/config.rs b/crates/shadowsocks-service/src/config.rs index 21539dda..1d80b64a 100644 --- a/crates/shadowsocks-service/src/config.rs +++ b/crates/shadowsocks-service/src/config.rs @@ -1233,12 +1233,12 @@ impl Config { if svr.tcp_weight.is_some() || svr.udp_weight.is_some() { let tcp_weight = svr.tcp_weight.unwrap_or(1.0); - if tcp_weight < 0.0 || tcp_weight > 1.0 { + if !(0.0..=1.0).contains(&tcp_weight) { let err = Error::new(ErrorKind::Invalid, "invalid `tcp_weight`, must be in [0, 1]", None); return Err(err); } let udp_weight = svr.udp_weight.unwrap_or(1.0); - if udp_weight < 0.0 || udp_weight > 1.0 { + if !(0.0..=1.0).contains(&udp_weight) { let err = Error::new(ErrorKind::Invalid, "invalid `udp_weight`, must be in [0, 1]", None); return Err(err); } @@ -1753,12 +1753,12 @@ impl fmt::Display for Config { remarks: svr.remarks().map(ToOwned::to_owned), id: svr.id().map(ToOwned::to_owned), mode: Some(svr.mode().to_string()), - tcp_weight: if svr.weight().tcp_weight() != 1.0 { + tcp_weight: if (svr.weight().tcp_weight() - 1.0).abs() > f32::EPSILON { Some(svr.weight().tcp_weight()) } else { None }, - udp_weight: if svr.weight().udp_weight() != 1.0 { + udp_weight: if (svr.weight().udp_weight() - 1.0).abs() > f32::EPSILON { Some(svr.weight().udp_weight()) } else { None diff --git a/crates/shadowsocks-service/src/local/loadbalancing/server_stat.rs b/crates/shadowsocks-service/src/local/loadbalancing/server_stat.rs index a71ed56b..af71a72a 100644 --- a/crates/shadowsocks-service/src/local/loadbalancing/server_stat.rs +++ b/crates/shadowsocks-service/src/local/loadbalancing/server_stat.rs @@ -50,7 +50,7 @@ fn max_latency_stdev() -> f64 { impl ServerStat { pub fn new(user_weight: f32) -> ServerStat { - assert!(user_weight >= 0.0 && user_weight <= 1.0); + assert!((0.0..=1.0).contains(&user_weight)); ServerStat { rtt: MAX_SERVER_RTT, diff --git a/crates/shadowsocks/src/config.rs b/crates/shadowsocks/src/config.rs index 44b3448a..b5ff91e3 100644 --- a/crates/shadowsocks/src/config.rs +++ b/crates/shadowsocks/src/config.rs @@ -128,7 +128,7 @@ impl ServerWeight { /// Set weight for TCP balancer in `[0, 1]` pub fn set_tcp_weight(&mut self, weight: f32) { - assert!(weight >= 0.0 && weight <= 1.0); + assert!((0.0..=1.0).contains(&weight)); self.tcp_weight = weight; } @@ -139,7 +139,7 @@ impl ServerWeight { /// Set weight for UDP balancer in `[0, 1]` pub fn set_udp_weight(&mut self, weight: f32) { - assert!(weight >= 0.0 && weight <= 1.0); + assert!((0.0..=1.0).contains(&weight)); self.udp_weight = weight; } }