Skip to content

Commit 9d890db

Browse files
authored
Merge pull request #1001 from TooTallNate/issue-991
2 parents cab3fda + ca38a4b commit 9d890db

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

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

+29-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,13 @@ 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+
* @since 1.5.0
113+
*/
114+
private int maxPendingConnections = -1;
115+
111116
/**
112117
* Creates a WebSocketServer that will attempt to
113118
* listen on port <var>WebSocketImpl.DEFAULT_PORT</var>.
@@ -308,6 +313,26 @@ public List<Draft> getDraft() {
308313
return Collections.unmodifiableList( drafts );
309314
}
310315

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

0 commit comments

Comments
 (0)