Skip to content

Commit bd78956

Browse files
Fixes to ConnectionLimit for use with WebSocket (#13350)
* Added method `AcceptListener.onClosed()`. * Disabled `WebSocketOverHTTP2Test.testConnectionLimit()`. * Refinements to `WebSocketConnection` OPEN state * Introduced NetworkConnectionLimit and associated Jetty module. * Deprecated ConnectionLimit and associated Jetty module, and removed their usages in favor of NetworkConnectionLimit. Signed-off-by: Lachlan Roberts <[email protected]> Signed-off-by: Simone Bordet <[email protected]> Co-authored-by: Simone Bordet <[email protected]>
1 parent 47e1a01 commit bd78956

File tree

19 files changed

+685
-74
lines changed

19 files changed

+685
-74
lines changed

documentation/jetty/modules/code/examples/src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import org.eclipse.jetty.rewrite.handler.RewriteHandler;
6363
import org.eclipse.jetty.rewrite.handler.RewriteRegexRule;
6464
import org.eclipse.jetty.server.ConnectionFactory;
65-
import org.eclipse.jetty.server.ConnectionLimit;
6665
import org.eclipse.jetty.server.Connector;
6766
import org.eclipse.jetty.server.CustomRequestLog;
6867
import org.eclipse.jetty.server.FormFields;
@@ -71,6 +70,7 @@
7170
import org.eclipse.jetty.server.HttpConnectionFactory;
7271
import org.eclipse.jetty.server.MemoryConnector;
7372
import org.eclipse.jetty.server.MemoryTransport;
73+
import org.eclipse.jetty.server.NetworkConnectionLimit;
7474
import org.eclipse.jetty.server.NetworkConnector;
7575
import org.eclipse.jetty.server.ProxyConnectionFactory;
7676
import org.eclipse.jetty.server.Request;
@@ -357,14 +357,14 @@ public void onOpen(NetworkConnector connector)
357357
// end::sameRandomPort[]
358358
}
359359

360-
public void connectionLimit()
360+
public void networkConnectionLimit()
361361
{
362-
// tag::connectionLimit[]
362+
// tag::networkConnectionLimit[]
363363
Server server = new Server();
364364

365-
// Limit connections to the server, across all connectors.
366-
ConnectionLimit serverConnectionLimit = new ConnectionLimit(1024, server);
367-
server.addBean(serverConnectionLimit);
365+
// Limit TCP connections to the server, across all connectors.
366+
NetworkConnectionLimit serverNetworkConnectionLimit = new NetworkConnectionLimit(1024, server);
367+
server.addBean(serverNetworkConnectionLimit);
368368

369369
ServerConnector connector1 = new ServerConnector(server);
370370
connector1.setPort(8080);
@@ -374,9 +374,9 @@ public void connectionLimit()
374374
connector2.setPort(9090);
375375
server.addConnector(connector2);
376376
// Limit connections for this connector only.
377-
ConnectionLimit connectorConnectionLimit = new ConnectionLimit(64, connector2);
378-
connector2.addBean(connectorConnectionLimit);
379-
// end::connectionLimit[]
377+
NetworkConnectionLimit connectorNetworkConnectionLimit = new NetworkConnectionLimit(64, connector2);
378+
connector2.addBean(connectorNetworkConnectionLimit);
379+
// end::networkConnectionLimit[]
380380
}
381381

382382
public void sslHandshakeListener() throws Exception

documentation/jetty/modules/operations-guide/pages/modules/standard.adoc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Existing, established connections will work normally.
3636

3737
Once accepting new connections has been suspended, Jetty will periodically check whether the rate has returned withing the limits, and if so, accepting new connections will be resumed.
3838

39-
This module is often used in conjunction with the xref:connection-limit[`connection-limit`] module.
39+
This module is often used in conjunction with the xref:network-connection-limit[`network-connection-limit`] module.
4040

4141
NOTE: The number of connections seen at the JVM level may be different from the number of connections seen at the OS level.
4242
For more information, refer to xref:programming-guide:server/http.adoc#connector-limiting[this section].
@@ -75,24 +75,24 @@ This property allows you to cap the max heap memory retained by the pool.
7575
`jetty.byteBufferPool.maxDirectMemory`::
7676
This property allows you to cap the max direct memory retained by the pool.
7777

78-
[[connection-limit]]
79-
== Module `connection-limit`
78+
[[network-connection-limit]]
79+
== Module `network-connection-limit`
8080

81-
The `connection-limit` module limits the number of connections accepted by the server, across all connectors.
81+
The `network-connection-limit` module limits the number of network connections accepted by the server, across all connectors.
8282

83-
Once the configured maximum number of connections is reached, Jetty will not accept more connections.
84-
Existing, established connections will work normally.
85-
When existing connections are closed, accepting new connections will be resumed.
83+
Once the configured maximum number of network connections is reached, Jetty will not accept more network connections.
84+
Existing, established network connections will work normally.
85+
When existing network connections are closed, accepting new network connections will be resumed.
8686

8787
This module is often used in conjunction with the xref:accept-rate-limit[`accept-rate-limit`] module.
8888

89-
NOTE: The number of connections seen at the JVM level may be different from the number of connections seen at the OS level.
89+
NOTE: The number of network connections seen at the JVM level may be different from the number of network connections seen at the OS level.
9090
For more information, refer to xref:programming-guide:server/http.adoc#connector-limiting[this section].
9191

92-
The module file is `$JETTY_HOME/modules/connection-limit.mod`:
92+
The module file is `$JETTY_HOME/modules/network-connection-limit.mod`:
9393

9494
----
95-
include::{jetty-home}/modules/connection-limit.mod[tags=documentation]
95+
include::{jetty-home}/modules/network-connection-limit-limit.mod[tags=documentation]
9696
----
9797

9898
[[console-capture]]

documentation/jetty/modules/programming-guide/pages/arch/io.adoc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,7 @@ This is useful in many cases, for example:
132132
`Connection.Listener` implementations must be added as beans to a server-side `Connector`, or to client-side `HttpClient`, `WebSocketClient`, `HTTP2Client` or `HTTP3Client`.
133133
You can add as beans many `Connection.Listener` objects, each with its own logic, so that you can separate different logics into different `Connection.Listener` implementations.
134134

135-
The Jetty I/O library provides useful `Connection.Listener` implementations that you should evaluate before writing your own:
136-
137-
* link:{javadoc-url}/org/eclipse/jetty/io/ConnectionStatistics.html[`ConnectionStatistics`]
138-
* link:{javadoc-url}/org/eclipse/jetty/server/ConnectionLimit.html[`ConnectionLimit`]
135+
The Jetty I/O library provides useful `Connection.Listener` implementations that you should evaluate before writing your own, for example link:{javadoc-url}/org/eclipse/jetty/io/ConnectionStatistics.html[`ConnectionStatistics`].
139136

140137
Here is a simple example of a `Connection.Listener` used both on the client and on the server:
141138

documentation/jetty/modules/programming-guide/pages/server/http.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,22 +404,22 @@ include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/ht
404404
[[connector-limiting]]
405405
=== Limiting Connections
406406

407-
It is possible to limit the number of connections accepted by the whole server (and therefore across all connectors), or by a specific connector.
407+
It is possible to limit the number of TCP connections accepted by the whole server (and therefore across all connectors), or by a specific connector.
408408

409-
This feature is implemented by class `org.eclipse.jetty.server.ConnectionLimit` and you can use it in this way:
409+
This feature is implemented by class `org.eclipse.jetty.server.NetworkConnectionLimit` and you can use it in this way:
410410

411411
[,java,indent=0,options=nowrap]
412412
----
413-
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=connectionLimit]
413+
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=networkConnectionLimit]
414414
----
415415

416416
[NOTE]
417417
====
418-
When the maximum number of connections is reached, no more connections will be accepted _at the JVM level_ -- but they could be accepted at the OS level.
418+
When the maximum number of TCP connections is reached, no more TCP connections will be accepted _at the JVM level_ -- but they could be accepted at the OS level.
419419
420-
This means that if you are using OS tools (like Linux's `ss`) to count the number of established connections, you may find a number that may be greater than the maximum number of connections configured in a `ConnectionLimit`.
420+
This means that if you are using OS tools (like Linux's `ss`) to count the number of established TCP connections, you may find a number that may be greater than the maximum number of TCP connections configured in a `NetworkConnectionLimit`.
421421
422-
Note also that different operative systems may behave differently when Jetty is not accepting connections: some OS accepts connections at the TCP level anyway (but does not notify this event to the JVM), some other OS may not accept connections at the TCP level.
422+
Note also that different operative systems may behave differently when Jetty is not accepting TCP connections: some OS accepts connections at the TCP level anyway (but does not notify this event to the JVM), some other OS may not accept connections at the TCP level.
423423
====
424424

425425
[[connector-protocol]]

jetty-core/jetty-http2/jetty-http2-common/src/main/java/org/eclipse/jetty/http2/HTTP2StreamEndPoint.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ public void close(Throwable cause)
183183
data.release();
184184
shutdownOutput();
185185
stream.close();
186+
connection.onClose(cause);
186187
onClose(cause);
187188
}
188189
}

jetty-core/jetty-io/src/main/java/org/eclipse/jetty/io/SelectorManager.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,21 @@ protected void endPointOpened(EndPoint endpoint)
309309
*/
310310
protected void endPointClosed(EndPoint endpoint)
311311
{
312+
Object transport = endpoint.getTransport();
313+
if (transport instanceof SelectableChannel selectableChannel)
314+
{
315+
for (AcceptListener l : _acceptListeners)
316+
{
317+
try
318+
{
319+
l.onClosed(selectableChannel);
320+
}
321+
catch (Throwable x)
322+
{
323+
LOG.warn("Failed to notify onClosed() on listener {}", l, x);
324+
}
325+
}
326+
}
312327
}
313328

314329
/**
@@ -456,7 +471,7 @@ protected void onAccepting(SelectableChannel channel)
456471
}
457472
catch (Throwable x)
458473
{
459-
LOG.warn("Failed to notify onAccepting on listener {}", l, x);
474+
LOG.warn("Failed to notify onAccepting() on listener {}", l, x);
460475
}
461476
}
462477
}
@@ -471,7 +486,7 @@ protected void onAcceptFailed(SelectableChannel channel, Throwable cause)
471486
}
472487
catch (Throwable x)
473488
{
474-
LOG.warn("Failed to notify onAcceptFailed on listener {}", l, x);
489+
LOG.warn("Failed to notify onAcceptFailed() on listener {}", l, x);
475490
}
476491
}
477492
}
@@ -486,7 +501,7 @@ protected void onAccepted(SelectableChannel channel)
486501
}
487502
catch (Throwable x)
488503
{
489-
LOG.warn("Failed to notify onAccepted on listener {}", l, x);
504+
LOG.warn("Failed to notify onAccepted() on listener {}", l, x);
490505
}
491506
}
492507
}
@@ -533,6 +548,15 @@ default void onAcceptFailed(SelectableChannel channel, Throwable cause)
533548
default void onAccepted(SelectableChannel channel)
534549
{
535550
}
551+
552+
/**
553+
* Called when an accepted {@link SelectableChannel} is closed.
554+
*
555+
* @param channel the accepted channel
556+
*/
557+
default void onClosed(SelectableChannel channel)
558+
{
559+
}
536560
}
537561

538562
@Override
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://jetty.org/configure_10_0.dtd">
3+
<Configure id="Server" class="org.eclipse.jetty.server.Server">
4+
<Call name="addBean">
5+
<Arg>
6+
<New class="org.eclipse.jetty.server.NetworkConnectionLimit">
7+
<Arg name= "maxNetworkConnectionCount" type="int">
8+
<Property name="jetty.networkConnectionLimit.maxNetworkConnectionCount" default="1000" />
9+
</Arg>
10+
<Arg name="server">
11+
<Ref refid="Server" />
12+
</Arg>
13+
<Set name="endPointIdleTimeout"><Property name="jetty.networkConnectionLimit.endPointIdleTimeout" default="1000" /></Set>
14+
</New>
15+
</Arg>
16+
</Call>
17+
</Configure>

jetty-core/jetty-server/src/main/config/modules/connection-limit.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Enables a server-wide limit on TCP connections.
55

66
[tags]
77
connector
8+
deprecated
9+
10+
[deprecated]
11+
Use module network-connection-limit instead.
812

913
[depend]
1014
server
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[description]
2+
Enables a server-wide limit on the number of connected EndPoints.
3+
4+
[tags]
5+
connector
6+
7+
[depend]
8+
server
9+
10+
[xml]
11+
etc/jetty-network-connection-limit.xml
12+
13+
[ini-template]
14+
#tag::documentation[]
15+
## The maximum number of network connections allowed across all connectors.
16+
#jetty.networkConnectionLimit.maxNetworkConnectionCount=1000
17+
18+
## The idle timeout to apply (in milliseconds) to existing EndPoints when the limit is reached.
19+
#jetty.networkConnectionLimit.endPointIdleTimeout=1000
20+
#end::documentation[]

jetty-core/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectionLimit.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@
5656
* @see LowResourceMonitor
5757
* @see Connection.Listener
5858
* @see SelectorManager.AcceptListener
59+
* @deprecated use {@link NetworkConnectionLimit} instead
5960
*/
61+
@Deprecated(forRemoval = true, since = "12.1.0")
6062
@ManagedObject
6163
public class ConnectionLimit extends AbstractLifeCycle implements Listener, SelectorManager.AcceptListener
6264
{

0 commit comments

Comments
 (0)