findSecondary method

  1. @override
Future<SecondaryAddress> findSecondary(
  1. String atSign, {
  2. Duration? timeout,
})
override

Finds the atServer address for atSign by looking it up in the atDirectory.

timeout bounds the TOTAL wall-clock spent doing so (connect + the internal retries + waiting for the atDirectory response). When null, the process-wide default (AtNetworkTimeouts.effectiveDefault, 30s) is used, and the value is always capped at AtNetworkTimeouts.maxAllowed (60s). On expiry the socket is closed and an AtTimeoutException is thrown.

Implementation

@override
Future<SecondaryAddress> findSecondary(String atSign,
    {Duration? timeout}) async {
  atSign = stripAtSignFromAtSign(atSign);

  // Convert the timeout into an absolute deadline once, then thread it down so
  // the retry loop, the connect and the response-wait all share ONE budget.
  final DateTime deadline = DateTime.now().add(
      AtNetworkTimeouts.cap(timeout ?? AtNetworkTimeouts.defaultTimeout));

  if (_cacheIsEmptyOrExpired(atSign)) {
    // _updateCache will either populate the cache, or throw an exception
    await _updateCache(atSign, _cacheDuration, deadline);
  }
  if (_map.containsKey(atSign)) {
    // should always be true, since _updateCache will throw an exception if it fails
    return _map[atSign]!.secondaryAddress;
  } else {
    // but just in case, we'll throw an exception if it's not
    throw Exception(
        "Failed to find atServer address for $atSign in atDirectory, in a theoretically impossible way");
  }
}