connect method
Connect attempts to connect to the peers passed in by peerStream. Will not connect to peers if they are within the backoff period. As Connect will attempt to dial peers as soon as it learns about them, the caller should try to keep the number, and rate, of inbound peers manageable.
Implementation
Future<void> connect(Stream<AddrInfo> peerStream) async {
await for (final pi in peerStream) {
if (pi.id == _host.id || pi.id.toString().isEmpty) {
continue;
}
var cachedPeer = _cache.get(pi.id);
final now = DateTime.now();
if (cachedPeer != null) {
if (now.isBefore(cachedPeer.nextTry)) {
continue;
}
cachedPeer.nextTry = now.add(cachedPeer.strat.delay());
} else {
cachedPeer = ConnCacheData(_backoff());
cachedPeer.nextTry = now.add(cachedPeer.strat.delay());
_cache.put(pi.id, cachedPeer);
}
// Connect to the peer
_connectToPeer(pi);
}
}