From e4f99a9560e6e3815c4c5ef6cf4e733becf1b1eb Mon Sep 17 00:00:00 2001 From: eladyn Date: Sun, 15 Oct 2023 01:37:56 +0200 Subject: [PATCH 1/2] retry enabling discovery instead of unwrap --- src/setup.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/setup.rs b/src/setup.rs index e2538cf8..07a713c3 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -15,7 +15,7 @@ use librespot_playback::{ }; #[allow(unused_imports)] // cfg use log::{debug, error, info, warn}; -use std::str::FromStr; +use std::{str::FromStr, thread, time::Duration}; pub(crate) fn initial_state(config: config::SpotifydConfig) -> main_loop::MainLoop { let mixer = { @@ -96,13 +96,30 @@ pub(crate) fn initial_state(config: config::SpotifydConfig) -> main_loop::MainLo } else { info!("no usable credentials found, enabling discovery"); debug!("Using device id '{}'", session_config.device_id); - let discovery_stream = - librespot_discovery::Discovery::builder(session_config.device_id.clone()) + const RETRY_MAX: u8 = 4; + let mut retry_counter = 0; + let mut backoff = Duration::from_secs(5); + let discovery_stream = loop { + match librespot_discovery::Discovery::builder(session_config.device_id.clone()) .name(config.device_name.clone()) .device_type(device_type) .port(zeroconf_port) .launch() - .unwrap(); + { + Ok(discovery_stream) => break discovery_stream, + Err(err) => { + error!("failed to enable discovery: {err}"); + if retry_counter >= RETRY_MAX { + panic!("failed to enable discovery (and no credentials provided)"); + } + info!("retrying discovery in {} seconds", backoff.as_secs()); + thread::sleep(backoff); + retry_counter += 1; + backoff *= 2; + info!("trying to enable discovery (retry {retry_counter}/{RETRY_MAX})"); + } + } + }; discovery_stream.into() }; From 207abfb2789f3cf8568f50a31027b5dd07eb66a2 Mon Sep 17 00:00:00 2001 From: eladyn Date: Sun, 15 Oct 2023 01:39:20 +0200 Subject: [PATCH 2/2] update changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e521ed0..a8081550 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,10 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- retry enabling discovery several times before exiting ([#1228]) + ### Changed - Credential caching has been re-enabled. ([#1214]) [#1214]: https://github.com/Spotifyd/spotifyd/pull/1214 +[#1228]: https://github.com/Spotifyd/spotifyd/pull/1228 ## [0.3.5]