Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making sure that the resolved DNS lists get used in full #3071

Merged
merged 4 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Making sure that the resolved DNS lists get used in full
Previously we would always process the list from the beginning. We would
only try maximumpeers elements from the list and never try more. This PR
firstly shuffles the peers list each time before applying it and then
also takes from the list for as long as there are free peers slots
available.

Signed-off-by: Jiri Peinlich <jiri.peinlich@gmail.com>
  • Loading branch information
gezero committed Nov 18, 2021
commit 3b22c1c4240c6c1cf8d6d59518c8623cf658fb8d
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -376,6 +377,7 @@ public Collection<PeerConnection> getPeers() {
public Stream<DiscoveryPeer> streamDiscoveredPeers() {
List<DiscoveryPeer> peers = dnsPeers.get();
if (peers != null) {
Collections.shuffle(peers);
return Stream.concat(peerDiscoveryAgent.streamDiscoveredPeers(), peers.stream());
}
return peerDiscoveryAgent.streamDiscoveredPeers();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can also shuffle this part ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, unfortunately streams don't have a convenient method to shuffle the elements.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shuffling a stream goes against the "lazy" nature of a stream I would say. To shuffle it you would need to process all elements at least once. So you need to collect, shuffle, and convert result to another stream.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,11 @@ public void connect(final Stream<? extends Peer> peerStream) {
if (!localNode.isReady()) {
return;
}
final int availablePeerSlots = Math.max(0, maxConnections - getConnectionCount());
peerStream
.takeWhile(peer -> Math.max(0, maxConnections - getConnectionCount()) > 0)
.filter(peer -> !connectionsById.containsKey(peer.getId()))
.filter(peer -> peer.getEnodeURL().isListening())
.filter(peerPermissions::allowNewOutboundConnectionTo)
.limit(availablePeerSlots)
.forEach(this::connect);
}

Expand Down