waitUntilReset method

Future<bool> waitUntilReset()

A utility function to wait until certain conditions related to rate limits are reset.

If the current state is not exceeded, the function will return immediately with false. Otherwise, it will delay the execution until the specified reset time and will return true.

The delay never exceeds _maxWait: a server-provided far-future resetAt cannot make this hang indefinitely. Negative or zero deltas (a reset time already in the past) resolve immediately.

Implementation

Future<bool> waitUntilReset() async {
  if (isNotExceeded) {
    //! No need to wait.
    return false;
  }

  //! Wait until rate limits are reset, but never longer than [_maxWait] so
  //! a hostile/misconfigured far-future `resetAt` cannot hang the caller.
  final remaining = resetAt.difference(DateTime.now().toUtc());
  final wait = remaining.isNegative
      ? Duration.zero
      : (remaining > _maxWait ? _maxWait : remaining);

  await Future.delayed(wait);

  return true;
}