Do pass max connections to `PeerManager`, new constructor signature

Remove calls to now instance private ``PeerManager.setMaxConnections()``.  Static fields regarding connection limits are now private instance fields.
This commit is contained in:
Ivan Vilata-i-Balaguer 2016-05-06 10:25:00 +02:00
parent dc90a950cb
commit fd37b8999b
5 changed files with 10 additions and 15 deletions

View File

@ -150,7 +150,7 @@ public class P2PService implements SetupListener, MessageListener, ConnectionLis
networkNode.addMessageListener(this);
Set<NodeAddress> seedNodeAddresses = seedNodesRepository.getSeedNodeAddresses(useLocalhost, networkId);
peerManager = new PeerManager(networkNode, seedNodeAddresses, storageDir, clock);
peerManager = new PeerManager(networkNode, maxConnections, seedNodeAddresses, storageDir, clock);
broadcaster = new Broadcaster(networkNode, peerManager);

View File

@ -28,17 +28,17 @@ public class PeerManager implements ConnectionListener {
// Use a long delay as the bootstrapping peer might need a while until it knows its onion address
private static final long REMOVE_ANONYMOUS_PEER_SEC = Timer.STRESS_TEST ? 10 : 120;
private static int MAX_CONNECTIONS;
private static int MIN_CONNECTIONS;
private static int MAX_CONNECTIONS_PEER;
private static int MAX_CONNECTIONS_NON_DIRECT;
private int MAX_CONNECTIONS;
private int MIN_CONNECTIONS;
private int MAX_CONNECTIONS_PEER;
private int MAX_CONNECTIONS_NON_DIRECT;
private static int MAX_CONNECTIONS_ABSOLUTE;
private int MAX_CONNECTIONS_ABSOLUTE;
private final boolean printReportedPeersDetails = true;
private boolean lostAllConnections;
public static void setMaxConnections(int maxConnections) {
private void setMaxConnections(int maxConnections) {
MAX_CONNECTIONS = maxConnections;
MIN_CONNECTIONS = Math.max(1, maxConnections - 4);
MAX_CONNECTIONS_PEER = MAX_CONNECTIONS + 4;
@ -46,10 +46,6 @@ public class PeerManager implements ConnectionListener {
MAX_CONNECTIONS_ABSOLUTE = MAX_CONNECTIONS + 18;
}
static {
setMaxConnections(12);
}
private static final int MAX_REPORTED_PEERS = 1000;
private static final int MAX_PERSISTED_PEERS = 500;
private static final long MAX_AGE = TimeUnit.DAYS.toMillis(14); // max age for reported peers is 14 days
@ -90,8 +86,10 @@ public class PeerManager implements ConnectionListener {
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
public PeerManager(NetworkNode networkNode, Set<NodeAddress> seedNodeAddresses, File storageDir, Clock clock) {
public PeerManager(NetworkNode networkNode, int maxConnections, Set<NodeAddress> seedNodeAddresses,
File storageDir, Clock clock) {
this.networkNode = networkNode;
setMaxConnections(maxConnections);
this.clock = clock;
// seedNodeAddresses can be empty (in case there is only 1 seed node, the seed node starting up has no other seed nodes)
this.seedNodeAddresses = new HashSet<>(seedNodeAddresses);

View File

@ -71,7 +71,6 @@ public class SeedNode {
maxConnections = Integer.parseInt(arg2);
log.info("From processArgs: maxConnections=" + maxConnections);
checkArgument(maxConnections < MAX_CONNECTIONS_LIMIT, "maxConnections seems to be a bit too high...");
PeerManager.setMaxConnections(maxConnections);
}
if (args.length > 3) {
String arg3 = args[3];

View File

@ -38,7 +38,6 @@ public class PeerServiceTest {
public void setup() throws InterruptedException {
LocalhostNetworkNode.setSimulateTorDelayTorNode(50);
LocalhostNetworkNode.setSimulateTorDelayHiddenService(8);
PeerManager.setMaxConnections(MAX_CONNECTIONS);
if (useLocalhost) {
seedNodeAddresses.add(new NodeAddress("localhost:8001"));

View File

@ -38,7 +38,6 @@ public class PeerManagerTest {
public void setup() throws InterruptedException {
LocalhostNetworkNode.setSimulateTorDelayTorNode(50);
LocalhostNetworkNode.setSimulateTorDelayHiddenService(8);
PeerManager.setMaxConnections(MAX_CONNECTIONS);
seedNodes = new HashSet<>();
if (useLocalhost) {