fix(audio): delay retries

This commit is contained in:
Ashley Wulber 2025-03-04 11:26:35 -05:00 committed by Michael Murphy
parent 6399cc51af
commit 406cdd3627

View file

@ -1,7 +1,7 @@
// Copyright 2023 System76 <info@system76.com> // Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use std::{cell::RefCell, mem, rc::Rc, thread}; use std::{cell::RefCell, mem, rc::Rc, thread, time::Duration};
extern crate libpulse_binding as pulse; extern crate libpulse_binding as pulse;
@ -34,7 +34,7 @@ pub fn connect() -> iced::Subscription<Event> {
Subscription::run_with_id( Subscription::run_with_id(
std::any::TypeId::of::<SomeWorker>(), std::any::TypeId::of::<SomeWorker>(),
stream::channel(50, move |mut output| async move { stream::channel(50, move |mut output| async move {
let mut state = State::Connecting; let mut state = State::Connecting(0);
loop { loop {
state = start_listening(state, &mut output).await; state = start_listening(state, &mut output).await;
@ -49,7 +49,7 @@ async fn start_listening(
) -> State { ) -> State {
match state { match state {
// Waiting for Connection to succeed // Waiting for Connection to succeed
State::Connecting => { State::Connecting(mut disconnect_count) => {
let mut guard = FROM_PULSE.lock().await; let mut guard = FROM_PULSE.lock().await;
let (from_pulse, to_pulse) = { let (from_pulse, to_pulse) = {
if guard.is_none() { if guard.is_none() {
@ -70,17 +70,25 @@ async fn start_listening(
match from_pulse.recv().await { match from_pulse.recv().await {
Some(Message::Connected) => { Some(Message::Connected) => {
disconnect_count = 0;
_ = output.send(Event::Connected).await; _ = output.send(Event::Connected).await;
State::Connected State::Connected
} }
Some(Message::Disconnected) => { Some(Message::Disconnected) => {
disconnect_count += 1;
_ = output.send(Event::Disconnected).await; _ = output.send(Event::Disconnected).await;
tokio::time::sleep(Duration::from_millis(
State::Connecting 2_usize
.saturating_pow(disconnect_count.try_into().unwrap_or(u32::MAX))
.try_into()
.unwrap_or(u64::MAX),
))
.await;
State::Connecting(1)
} }
Some(m) => { Some(m) => {
tracing::error!("Unexpected message: {:?}", m); tracing::error!("Unexpected message: {:?}", m);
State::Connecting State::Connecting(1)
} }
None => { None => {
panic!("Pulse Sender dropped, something has gone wrong!"); panic!("Pulse Sender dropped, something has gone wrong!");
@ -90,7 +98,7 @@ async fn start_listening(
State::Connected => { State::Connected => {
let mut guard = FROM_PULSE.lock().await; let mut guard = FROM_PULSE.lock().await;
let Some((from_pulse, _)) = guard.as_mut() else { let Some((from_pulse, _)) = guard.as_mut() else {
return State::Connecting; return State::Connecting(1);
}; };
// This is where we match messages from the pulse server to pass to the gui // This is where we match messages from the pulse server to pass to the gui
match from_pulse.recv().await { match from_pulse.recv().await {
@ -121,11 +129,11 @@ async fn start_listening(
} }
Some(Message::Disconnected) => { Some(Message::Disconnected) => {
_ = output.send(Event::Disconnected).await; _ = output.send(Event::Disconnected).await;
State::Connecting State::Connecting(1)
} }
None => { None => {
_ = output.send(Event::Disconnected).await; _ = output.send(Event::Disconnected).await;
State::Connecting State::Connecting(1)
} }
_ => State::Connected, _ => State::Connected,
} }
@ -135,7 +143,7 @@ async fn start_listening(
// #[derive(Debug)] // #[derive(Debug)]
enum State { enum State {
Connecting, Connecting(usize),
Connected, Connected,
} }