startWatchdog static method

void startWatchdog()

Start a timer that notices when the event loop of this isolate was blocked longer than the limit, also after an await. It can not tell which code did it. The server calls it when it listens.

Implementation

static void startWatchdog() {
  if (!enabled || _watchdog != null) return;
  Duration interval = _limit < minWatchdogInterval
      ? minWatchdogInterval
      : _limit;
  int limit = _limit.inMilliseconds;
  Stopwatch clock = Stopwatch()..start();
  int last = 0;
  int quietUntil = 0;
  _watchdog = Timer.periodic(interval, (Timer timer) {
    int now = clock.elapsedMilliseconds;
    int gap = now - last;
    last = now;
    // ponytail: the check runs once per interval, so a block between
    // one and two limits long may be missed. The route check above is
    // the exact one.
    if (gap - interval.inMilliseconds <= limit || now < quietUntil) return;
    quietUntil = now + watchdogQuiet.inMilliseconds;
    _warn(
      'The event loop of isolate ${Dox().isolateId} was blocked for about '
      '$gap ms (limit $limit ms). Some code ran that long without an '
      'await, so other requests had to wait. Move heavy CPU work to '
      'await Dox().compute(...).',
    );
  });
}