Skip to content

Commit ce5518e

Browse files
committed
Merged branch 'jetty-12.0.x' into 'jetty-12.1.x'.
Signed-off-by: Simone Bordet <[email protected]>
2 parents 269af78 + 11476a9 commit ce5518e

File tree

8 files changed

+323
-149
lines changed

8 files changed

+323
-149
lines changed

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.eclipse.jetty.client.HttpClient;
3636
import org.eclipse.jetty.ee10.servlet.DefaultServlet;
3737
import org.eclipse.jetty.ee10.servlet.ResourceServlet;
38-
import org.eclipse.jetty.ee10.servlet.ResourceServlet;
3938
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
4039
import org.eclipse.jetty.ee10.servlet.ServletHolder;
4140
import org.eclipse.jetty.ee10.webapp.WebAppContext;
@@ -61,6 +60,7 @@
6160
import org.eclipse.jetty.rewrite.handler.RewriteHandler;
6261
import org.eclipse.jetty.rewrite.handler.RewriteRegexRule;
6362
import org.eclipse.jetty.server.ConnectionFactory;
63+
import org.eclipse.jetty.server.ConnectionLimit;
6464
import org.eclipse.jetty.server.Connector;
6565
import org.eclipse.jetty.server.CustomRequestLog;
6666
import org.eclipse.jetty.server.FormFields;
@@ -353,6 +353,28 @@ public void onOpen(NetworkConnector connector)
353353
// end::sameRandomPort[]
354354
}
355355

356+
public void connectionLimit()
357+
{
358+
// tag::connectionLimit[]
359+
Server server = new Server();
360+
361+
// Limit connections to the server, across all connectors.
362+
ConnectionLimit serverConnectionLimit = new ConnectionLimit(1024, server);
363+
server.addBean(serverConnectionLimit);
364+
365+
ServerConnector connector1 = new ServerConnector(server);
366+
connector1.setPort(8080);
367+
server.addConnector(connector1);
368+
369+
ServerConnector connector2 = new ServerConnector(server);
370+
connector2.setPort(9090);
371+
server.addConnector(connector2);
372+
// Limit connections for this connector only.
373+
ConnectionLimit connectorConnectionLimit = new ConnectionLimit(64, connector2);
374+
connector2.addBean(connectorConnectionLimit);
375+
// end::connectionLimit[]
376+
}
377+
356378
public void sslHandshakeListener() throws Exception
357379
{
358380
// tag::sslHandshakeListener[]

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

+16
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ This property allows you to cap the max heap memory retained by the pool.
5454
`jetty.byteBufferPool.maxDirectMemory`::
5555
This property allows you to cap the max direct memory retained by the pool.
5656

57+
[[connectionlimit]]
58+
== Module `connectionlimit`
59+
60+
The `connectionlimit` module limits the number of connections accepted by the server, across all connectors.
61+
62+
Once the configured maximum number of connections is reached, Jetty will not accept more connections.
63+
Existing, established connections will work normally.
64+
When existing connections are closed, accepting new connections will be resumed.
65+
66+
NOTE: The number of connections seen at the JVM level may be different from the number of connections seen at the OS level.
67+
For more information, refer to xref:programming-guide:server/http.adoc#connector-limiting[this section].
68+
69+
The module file is `$JETTY_HOME/modules/connectionlimit.mod`:
70+
71+
include::{jetty-home}/modules/connectionlimit.mod[tags=documentation]
72+
5773
[[console-capture]]
5874
== Module `console-capture`
5975

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

+21
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,27 @@ For example:
394394
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=sameRandomPort]
395395
----
396396

397+
[[connector-limiting]]
398+
=== Limiting Connections
399+
400+
It is possible to limit the number of connections accepted by the whole server (and therefore across all connectors), or by a specific connector.
401+
402+
This feature is implemented by class `org.eclipse.jetty.server.ConnectionLimit` and you can use it in this way:
403+
404+
[,java,indent=0,options=nowrap]
405+
----
406+
include::code:example$src/main/java/org/eclipse/jetty/docs/programming/server/http/HTTPServerDocs.java[tags=connectionLimit]
407+
----
408+
409+
[NOTE]
410+
====
411+
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.
412+
413+
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`.
414+
415+
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.
416+
====
417+
397418
[[connector-protocol]]
398419
=== Configuring Protocols
399420

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.net.ConnectException;
1919
import java.net.SocketTimeoutException;
2020
import java.nio.channels.CancelledKeyException;
21+
import java.nio.channels.ClosedChannelException;
2122
import java.nio.channels.ClosedSelectorException;
2223
import java.nio.channels.SelectableChannel;
2324
import java.nio.channels.SelectionKey;
@@ -864,14 +865,6 @@ class Accept implements SelectorUpdate, Runnable, Closeable
864865
_selectorManager.onAccepting(channel);
865866
}
866867

867-
@Override
868-
public void close()
869-
{
870-
if (LOG.isDebugEnabled())
871-
LOG.debug("closed accept of {}", channel);
872-
IO.close(channel);
873-
}
874-
875868
@Override
876869
public void update(Selector selector)
877870
{
@@ -882,10 +875,9 @@ public void update(Selector selector)
882875
}
883876
catch (Throwable x)
884877
{
885-
IO.close(channel);
886-
_selectorManager.onAcceptFailed(channel, x);
887878
if (LOG.isDebugEnabled())
888879
LOG.debug("Could not register channel after accept {}", channel, x);
880+
failed(x);
889881
}
890882
}
891883

@@ -894,22 +886,28 @@ public void run()
894886
{
895887
try
896888
{
897-
createEndPoint(channel, key);
898889
_selectorManager.onAccepted(channel);
890+
createEndPoint(channel, key);
899891
}
900892
catch (Throwable x)
901893
{
894+
if (LOG.isDebugEnabled())
895+
LOG.debug("Could not process accepted channel {}", channel, x);
902896
failed(x);
903897
}
904898
}
905899

906-
protected void failed(Throwable failure)
900+
@Override
901+
public void close()
907902
{
908-
IO.close(channel);
909903
if (LOG.isDebugEnabled())
910-
LOG.warn("Could not accept {}", channel, failure);
911-
else
912-
LOG.warn("Could not accept {}: {}", channel, String.valueOf(failure));
904+
LOG.debug("Closed accept of {}", channel);
905+
failed(new ClosedChannelException());
906+
}
907+
908+
private void failed(Throwable failure)
909+
{
910+
IO.close(channel);
913911
_selectorManager.onAcceptFailed(channel, failure);
914912
}
915913

@@ -1028,6 +1026,8 @@ public void update(Selector selector)
10281026
IO.close((Closeable)attachment);
10291027
}
10301028
_selector = null;
1029+
if (LOG.isDebugEnabled())
1030+
LOG.debug("Closing {} on {}", selector, ManagedSelector.this);
10311031
IO.close(selector);
10321032
}
10331033
finally
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# DO NOT EDIT THIS FILE - See: https://jetty.org/docs/
22

33
[description]
4-
Enables a server-wide connection limit.
4+
Enables a server-wide limit on TCP connections.
55

66
[tags]
77
connector
@@ -13,9 +13,10 @@ server
1313
etc/jetty-connectionlimit.xml
1414

1515
[ini-template]
16-
17-
## The limit of connections to apply
16+
#tag::documentation[]
17+
## The maximum number of TCP connections allowed across all connectors.
1818
#jetty.connectionlimit.maxConnections=1000
1919

20-
## The idle timeout to apply (in milliseconds) when connections are limited
20+
## The idle timeout to apply (in milliseconds) to existing connections when the connection limit is reached.
2121
#jetty.connectionlimit.idleTimeout=1000
22+
#end::documentation[]

0 commit comments

Comments
 (0)