update voice heartbeat, fix the new test issue

This commit is contained in:
kozabrada123 2024-01-19 19:42:24 +01:00
parent 3fdb258d54
commit 5a290c0665
4 changed files with 34 additions and 26 deletions

10
Cargo.lock generated
View File

@ -233,7 +233,6 @@ dependencies = [
"reqwest",
"rustls",
"rustls-native-certs",
"safina-timer",
"serde",
"serde-aux",
"serde_json",
@ -1801,15 +1800,6 @@ version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
[[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 = "salsa20"
version = "0.10.2"

View File

@ -11,7 +11,7 @@ website = ["https://discord.com/invite/m3FpcapGDD"]
[features]
default = ["client", "rt-multi-thread"]
default = ["client", "rt-multi-thread", "voice"]
backend = ["dep:poem", "dep:sqlx"]
rt-multi-thread = ["tokio/rt-multi-thread"]
rt = ["tokio/rt"]
@ -57,7 +57,6 @@ sqlx = { version = "0.7.3", features = [
], optional = true }
discortp = { version = "0.5.0", optional = true, features = ["rtp", "discord", "demux"] }
crypto_secretbox = {version = "0.1.1", optional = true}
safina-timer = "0.1.11"
rand = "0.8.5"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]

View File

@ -1,12 +1,25 @@
use std::{
sync::Arc,
time::{Duration, Instant},
};
use futures_util::SinkExt;
use log::*;
use safina_timer::sleep_until;
use tokio::sync::{mpsc::Sender, Mutex};
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep_until;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep_until;
use std::{sync::Arc, time::Duration};
use tokio::sync::{
mpsc::{Receiver, Sender},
Mutex,
};
#[cfg(not(target_arch = "wasm32"))]
use tokio::task;
use crate::{
gateway::heartbeat::HEARTBEAT_ACK_TIMEOUT,
@ -37,7 +50,7 @@ impl VoiceHeartbeatHandler {
let kill_receive = kill_rc.resubscribe();
#[cfg(not(target_arch = "wasm32"))]
tokio::task::spawn(async move {
task::spawn(async move {
Self::heartbeat_task(
websocket_tx,
heartbeat_interval,
@ -73,15 +86,13 @@ impl VoiceHeartbeatHandler {
websocket_tx: Arc<Mutex<Sink>>,
heartbeat_interval: Duration,
starting_nonce: u64,
mut receive: tokio::sync::mpsc::Receiver<VoiceHeartbeatThreadCommunication>,
mut receive: Receiver<VoiceHeartbeatThreadCommunication>,
mut kill_receive: tokio::sync::broadcast::Receiver<()>,
) {
let mut last_heartbeat_timestamp: Instant = Instant::now();
let mut last_heartbeat_acknowledged = true;
let mut nonce: u64 = starting_nonce;
safina_timer::start_timer_thread();
loop {
if kill_receive.try_recv().is_ok() {
trace!("VGW: Closing heartbeat task");

View File

@ -12,6 +12,16 @@ use wasm_bindgen_test::*;
#[cfg(target_arch = "wasm32")]
wasm_bindgen_test_configure!(run_in_browser);
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::Instant;
#[cfg(target_arch = "wasm32")]
use wasmtimer::std::Instant;
#[cfg(not(target_arch = "wasm32"))]
use tokio::time::sleep;
#[cfg(target_arch = "wasm32")]
use wasmtimer::tokio::sleep;
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
/// Tests establishing a connection (hello and heartbeats) on the local gateway;
@ -61,11 +71,9 @@ async fn test_gateway_authenticate() {
gateway.send_identify(identify).await;
let current_time = std::time::Instant::now();
tokio::select! {
// Fail, we timed out waiting for it
() = safina_timer::sleep_until(current_time + Duration::from_secs(20)) => {
() = sleep(Duration::from_secs(20)) => {
println!("Timed out waiting for event, failing..");
assert!(false);
}