poll method
Adaptive polling loop.
Repeatedly calls check until it returns true, the startupTimeout
elapses, or check throws a non-transient exception.
Behaviour:
- Each iteration measures how long
checkitself took and sleeps only for the remainder of pollInterval (adaptive sleep to maintain per-interval cadence without busy-waiting). - TimeoutException and SocketException are always treated as
transient. Additional types can be added via
transientExceptions. - A
_StopIterationthrown insidecheckcauses polling to stop immediately and returnsfalse(used by ContainerStatusWaitStrategy). - Returns
trueas soon ascheckreturnstrue. - Returns
falseafter the deadline is exceeded without success.
Parameters:
check— async predicate that returnstruewhen the container is ready.transientExceptions— additional exception types to swallow during polling for this specific call.
Implementation
Future<bool> poll(
Future<bool> Function() check, {
List<Type>? transientExceptions,
}) async {
final allTransient = <Type>{
..._transientExceptions,
...?transientExceptions,
};
final deadline = DateTime.now().add(startupTimeout);
while (DateTime.now().isBefore(deadline)) {
final checkStart = DateTime.now();
try {
if (await check()) {
return true;
}
} on _StopIteration {
return false;
} catch (e) {
final isTransient = allTransient.any((t) => _isTransient(e, t));
if (!isTransient) {
rethrow;
}
}
final elapsed = DateTime.now().difference(checkStart);
final remaining = pollInterval - elapsed;
if (remaining > Duration.zero) {
await Future<void>.delayed(remaining);
}
}
return false;
}