diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java index b5ea9b38e416..05f9fc8c5faa 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java @@ -18,6 +18,7 @@ import java.net.ConnectException; import java.net.SocketTimeoutException; import java.nio.channels.CancelledKeyException; +import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedSelectorException; import java.nio.channels.SelectableChannel; import java.nio.channels.SelectionKey; @@ -862,8 +863,8 @@ class Accept implements SelectorUpdate, Runnable, Closeable public void close() { if (LOG.isDebugEnabled()) - LOG.debug("closed accept of {}", channel); - IO.close(channel); + LOG.debug("Closed accept of {}", channel); + failed(new ClosedChannelException()); } @Override @@ -876,10 +877,9 @@ public void update(Selector selector) } catch (Throwable x) { - IO.close(channel); - _selectorManager.onAcceptFailed(channel, x); if (LOG.isDebugEnabled()) LOG.debug("Could not register channel after accept {}", channel, x); + failed(x); } } @@ -888,22 +888,20 @@ public void run() { try { - createEndPoint(channel, key); _selectorManager.onAccepted(channel); + createEndPoint(channel, key); } catch (Throwable x) { + if (LOG.isDebugEnabled()) + LOG.debug("Could not process accepted channel {}", channel, x); failed(x); } } - protected void failed(Throwable failure) + private void failed(Throwable failure) { IO.close(channel); - if (LOG.isDebugEnabled()) - LOG.warn("Could not accept {}", channel, failure); - else - LOG.warn("Could not accept {}: {}", channel, String.valueOf(failure)); _selectorManager.onAcceptFailed(channel, failure); } diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectionLimit.java b/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectionLimit.java index d7f1793d30e0..9a968c810d7a 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectionLimit.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/ConnectionLimit.java @@ -23,6 +23,7 @@ import org.eclipse.jetty.io.Connection.Listener; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.io.SelectorManager; +import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.annotation.ManagedAttribute; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.annotation.Name; @@ -169,9 +170,10 @@ protected void doStop() throws Exception } } - protected void check() + protected boolean check() { - if ((_accepting.size() + _connections) >= _maxConnections) + int total = _accepting.size() + _connections; + if (total >= _maxConnections) { if (!_limiting) { @@ -179,6 +181,7 @@ protected void check() LOG.info("Connection Limit({}) reached for {}", _maxConnections, _connectors); limit(); } + return total > _maxConnections; } else { @@ -188,6 +191,7 @@ protected void check() LOG.info("Connection Limit({}) cleared for {}", _maxConnections, _connectors); unlimit(); } + return false; } } @@ -231,7 +235,8 @@ public void onAccepting(SelectableChannel channel) _accepting.add(channel); if (LOG.isDebugEnabled()) LOG.debug("onAccepting ({}+{}) < {} {}", _accepting.size(), _connections, _maxConnections, channel); - check(); + if (check()) + IO.close(channel); } }