diff --git a/p2p/src/main/java/bisq/network/p2p/NetworkNodeProvider.java b/p2p/src/main/java/bisq/network/p2p/NetworkNodeProvider.java index 03652b970f..81c4107809 100644 --- a/p2p/src/main/java/bisq/network/p2p/NetworkNodeProvider.java +++ b/p2p/src/main/java/bisq/network/p2p/NetworkNodeProvider.java @@ -45,6 +45,7 @@ public class NetworkNodeProvider implements Provider { public NetworkNodeProvider(NetworkProtoResolver networkProtoResolver, BridgeAddressProvider bridgeAddressProvider, @Nullable NetworkFilter networkFilter, + @Named(Config.MAX_CONNECTIONS) int maxConnections, @Named(Config.USE_LOCALHOST_FOR_P2P) boolean useLocalhostForP2P, @Named(Config.NODE_PORT) int port, @Named(Config.TOR_DIR) File torDir, @@ -56,7 +57,7 @@ public class NetworkNodeProvider implements Provider { @Named(Config.TOR_STREAM_ISOLATION) boolean streamIsolation, @Named(Config.TOR_CONTROL_USE_SAFE_COOKIE_AUTH) boolean useSafeCookieAuthentication) { if (useLocalhostForP2P) { - networkNode = new LocalhostNetworkNode(port, networkProtoResolver, networkFilter); + networkNode = new LocalhostNetworkNode(port, networkProtoResolver, networkFilter, maxConnections); } else { TorMode torMode = getTorMode(bridgeAddressProvider, torDir, @@ -66,7 +67,7 @@ public class NetworkNodeProvider implements Provider { password, cookieFile, useSafeCookieAuthentication); - networkNode = new TorNetworkNode(port, networkProtoResolver, streamIsolation, torMode, networkFilter); + networkNode = new TorNetworkNode(port, networkProtoResolver, streamIsolation, torMode, networkFilter, maxConnections); } } diff --git a/p2p/src/main/java/bisq/network/p2p/network/LocalhostNetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/LocalhostNetworkNode.java index 97fce6e14f..e0251eccfc 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/LocalhostNetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/LocalhostNetworkNode.java @@ -56,8 +56,9 @@ public class LocalhostNetworkNode extends NetworkNode { public LocalhostNetworkNode(int port, NetworkProtoResolver networkProtoResolver, - @Nullable NetworkFilter networkFilter) { - super(port, networkProtoResolver, networkFilter); + @Nullable NetworkFilter networkFilter, + int maxConnections) { + super(port, networkProtoResolver, networkFilter, maxConnections); } @Override diff --git a/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java index d24bb03a16..7687104bda 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/NetworkNode.java @@ -72,6 +72,7 @@ public abstract class NetworkNode implements MessageListener { private final NetworkProtoResolver networkProtoResolver; @Nullable private final NetworkFilter networkFilter; + private final int maxConnections; private final CopyOnWriteArraySet inBoundConnections = new CopyOnWriteArraySet<>(); private final CopyOnWriteArraySet messageListeners = new CopyOnWriteArraySet<>(); @@ -92,10 +93,12 @@ public abstract class NetworkNode implements MessageListener { NetworkNode(int servicePort, NetworkProtoResolver networkProtoResolver, - @Nullable NetworkFilter networkFilter) { + @Nullable NetworkFilter networkFilter, + int maxConnections) { this.servicePort = servicePort; this.networkProtoResolver = networkProtoResolver; this.networkFilter = networkFilter; + this.maxConnections = maxConnections; } /////////////////////////////////////////////////////////////////////////////////////////// @@ -437,7 +440,10 @@ public abstract class NetworkNode implements MessageListener { void createExecutorService() { if (executorService == null) - executorService = Utilities.getListeningExecutorService("NetworkNode-" + servicePort, 15, 30, 60); + executorService = Utilities.getListeningExecutorService("NetworkNode-" + servicePort, + maxConnections * 2, + maxConnections * 4, + 60); } void startServer(ServerSocket serverSocket) { diff --git a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java index d070304e77..14fe3a0f7b 100644 --- a/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java +++ b/p2p/src/main/java/bisq/network/p2p/network/TorNetworkNode.java @@ -78,8 +78,9 @@ public class TorNetworkNode extends NetworkNode { NetworkProtoResolver networkProtoResolver, boolean useStreamIsolation, TorMode torMode, - @Nullable NetworkFilter networkFilter) { - super(servicePort, networkProtoResolver, networkFilter); + @Nullable NetworkFilter networkFilter, + int maxConnections) { + super(servicePort, networkProtoResolver, networkFilter, maxConnections); this.torMode = torMode; this.streamIsolation = useStreamIsolation; createExecutorService(); diff --git a/p2p/src/test/java/bisq/network/p2p/network/LocalhostNetworkNodeTest.java b/p2p/src/test/java/bisq/network/p2p/network/LocalhostNetworkNodeTest.java index 8315332c34..e44c6da311 100644 --- a/p2p/src/test/java/bisq/network/p2p/network/LocalhostNetworkNodeTest.java +++ b/p2p/src/test/java/bisq/network/p2p/network/LocalhostNetworkNodeTest.java @@ -41,7 +41,7 @@ public class LocalhostNetworkNodeTest { @Test public void testMessage() throws InterruptedException, IOException { CountDownLatch msgLatch = new CountDownLatch(2); - LocalhostNetworkNode node1 = new LocalhostNetworkNode(9001, TestUtils.getNetworkProtoResolver(), null); + LocalhostNetworkNode node1 = new LocalhostNetworkNode(9001, TestUtils.getNetworkProtoResolver(), null, 12); node1.addMessageListener((message, connection) -> { log.debug("onMessage node1 " + message); msgLatch.countDown(); @@ -69,7 +69,7 @@ public class LocalhostNetworkNodeTest { } }); - LocalhostNetworkNode node2 = new LocalhostNetworkNode(9002, TestUtils.getNetworkProtoResolver(), null); + LocalhostNetworkNode node2 = new LocalhostNetworkNode(9002, TestUtils.getNetworkProtoResolver(), null, 12); node2.addMessageListener((message, connection) -> { log.debug("onMessage node2 " + message); msgLatch.countDown(); diff --git a/p2p/src/test/java/bisq/network/p2p/network/TorNetworkNodeTest.java b/p2p/src/test/java/bisq/network/p2p/network/TorNetworkNodeTest.java index 33bc3d7912..ef20da5d42 100644 --- a/p2p/src/test/java/bisq/network/p2p/network/TorNetworkNodeTest.java +++ b/p2p/src/test/java/bisq/network/p2p/network/TorNetworkNodeTest.java @@ -56,7 +56,7 @@ public class TorNetworkNodeTest { latch = new CountDownLatch(1); int port = 9001; TorNetworkNode node1 = new TorNetworkNode(port, TestUtils.getNetworkProtoResolver(), false, - new NewTor(new File("torNode_" + port), null, "", this::getBridgeAddresses), null); + new NewTor(new File("torNode_" + port), null, "", this::getBridgeAddresses), null, 12); node1.start(new SetupListener() { @Override public void onTorNodeReady() { @@ -83,7 +83,7 @@ public class TorNetworkNodeTest { latch = new CountDownLatch(1); int port2 = 9002; TorNetworkNode node2 = new TorNetworkNode(port2, TestUtils.getNetworkProtoResolver(), false, - new NewTor(new File("torNode_" + port), null, "", this::getBridgeAddresses), null); + new NewTor(new File("torNode_" + port), null, "", this::getBridgeAddresses), null, 12); node2.start(new SetupListener() { @Override public void onTorNodeReady() { @@ -141,7 +141,7 @@ public class TorNetworkNodeTest { latch = new CountDownLatch(2); int port = 9001; TorNetworkNode node1 = new TorNetworkNode(port, TestUtils.getNetworkProtoResolver(), false, - new NewTor(new File("torNode_" + port), null, "", this::getBridgeAddresses), null); + new NewTor(new File("torNode_" + port), null, "", this::getBridgeAddresses), null, 12); node1.start(new SetupListener() { @Override public void onTorNodeReady() { @@ -167,7 +167,7 @@ public class TorNetworkNodeTest { int port2 = 9002; TorNetworkNode node2 = new TorNetworkNode(port2, TestUtils.getNetworkProtoResolver(), false, - new NewTor(new File("torNode_" + port), null, "", this::getBridgeAddresses), null); + new NewTor(new File("torNode_" + port), null, "", this::getBridgeAddresses), null, 12); node2.start(new SetupListener() { @Override public void onTorNodeReady() {