Cleanups extracted from delayed PR #11876 (#11958)

* Cleanups extracted from delayed PR #11876

* Update from review
This commit is contained in:
Greg Wilkins 2024-06-26 16:49:58 +10:00 committed by GitHub
parent f21bbcf40f
commit 15bcc5ecb0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 93 additions and 58 deletions

View File

@ -188,7 +188,7 @@ public class HttpDestination extends ContainerLifeCycle implements Destination,
protected void doStop() throws Exception
{
requestTimeouts.destroy();
abort(new AsynchronousCloseException());
abortExchanges(new AsynchronousCloseException());
Sweeper connectionPoolSweeper = client.getBean(Sweeper.class);
if (connectionPoolSweeper != null && connectionPool instanceof Sweeper.Sweepable)
connectionPoolSweeper.remove((Sweeper.Sweepable)connectionPool);
@ -294,7 +294,7 @@ public class HttpDestination extends ContainerLifeCycle implements Destination,
@Override
public void failed(Throwable x)
{
abort(x);
abortExchanges(x);
}
@Override
@ -513,7 +513,7 @@ public class HttpDestination extends ContainerLifeCycle implements Destination,
*
* @param cause the abort cause
*/
public void abort(Throwable cause)
private void abortExchanges(Throwable cause)
{
// Copy the queue of exchanges and fail only those that are queued at this moment.
// The application may queue another request from the failure/complete listener

View File

@ -32,10 +32,8 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

View File

@ -110,7 +110,7 @@ public class HTTP2Flusher extends IteratingCallback implements Dumpable
{
entries.offer(entry);
if (LOG.isDebugEnabled())
LOG.debug("Appended {}, entries={}", entry, entries.size());
LOG.debug("Appended {}, entries={}, {}", entry, entries.size(), this);
}
}
if (closed == null)
@ -129,7 +129,7 @@ public class HTTP2Flusher extends IteratingCallback implements Dumpable
{
list.forEach(entries::offer);
if (LOG.isDebugEnabled())
LOG.debug("Appended {}, entries={}", list, entries.size());
LOG.debug("Appended {}, entries={} {}", list, entries.size(), this);
}
}
if (closed == null)
@ -158,7 +158,7 @@ public class HTTP2Flusher extends IteratingCallback implements Dumpable
protected Action process() throws Throwable
{
if (LOG.isDebugEnabled())
LOG.debug("Flushing {}", session);
LOG.debug("process {} {}", session, this);
try (AutoLock ignored = lock.lock())
{
@ -181,7 +181,7 @@ public class HTTP2Flusher extends IteratingCallback implements Dumpable
if (pendingEntries.isEmpty())
{
if (LOG.isDebugEnabled())
LOG.debug("Flushed {}", session);
LOG.debug("Flushed {} {}", session, this);
return Action.IDLE;
}
@ -254,7 +254,7 @@ public class HTTP2Flusher extends IteratingCallback implements Dumpable
if (LOG.isDebugEnabled())
LOG.debug("Failure generating {}", entry, failure);
failed(failure);
return Action.SUCCEEDED;
return Action.SCHEDULED;
}
}

View File

@ -202,7 +202,7 @@ public class ReadWriteFailuresTest
POST / HTTP/1.1
Host: localhost
Content-Length: 1
""";
try (LocalConnector.LocalEndPoint endPoint = connector.executeRequest(request))
{
@ -234,7 +234,7 @@ public class ReadWriteFailuresTest
POST / HTTP/1.1
Host: localhost
Content-Length: %d
%s
""".formatted(content.length(), content);
HttpTester.Response response = HttpTester.parseResponse(connector.getResponse(request, 5, TimeUnit.SECONDS));

View File

@ -17,6 +17,7 @@ import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
@ -39,6 +40,7 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class EventsHandlerTest
{
@ -117,6 +119,7 @@ public class EventsHandlerTest
{
AtomicReference<Long> beginNanoTime = new AtomicReference<>();
AtomicReference<Long> readyNanoTime = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
EventsHandler eventsHandler = new EventsHandler(new EchoHandler())
{
@Override
@ -124,6 +127,7 @@ public class EventsHandlerTest
{
beginNanoTime.set(request.getBeginNanoTime());
readyNanoTime.set(request.getHeadersNanoTime());
latch.countDown();
}
};
startServer(eventsHandler);
@ -148,6 +152,7 @@ public class EventsHandlerTest
String response = endPoint.getResponse();
assertThat(response, containsString("HTTP/1.1 200 OK"));
assertTrue(latch.await(5, TimeUnit.SECONDS));
assertThat(NanoTime.millisSince(beginNanoTime.get()), greaterThan(900L));
assertThat(NanoTime.millisSince(readyNanoTime.get()), greaterThan(450L));
}

View File

@ -303,58 +303,28 @@ public class ExceptionUtil
return t1;
}
public static void callAndThen(Throwable cause, Consumer<Throwable> first, Consumer<Throwable> second)
/** Call a method that handles a {@link Throwable}, catching and associating any exception that it throws.
* @param cause The {@link Throwable} to pass to the consumer
* @param consumer The handler of a {@link Throwable}
*/
public static void call(Throwable cause, Consumer<Throwable> consumer)
{
try
{
first.accept(cause);
consumer.accept(cause);
}
catch (Throwable t)
{
addSuppressedIfNotAssociated(cause, t);
}
finally
{
second.accept(cause);
}
}
public static void callAndThen(Throwable cause, Consumer<Throwable> first, Runnable second)
{
try
{
first.accept(cause);
}
catch (Throwable t)
{
addSuppressedIfNotAssociated(cause, t);
}
finally
{
second.run();
}
}
public static void callAndThen(Runnable first, Runnable second)
{
try
{
first.run();
}
catch (Throwable t)
{
// ignored
}
finally
{
second.run();
ExceptionUtil.addSuppressedIfNotAssociated(t, cause);
throw t;
}
}
/**
* Call a {@link Invocable.Callable} and handle failures
* @param callable The runnable to call
* @param failure The handling of failures
* Call a {@link Invocable.Callable} and handle any resulting failures
* @param callable The {@link org.eclipse.jetty.util.thread.Invocable.Callable} to call
* @param failure A handler of failures from the call
* @see #run(Runnable, Consumer)
*/
public static void call(Invocable.Callable callable, Consumer<Throwable> failure)
{
@ -380,6 +350,7 @@ public class ExceptionUtil
* Call a {@link Runnable} and handle failures
* @param runnable The runnable to call
* @param failure The handling of failures
* @see #call(Throwable, Consumer)
*/
public static void run(Runnable runnable, Consumer<Throwable> failure)
{
@ -401,6 +372,71 @@ public class ExceptionUtil
}
}
/**
* Call a handler of {@link Throwable} and then always call another, suppressing any exceptions thrown.
* @param cause The {@link Throwable} to be passed to both consumers.
* @param call The first {@link Consumer} of {@link Throwable} to call.
* @param then The second {@link Consumer} of {@link Throwable} to call.
*/
public static void callAndThen(Throwable cause, Consumer<Throwable> call, Consumer<Throwable> then)
{
try
{
call.accept(cause);
}
catch (Throwable t)
{
addSuppressedIfNotAssociated(cause, t);
}
finally
{
then.accept(cause);
}
}
/**
* Call a handler of {@link Throwable} and then always call a {@link Runnable}, suppressing any exceptions thrown.
* @param cause The {@link Throwable} to be passed to both consumers.
* @param call The {@link Consumer} of {@link Throwable} to call.
* @param then The {@link Runnable} to call.
*/
public static void callAndThen(Throwable cause, Consumer<Throwable> call, Runnable then)
{
try
{
call.accept(cause);
}
catch (Throwable t)
{
addSuppressedIfNotAssociated(cause, t);
}
finally
{
then.run();
}
}
/**
* Call a {@link Runnable} and then always call another, ignoring any exceptions thrown.
* @param call The first {@link Runnable} to call.
* @param then The second {@link Runnable} to call.
*/
public static void callAndThen(Runnable call, Runnable then)
{
try
{
call.run();
}
catch (Throwable t)
{
// ignored
}
finally
{
then.run();
}
}
/**
* <p>Get from a {@link CompletableFuture} and convert any uncheck exceptions to {@link RuntimeException}.</p>
* @param completableFuture The future to get from.

View File

@ -47,7 +47,6 @@ import org.eclipse.jetty.server.HttpStream;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.Session;
import org.eclipse.jetty.session.AbstractSessionManager;
import org.eclipse.jetty.session.DefaultSessionIdManager;
import org.eclipse.jetty.session.ManagedSession;
import org.eclipse.jetty.session.SessionCache;
import org.eclipse.jetty.session.SessionConfig;

View File

@ -18,7 +18,6 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@ -70,10 +69,8 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;