Skip to content

Commit

Permalink
Backport jetty#7515 to 9.4 - Connection limit problem for "onAcceptin…
Browse files Browse the repository at this point in the history
…g" connections

* Backport for the PR jetty#12320
  * Fixed `ManagedSelector.Accept` to emit the event "accept failed" when closed.
  * Fixed `ConnectionLimit` to close connections that exceed the maximum (may happen when the connector is configured with acceptors=0).
  • Loading branch information
vlsorokin authored and Volodymyr Sorokin committed Oct 9, 2024
1 parent 2705e0e commit b168598
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
19 changes: 8 additions & 11 deletions jetty-io/src/main/java/org/eclipse/jetty/io/ManagedSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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;
Expand Down Expand Up @@ -861,8 +862,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
Expand All @@ -875,10 +876,9 @@ public void update(Selector selector)
}
catch (Throwable x)
{
IO.close(channel);
_selectorManager.onAcceptFailed(channel, x);
if (LOG.isDebugEnabled())
LOG.debug(x);
LOG.debug("Could not register channel after accept {}", channel, x);
failed(x);
}
}

Expand All @@ -887,23 +887,20 @@ public void run()
{
try
{
createEndPoint(channel, key);
_selectorManager.onAccepted(channel);
createEndPoint(channel, key);
}
catch (Throwable x)
{
if (LOG.isDebugEnabled())
LOG.debug(x);
LOG.debug("Could not process accepted channel {}", channel, x);
failed(x);
}
}

protected void failed(Throwable failure)
private void failed(Throwable failure)
{
IO.close(channel);
LOG.warn(String.valueOf(failure));
if (LOG.isDebugEnabled())
LOG.debug(failure);
_selectorManager.onAcceptFailed(channel, failure);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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;
Expand Down Expand Up @@ -172,16 +173,18 @@ 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)
{
_limiting = true;
LOG.info("Connection Limit({}) reached for {}", _maxConnections, _connectors);
limit();
}
return total > _maxConnections;
}
else
{
Expand All @@ -191,6 +194,7 @@ protected void check()
LOG.info("Connection Limit({}) cleared for {}", _maxConnections, _connectors);
unlimit();
}
return false;
}
}

Expand Down Expand Up @@ -234,7 +238,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);
}
}

Expand Down

0 comments on commit b168598

Please sign in to comment.