Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Pick the local server address at launch again when it answers instead of staying on remote

Barnabasb50c4a81888e
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index 0548fe1c..02549219 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -1140,7 +1140,7 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
final localReachable = await ApiService.pingServer(
auth.localServerUrl,
customHeaders: auth.customHeaders,
- ).timeout(const Duration(seconds: 3), onTimeout: () => false);
+ ).timeout(const Duration(seconds: 6), onTimeout: () => false);
if (localReachable) {
debugPrint('[Library] Local server ping succeeded — going online');
await auth.checkLocalServer();
@@ -1230,10 +1230,14 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
return;
}
- final reachable = await ApiService.pingServer(
+ final probe = await ApiService.pingServerDetailed(
auth.localServerUrl,
customHeaders: auth.customHeaders,
- ).timeout(const Duration(seconds: 3), onTimeout: () => false);
+ ).timeout(
+ const Duration(seconds: 6),
+ onTimeout: () => (ok: false, detail: 'no answer within 6s'),
+ );
+ final reachable = probe.ok;
if (auth.useLocalServer) {
if (reachable) {
@@ -1242,11 +1246,11 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
} else {
_localProbeFailures++;
if (_localProbeFailures >= _localProbeFailuresToFlip) {
- debugPrint('[Library] Local probe failed ${_localProbeFailures}x — switching to remote');
+ debugPrint('[Library] Local probe failed ${_localProbeFailures}x — switching to remote (${probe.detail})');
auth.clearLocalOverride();
_localProbeFailures = 0;
} else {
- debugPrint('[Library] Local probe miss $_localProbeFailures/$_localProbeFailuresToFlip');
+ debugPrint('[Library] Local probe miss $_localProbeFailures/$_localProbeFailuresToFlip (${probe.detail})');
}
}
} else if (reachable) {
@@ -1254,6 +1258,8 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
await auth.checkLocalServer();
_localLastReachableAt = DateTime.now();
_localProbeFailures = 0;
+ } else {
+ debugPrint('[Library] Local probe miss while on remote (${probe.detail})');
}
}
diff --git a/lib/providers/auth_provider.dart b/lib/providers/auth_provider.dart
index 46556f55..e0dae7f5 100644
--- a/lib/providers/auth_provider.dart
+++ b/lib/providers/auth_provider.dart
@@ -498,37 +498,53 @@ class AuthProvider extends ChangeNotifier {
// Load local server config
await _loadLocalServerSettings();
- // Check if server is actually reachable.
- // If local server is enabled and we're on WiFi, try local first
- // (lower latency) and only fall back to remote if local fails.
+ // Check if server is actually reachable. With a local address set,
+ // local and remote are pinged together: local answering inside two
+ // seconds wins outright, otherwise launch carries on with the remote
+ // answer and a local reply inside six seconds still switches the app
+ // over quietly. No WiFi gate any more - a local address can sit
+ // behind a VPN or Tailscale and answer on cellular, and a LAN address
+ // off its network just fails in the background at no cost to launch.
var reachable = false;
if (_localServerEnabled && _localServerUrl.isNotEmpty) {
final connectivity = await Connectivity().checkConnectivity();
- if (connectivity.contains(ConnectivityResult.wifi)) {
- debugPrint(
- '[Auth] On WiFi with local server enabled, trying local first... (${sw.elapsedMilliseconds}ms)',
- );
- final localReachable = await ApiService.pingServer(
- _localServerUrl,
- customHeaders: _customHeaders,
- ).timeout(const Duration(seconds: 2), onTimeout: () => false);
- if (localReachable) {
- debugPrint(
- '[Auth] Local server reachable - using local (${sw.elapsedMilliseconds}ms)',
- );
- _useLocalServer = true;
- reachable = true;
- }
- }
- }
- if (!reachable) {
debugPrint(
- '[Auth] pinging remote server... (${sw.elapsedMilliseconds}ms)',
+ '[Auth] Local server enabled, pinging local and remote together '
+ '(net=${connectivity.map((c) => c.name).join(',')}) (${sw.elapsedMilliseconds}ms)',
);
+ final localPing = ApiService.pingServer(
+ _localServerUrl,
+ customHeaders: _customHeaders,
+ ).timeout(const Duration(seconds: 6), onTimeout: () => false);
// Cap at 5s so a silently-dropping reverse proxy or dead network
// path doesn't hold up app launch. The health-check timer will
// re-probe every 60s once we're past startup, so a transient
// false-offline self-corrects quickly.
+ final remotePing = ApiService.pingServer(
+ restoredUrl,
+ customHeaders: _customHeaders,
+ ).timeout(const Duration(seconds: 5), onTimeout: () => false);
+ final localReachable = await localPing.timeout(
+ const Duration(seconds: 2),
+ onTimeout: () => false,
+ );
+ if (localReachable) {
+ debugPrint(
+ '[Auth] Local server reachable - using local (${sw.elapsedMilliseconds}ms)',
+ );
+ _useLocalServer = true;
+ reachable = true;
+ } else {
+ unawaited(localPing.then(_adoptLateLocalAnswer));
+ reachable = await remotePing;
+ debugPrint(
+ '[Auth] remote ping result: reachable=$reachable (${sw.elapsedMilliseconds}ms)',
+ );
+ }
+ } else {
+ debugPrint(
+ '[Auth] pinging remote server... (${sw.elapsedMilliseconds}ms)',
+ );
reachable = await ApiService.pingServer(
restoredUrl,
customHeaders: _customHeaders,
@@ -1056,6 +1072,21 @@ class AuthProvider extends ChangeNotifier {
}
}
+ /// The launch-time local ping answering after the two-second decision
+ /// already went to remote. Switch quietly; the library reloads on the
+ /// local address through the usual auth notification.
+ void _adoptLateLocalAnswer(bool reachable) {
+ if (!reachable || _useLocalServer) return;
+ if (!_localServerEnabled || _localServerUrl.isEmpty || _serverUrl == null) {
+ return;
+ }
+ _useLocalServer = true;
+ _serverReachable = true;
+ debugPrint('[Auth] Local server answered late - switching to local');
+ SocketService().switchServer(activeServerUrl!);
+ notifyListeners();
+ }
+
/// Check if the configured local server is reachable.
/// Called on WiFi connectivity changes by LibraryProvider.
Future<void> checkLocalServer() async {
@@ -1066,7 +1097,7 @@ class AuthProvider extends ChangeNotifier {
final reachable = await ApiService.pingServer(
_localServerUrl,
customHeaders: _customHeaders,
- ).timeout(const Duration(seconds: 3), onTimeout: () => false);
+ ).timeout(const Duration(seconds: 6), onTimeout: () => false);
_useLocalServer = reachable;
if (reachable) _serverReachable = true;
} catch (_) {