resolvePeerWithDns method

Future<String> resolvePeerWithDns()

Resolves url.host (timed) and records dns phase + peer address.

Used as a fallback when connect-time DNS was not captured (e.g. TrackedHttpClient wrapping a plain http.Client()). Skips DNS timing when:

  • HttpTimingPhase.dns was already recorded (e.g. via connect factory);
  • url.host is a literal IP address; or
  • the host is already in the process-wide IP cache (repeat requests in the same app session).

Implementation

Future<String> resolvePeerWithDns() async {
  if (_isIpAddress(url.host)) {
    applyPeer(address: url.host);
    return url.host;
  }

  if (_ipCache.containsKey(url.host)) {
    final cached = _ipCache[url.host]!;
    applyPeer(address: cached);
    return cached;
  }

  final alreadyHasDns = _completedPhases.contains(HttpTimingPhase.dns);
  final sw = Stopwatch()..start();
  try {
    final addresses = await InternetAddress.lookup(url.host);
    if (!alreadyHasDns) {
      recordPhase(HttpTimingPhase.dns, elapsedMs(sw));
    }
    final ip = addresses.isNotEmpty ? addresses.first.address : 'UNKNOWN';
    _ipCache[url.host] = ip;
    applyPeer(address: ip);
    return ip;
  } catch (_) {
    if (!alreadyHasDns) {
      recordPhase(HttpTimingPhase.dns, elapsedMs(sw));
    }
    applyPeer(address: 'UNKNOWN');
    return 'UNKNOWN';
  }
}