import '../session/server_session.dart'; /// Probes one address and reports its session state with a reason. typedef ServerProbe = Future Function(Uri endpoint); /// Adapts a plain state check to a [ServerProbe] without reasons. ServerProbe probeFromCheck(Future Function(Uri) check) => (uri) async => ServerProbeResult(await check(uri)); /// Probe only the selected profile's explicit aliases, with a bounded wait. /// Never send a mutation or discover/switch logical servers. When every /// address is unreachable the first address and its reason are returned. Future<({Uri endpoint, SessionState state, String? reason})> probeServerAddresses( List addresses, { required ServerProbe probe, required bool Function() isCurrent, }) async { if (addresses.isEmpty) throw ArgumentError('No server addresses to probe.'); String? reason; for (final endpoint in addresses) { if (!isCurrent()) break; final result = await probe(endpoint).timeout( const Duration(seconds: 12), onTimeout: () => ServerProbeResult( SessionState.unreachable, reason: 'Could not reach ${endpoint.host}: no answer in time.', ), ); if (!isCurrent()) break; if (result.state != SessionState.unreachable) { return (endpoint: endpoint, state: result.state, reason: null); } reason ??= result.reason; } return ( endpoint: addresses.first, state: SessionState.unreachable, reason: reason, ); }