Skip to content

Commit 3ebbe21

Browse files
committed
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.
1 parent 4232021 commit 3ebbe21

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/main/java/org/java_websocket/server/WebSocketServer.java

+26-4
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
package org.java_websocket.server;
2727

2828
import java.io.IOException;
29-
import java.net.InetSocketAddress;
30-
import java.net.ServerSocket;
31-
import java.net.Socket;
29+
import java.net.*;
3230
import java.nio.ByteBuffer;
3331
import java.nio.channels.CancelledKeyException;
3432
import java.nio.channels.ClosedByInterruptException;
@@ -108,6 +106,12 @@ public abstract class WebSocketServer extends AbstractWebSocket implements Runna
108106

109107
private WebSocketServerFactory wsf = new DefaultWebSocketServerFactory();
110108

109+
/**
110+
* Attribute which allows you to configure the socket "backlog" parameter
111+
* which determines how many client connections can be queued.
112+
*/
113+
private int maxPendingConnections = -1;
114+
111115
/**
112116
* Creates a WebSocketServer that will attempt to
113117
* listen on port <var>WebSocketImpl.DEFAULT_PORT</var>.
@@ -308,6 +312,24 @@ public List<Draft> getDraft() {
308312
return Collections.unmodifiableList( drafts );
309313
}
310314

315+
/**
316+
* Set the requested maximum number of pending connections on the socket. The exact semantics are implementation
317+
* specific. The value provided should be greater than 0. If it is less than or equal to 0, then
318+
* an implementation specific default will be used. This option will be passed as "backlog" parameter to {@link ServerSocket#bind(SocketAddress, int)}
319+
*/
320+
public void setMaxPendingConnections(int numberOfConnections) {
321+
maxPendingConnections = numberOfConnections;
322+
}
323+
324+
/**
325+
* Returns the currently configured maximum number of pending connections.
326+
*
327+
* @see #setMaxPendingConnections(int)
328+
*/
329+
public int getMaxPendingConnections() {
330+
return maxPendingConnections;
331+
}
332+
311333
// Runnable IMPLEMENTATION /////////////////////////////////////////////////
312334
public void run() {
313335
if (!doEnsureSingleThread()) {
@@ -505,7 +527,7 @@ private boolean doSetupSelectorAndServerThread() {
505527
ServerSocket socket = server.socket();
506528
socket.setReceiveBufferSize( WebSocketImpl.RCVBUF );
507529
socket.setReuseAddress( isReuseAddr() );
508-
socket.bind( address );
530+
socket.bind( address, getMaxPendingConnections() );
509531
selector = Selector.open();
510532
server.register( selector, server.validOps() );
511533
startConnectionLostTimer();

0 commit comments

Comments
 (0)