From 3ebbe21da698905c32ad58f03d70dfe93764e25d Mon Sep 17 00:00:00 2001 From: PhilipRoman Date: Wed, 22 Apr 2020 11:18:43 +0300 Subject: [PATCH 1/2] Allow user to specify max number of pending connections to a server Adds a field and two new methods to WebSocketServer: setMaxPendingConnections(int) and getMaxPendingConnections() and uses the value as a parameter when binding the server socket. --- .../server/WebSocketServer.java | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/java_websocket/server/WebSocketServer.java b/src/main/java/org/java_websocket/server/WebSocketServer.java index 32f3fdba..961eda28 100644 --- a/src/main/java/org/java_websocket/server/WebSocketServer.java +++ b/src/main/java/org/java_websocket/server/WebSocketServer.java @@ -26,9 +26,7 @@ package org.java_websocket.server; import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.net.Socket; +import java.net.*; import java.nio.ByteBuffer; import java.nio.channels.CancelledKeyException; import java.nio.channels.ClosedByInterruptException; @@ -108,6 +106,12 @@ public abstract class WebSocketServer extends AbstractWebSocket implements Runna private WebSocketServerFactory wsf = new DefaultWebSocketServerFactory(); + /** + * Attribute which allows you to configure the socket "backlog" parameter + * which determines how many client connections can be queued. + */ + private int maxPendingConnections = -1; + /** * Creates a WebSocketServer that will attempt to * listen on port WebSocketImpl.DEFAULT_PORT. @@ -308,6 +312,24 @@ public List getDraft() { return Collections.unmodifiableList( drafts ); } + /** + * Set the requested maximum number of pending connections on the socket. The exact semantics are implementation + * specific. The value provided should be greater than 0. If it is less than or equal to 0, then + * an implementation specific default will be used. This option will be passed as "backlog" parameter to {@link ServerSocket#bind(SocketAddress, int)} + */ + public void setMaxPendingConnections(int numberOfConnections) { + maxPendingConnections = numberOfConnections; + } + + /** + * Returns the currently configured maximum number of pending connections. + * + * @see #setMaxPendingConnections(int) + */ + public int getMaxPendingConnections() { + return maxPendingConnections; + } + // Runnable IMPLEMENTATION ///////////////////////////////////////////////// public void run() { if (!doEnsureSingleThread()) { @@ -505,7 +527,7 @@ private boolean doSetupSelectorAndServerThread() { ServerSocket socket = server.socket(); socket.setReceiveBufferSize( WebSocketImpl.RCVBUF ); socket.setReuseAddress( isReuseAddr() ); - socket.bind( address ); + socket.bind( address, getMaxPendingConnections() ); selector = Selector.open(); server.register( selector, server.validOps() ); startConnectionLostTimer(); From ca38a4b13bc424b9430f3ad812c7c9d57eb6b0a9 Mon Sep 17 00:00:00 2001 From: Philip John Roman <33231208+PhilipRoman@users.noreply.github.com> Date: Wed, 22 Apr 2020 14:48:55 +0300 Subject: [PATCH 2/2] Add "since 1.5.0" tag to new methods --- src/main/java/org/java_websocket/server/WebSocketServer.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/java_websocket/server/WebSocketServer.java b/src/main/java/org/java_websocket/server/WebSocketServer.java index 961eda28..f7349a6d 100644 --- a/src/main/java/org/java_websocket/server/WebSocketServer.java +++ b/src/main/java/org/java_websocket/server/WebSocketServer.java @@ -109,6 +109,7 @@ public abstract class WebSocketServer extends AbstractWebSocket implements Runna /** * Attribute which allows you to configure the socket "backlog" parameter * which determines how many client connections can be queued. + * @since 1.5.0 */ private int maxPendingConnections = -1; @@ -316,6 +317,7 @@ public List getDraft() { * Set the requested maximum number of pending connections on the socket. The exact semantics are implementation * specific. The value provided should be greater than 0. If it is less than or equal to 0, then * an implementation specific default will be used. This option will be passed as "backlog" parameter to {@link ServerSocket#bind(SocketAddress, int)} + * @since 1.5.0 */ public void setMaxPendingConnections(int numberOfConnections) { maxPendingConnections = numberOfConnections; @@ -325,6 +327,7 @@ public void setMaxPendingConnections(int numberOfConnections) { * Returns the currently configured maximum number of pending connections. * * @see #setMaxPendingConnections(int) + * @since 1.5.0 */ public int getMaxPendingConnections() { return maxPendingConnections;