Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop retrying settings sync and the home server check so often while offline

Barnabase401f20c4263
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index 60e98dfe..2bca6700 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -1209,6 +1209,16 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
static const _localProbeFailuresToFlip = 2;
static const _localProbePiggybackWindow = Duration(seconds: 25);
+ // Away from home the local server never answers, and a ping every 30s all
+ // day on mobile data (or with no signal at all) is pure waste. After two
+ // minutes of misses the checks spread out to at most every 5 minutes. A
+ // network change starts the fast checks again, so joining home WiFi still
+ // switches over straight away.
+ int _remoteProbeMisses = 0;
+ DateTime? _nextRemoteProbeAt;
+ static const _remoteProbeFastMisses = 4;
+ static const _remoteProbeMaxGap = Duration(minutes: 5);
+
void _startLocalProbeTimer() {
final auth = _auth;
if (auth == null) return;
@@ -1222,6 +1232,8 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
_stopLocalProbeTimer();
return;
}
+ _remoteProbeMisses = 0;
+ _nextRemoteProbeAt = null;
if (_localProbeTimer != null) return;
_localProbeFailures = 0;
_localProbeTimer = Timer.periodic(_localProbeInterval, (_) => _probeLocalServer());
@@ -1259,6 +1271,12 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
DateTime.now().difference(last) < _localProbePiggybackWindow) {
return;
}
+ final nextRemote = _nextRemoteProbeAt;
+ if (!auth.useLocalServer &&
+ nextRemote != null &&
+ DateTime.now().isBefore(nextRemote)) {
+ return;
+ }
final probe = await ApiService.pingServerDetailed(
auth.localServerUrl,
@@ -1288,8 +1306,20 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
await auth.checkLocalServer();
_localLastReachableAt = DateTime.now();
_localProbeFailures = 0;
+ _remoteProbeMisses = 0;
+ _nextRemoteProbeAt = null;
} else {
- debugPrint('[Library] Local probe miss while on remote (${probe.detail})');
+ _remoteProbeMisses++;
+ if (_remoteProbeMisses > _remoteProbeFastMisses) {
+ final steps = _remoteProbeMisses - _remoteProbeFastMisses;
+ final gap = _localProbeInterval * (1 << steps.clamp(0, 4));
+ final wait = gap < _remoteProbeMaxGap ? gap : _remoteProbeMaxGap;
+ _nextRemoteProbeAt = DateTime.now().add(wait);
+ debugPrint(
+ '[Library] Local probe miss while on remote (${probe.detail}), next in ${wait.inSeconds}s');
+ } else {
+ debugPrint('[Library] Local probe miss while on remote (${probe.detail})');
+ }
}
}
diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart
index e2926df9..b96bdcec 100644
--- a/lib/screens/settings_screen.dart
+++ b/lib/screens/settings_screen.dart
@@ -4744,7 +4744,8 @@ class _SettingsScreenState extends State<SettingsScreen> {
_syncUploading = true;
HapticFeedback.mediumImpact();
try {
- final result = await SettingsSyncService().pushIfChanged();
+ final result =
+ await SettingsSyncService().pushIfChanged(ignoreBackoff: true);
if (!mounted) return;
await _refreshSyncStatus();
if (!mounted) return;
diff --git a/lib/services/settings_sync_service.dart b/lib/services/settings_sync_service.dart
index 8beac694..5fb9aa9a 100644
--- a/lib/services/settings_sync_service.dart
+++ b/lib/services/settings_sync_service.dart
@@ -180,6 +180,31 @@ class SettingsSyncService {
bool _busy = false;
DateTime? _lastPullAt;
+ /// With the folder unreachable (no signal, a network that blocks it) the
+ /// change check used to retry every 45 seconds for as long as the app
+ /// stayed alive. Automatic pushes now wait longer after each failure; the
+ /// manual Sync button and coming back to the app still try right away.
+ int _networkFailures = 0;
+ DateTime? _retryAfter;
+ static const _maxRetryDelay = Duration(minutes: 30);
+
+ void _noteNetworkFailure() {
+ _networkFailures++;
+ final minutes = 1 << (_networkFailures - 1).clamp(0, 5);
+ final delay = Duration(minutes: minutes) < _maxRetryDelay
+ ? Duration(minutes: minutes)
+ : _maxRetryDelay;
+ _retryAfter = DateTime.now().add(delay);
+ debugPrint(
+ '[SettingsSync] folder unreachable ${_networkFailures}x - next automatic try in ${delay.inMinutes}m',
+ );
+ }
+
+ void _noteReachable() {
+ _networkFailures = 0;
+ _retryAfter = null;
+ }
+
/// Applying a remote payload writes every setting locally, which fires the
/// same change notification a user edit does. Left alone that pushes the
/// settings straight back up with a fresh timestamp, the other device pulls
@@ -383,6 +408,8 @@ class SettingsSyncService {
return;
}
_lastPullAt = now;
+ // Coming back to the app is a natural moment to try again.
+ _retryAfter = null;
// Send anything this device is still holding BEFORE taking the remote copy.
// A change made just before the app was swiped away never got its push -
// the process was gone - so it is sitting here unsent. Pulling first would
@@ -591,8 +618,14 @@ class SettingsSyncService {
/// on the device until some unrelated toggle happened to move. Comparing a
/// hash of the payload catches it without making dozens of setters notify
/// and rebuild the UI for no reason.
- Future<SyncResult> pushIfChanged() async {
+ Future<SyncResult> pushIfChanged({bool ignoreBackoff = false}) async {
if (!await getEnabled()) return const SyncResult(SyncStatus.notConfigured);
+ final retryAfter = _retryAfter;
+ if (!ignoreBackoff &&
+ retryAfter != null &&
+ DateTime.now().isBefore(retryAfter)) {
+ return const SyncResult(SyncStatus.network, 'waiting to retry');
+ }
try {
final hash = _hash(await buildPayload());
if (hash == await _lastHash()) {
@@ -730,6 +763,7 @@ class SettingsSyncService {
return SyncResult(SyncStatus.authFailed, null, _http(resp, uri, 'PUT'));
}
if (resp.statusCode >= 200 && resp.statusCode < 300) {
+ _noteReachable();
await _setLastSeen(stamp);
await _setLastHash(_hash(payload));
debugPrint(
@@ -741,6 +775,7 @@ class SettingsSyncService {
return SyncResult(SyncStatus.network, null, _http(resp, uri, 'PUT'));
} catch (e) {
debugPrint('[SettingsSync] push failed: ${e.runtimeType} - $e');
+ _noteNetworkFailure();
return SyncResult(SyncStatus.network, null, _err(e, uri, 'PUT'));
} finally {
_busy = false;