fix(offline): use hybrid strategy to reduce false positives
Implement 9/10 check strategy: - 9 out of 10 connectivity checks go to 1.1.1.1 (Cloudflare DNS) - 1 out of 10 checks go to Qobuz to verify service availability This prevents false offline detections when the app is making many concurrent API calls (e.g., loading genre hierarchies), which could trigger Qobuz rate limiting and cause timeout-based false positives. Also reduced timeout from 15s to 10s and retry wait from 2s to 1s since 1.1.1.1 responds much faster than Qobuz.
This commit is contained in:
1 parent
e86ceb827c
commit
aa04a26b6c
1 file changed
+43
-11
@@ -514,12 +514,22 @@ impl OfflineState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check network connectivity by attempting to reach Qobuz API.
|
/// Counter for alternating between neutral endpoint and Qobuz checks.
|
||||||
/// Uses a longer timeout (15s) and retries once before declaring offline
|
/// Using atomic for thread safety across async calls.
|
||||||
/// to avoid false positives from temporary latency spikes.
|
static CHECK_COUNTER: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0);
|
||||||
|
|
||||||
|
/// Check network connectivity using a hybrid strategy:
|
||||||
|
/// - 9 out of 10 checks go to a neutral endpoint (1.1.1.1) to verify basic internet
|
||||||
|
/// - 1 out of 10 checks go to Qobuz to verify service availability
|
||||||
|
///
|
||||||
|
/// This reduces load on Qobuz API and avoids false positives from rate limiting
|
||||||
|
/// when the app is making many concurrent API calls.
|
||||||
pub async fn check_network_connectivity() -> bool {
|
pub async fn check_network_connectivity() -> bool {
|
||||||
|
let counter = CHECK_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
|
||||||
|
let check_qobuz = counter % 10 == 0;
|
||||||
|
|
||||||
let client = reqwest::Client::builder()
|
let client = reqwest::Client::builder()
|
||||||
.timeout(Duration::from_secs(15))
|
.timeout(Duration::from_secs(10))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let client = match client {
|
let client = match client {
|
||||||
@@ -527,8 +537,35 @@ pub async fn check_network_connectivity() -> bool {
|
|||||||
Err(_) => return false,
|
Err(_) => return false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let endpoint = if check_qobuz {
|
||||||
|
"https://www.qobuz.com"
|
||||||
|
} else {
|
||||||
|
// Cloudflare DNS - highly reliable, low latency
|
||||||
|
"https://1.1.1.1"
|
||||||
|
};
|
||||||
|
|
||||||
// Try up to 2 times before declaring offline
|
// Try up to 2 times before declaring offline
|
||||||
for attempt in 1..=2 {
|
for attempt in 1..=2 {
|
||||||
|
match client.head(endpoint).send().await {
|
||||||
|
Ok(response) => {
|
||||||
|
if response.status().is_success() || response.status().is_redirection() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("Network check attempt {} to {} failed: {}", attempt, endpoint, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait 1 second before retry
|
||||||
|
if attempt < 2 {
|
||||||
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If neutral endpoint failed, try Qobuz as fallback (maybe it's a DNS issue)
|
||||||
|
if !check_qobuz {
|
||||||
|
log::info!("Neutral endpoint failed, trying Qobuz as fallback...");
|
||||||
match client.head("https://www.qobuz.com").send().await {
|
match client.head("https://www.qobuz.com").send().await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
if response.status().is_success() || response.status().is_redirection() {
|
if response.status().is_success() || response.status().is_redirection() {
|
||||||
@@ -536,17 +573,12 @@ pub async fn check_network_connectivity() -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::warn!("Network check attempt {} failed: {}", attempt, e);
|
log::warn!("Qobuz fallback check also failed: {}", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait 2 seconds before retry
|
|
||||||
if attempt < 2 {
|
|
||||||
tokio::time::sleep(Duration::from_secs(2)).await;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log::info!("Network connectivity check failed after 2 attempts");
|
log::info!("Network connectivity check failed after all attempts");
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user