Skip to content

Commit

Permalink
Merge pull request #1228 from eladyn/discovery_retry
Browse files Browse the repository at this point in the history
retry enabling discovery
  • Loading branch information
eladyn authored Mar 19, 2024
2 parents f595bd7 + 207abfb commit a779040
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
25 changes: 21 additions & 4 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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()
};

Expand Down

0 comments on commit a779040

Please sign in to comment.