diff --git a/Cargo.lock b/Cargo.lock index d83fe80..a810276 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -197,7 +197,6 @@ dependencies = [ "chorus-macros", "chrono", "custom_error", - "futures-timer", "futures-util", "getrandom", "hostname", @@ -213,6 +212,7 @@ dependencies = [ "rustls", "rustls-native-certs", "rusty-hook", + "safina-timer", "serde", "serde-aux", "serde_json", @@ -624,12 +624,6 @@ version = "0.3.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" -[[package]] -name = "futures-timer" -version = "3.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e64b03909df88034c26dc1547e8970b91f98bdb65165d6a4e9110d94263dbb2c" - [[package]] name = "futures-util" version = "0.3.29" @@ -1714,6 +1708,15 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" +[[package]] +name = "safina-timer" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1081a264d1a3e81b75c4bcd5696094fb6ce470c2ded14cbd47bcb5229079b9df" +dependencies = [ + "once_cell", +] + [[package]] name = "schannel" version = "0.1.22" diff --git a/Cargo.toml b/Cargo.toml index 47b485a..df16dfc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,7 @@ sqlx = { version = "0.7.1", features = [ "runtime-tokio-native-tls", "any", ], optional = true } -futures-timer = "3.0.2" +safina-timer = "0.1.11" rand = "0.8.5" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/src/gateway.rs b/src/gateway.rs index edd402d..4229902 100644 --- a/src/gateway.rs +++ b/src/gateway.rs @@ -12,8 +12,7 @@ use std::any::Any; use std::collections::HashMap; use std::fmt::Debug; use std::sync::{Arc, RwLock}; -use std::time::Duration; -use tokio::time::sleep_until; +use std::time::{self, Duration}; use futures_util::stream::SplitSink; use futures_util::stream::SplitStream; @@ -25,8 +24,6 @@ use tokio::sync::mpsc::Sender; use tokio::sync::Mutex; use tokio::task; use tokio::task::JoinHandle; -use tokio::time; -use tokio::time::Instant; use tokio_tungstenite::MaybeTlsStream; use tokio_tungstenite::{connect_async_tls_with_config, Connector, WebSocketStream}; @@ -798,10 +795,12 @@ impl HeartbeatHandler { mut receive: tokio::sync::mpsc::Receiver, mut kill_receive: tokio::sync::broadcast::Receiver<()>, ) { - let mut last_heartbeat_timestamp: Instant = time::Instant::now(); + let mut last_heartbeat_timestamp: time::Instant = time::Instant::now(); let mut last_heartbeat_acknowledged = true; let mut last_seq_number: Option = None; + safina_timer::start_timer_thread(); + loop { if kill_receive.try_recv().is_ok() { trace!("GW: Closing heartbeat task"); @@ -818,7 +817,7 @@ impl HeartbeatHandler { let mut should_send = false; tokio::select! { - () = sleep_until(last_heartbeat_timestamp + timeout) => { + () = safina_timer::sleep_until(last_heartbeat_timestamp + timeout) => { should_send = true; } Some(communication) = receive.recv() => {