reconnectBackoff function

Duration reconnectBackoff(
  1. int failures, {
  2. Duration initial = const Duration(seconds: 1),
  3. Duration max = const Duration(minutes: 1),
})

The exponential reconnect delay before connection attempt failures + 1, given failures consecutive failures so far (>= 1): initial * 2^(failures - 1), capped at max.

This is the deterministic base delay; Firehose adds random jitter on top so a fleet of instances that lost the same relay does not retry in lockstep.

Implementation

Duration reconnectBackoff(
  final int failures, {
  final Duration initial = const Duration(seconds: 1),
  final Duration max = const Duration(minutes: 1),
}) {
  // Cap the exponent so the shift can never overflow before `max` applies.
  final exponent = (failures - 1).clamp(0, 30);
  final delay = initial * (1 << exponent);

  return delay > max ? max : delay;
}