check method

Future<NetworkReport> check({
  1. bool forceRefresh = false,
})

Performs a comprehensive network check.

To ensure efficiency, this method employs:

  1. Caching: Returns a recent report if within the cacheValidityMs window.
  2. Request Coalescing: If multiple callers trigger check simultaneously, only one network probe is executed, and its result is shared among all callers.
  • forceRefresh: If true, ignores the cache and forces a new network probe.

Returns a NetworkReport containing detailed statistics and security flags.

Implementation

Future<NetworkReport> check({bool forceRefresh = false}) async {
  // 1. Return cached report if still valid and not forcing refresh
  if (!forceRefresh &&
      _lastReport != null &&
      _lastReportTime != null &&
      DateTime.now().difference(_lastReportTime!).inMilliseconds <
          _config.cacheValidityMs.toInt()) {
    return _lastReport!;
  }

  // 2. Coalesce concurrent requests (Thundering Herd Protection)
  if (_pendingCheck != null) {
    return _pendingCheck!;
  }

  _pendingCheck = _performCheck();
  try {
    final report = await _pendingCheck!;
    return report;
  } finally {
    _pendingCheck = null;
  }
}