poll method

Future<bool> poll(
  1. Future<bool> check(), {
  2. List<Type>? transientExceptions,
})

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 check itself 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 _StopIteration thrown inside check causes polling to stop immediately and returns false (used by ContainerStatusWaitStrategy).
  • Returns true as soon as check returns true.
  • Returns false after the deadline is exceeded without success.

Parameters:

  • check — async predicate that returns true when 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;
}