use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class StatisticsHandler method shutdown.
@Override
public Future<Void> shutdown() {
FutureCallback shutdown = new FutureCallback(false);
_shutdown.compareAndSet(null, shutdown);
shutdown = _shutdown.get();
if (_dispatchedStats.getCurrent() == 0)
shutdown.succeeded();
return shutdown;
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class StatisticsHandler method doStop.
@Override
protected void doStop() throws Exception {
super.doStop();
FutureCallback shutdown = _shutdown.get();
if (shutdown != null && !shutdown.isDone())
shutdown.failed(new TimeoutException());
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class StatisticsHandler method handle.
@Override
public void handle(String path, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
_dispatchedStats.increment();
final long start;
HttpChannelState state = baseRequest.getHttpChannelState();
if (state.isInitial()) {
// new request
_requestStats.increment();
start = baseRequest.getTimeStamp();
} else {
// resumed request
start = System.currentTimeMillis();
_asyncDispatches.increment();
}
try {
Handler handler = getHandler();
if (handler != null && _shutdown.get() == null && isStarted())
handler.handle(path, baseRequest, request, response);
else if (baseRequest.isHandled()) {
if (_wrapWarning.compareAndSet(false, true))
LOG.warn("Bad statistics configuration. Latencies will be incorrect in {}", this);
} else {
baseRequest.setHandled(true);
response.sendError(HttpStatus.SERVICE_UNAVAILABLE_503);
}
} finally {
final long now = System.currentTimeMillis();
final long dispatched = now - start;
_dispatchedStats.decrement();
_dispatchedTimeStats.set(dispatched);
if (state.isSuspended()) {
if (state.isInitial()) {
state.addListener(_onCompletion);
_asyncWaitStats.increment();
}
} else if (state.isInitial()) {
long d = _requestStats.decrement();
_requestTimeStats.set(dispatched);
updateResponse(baseRequest);
// If we have no more dispatches, should we signal shutdown?
FutureCallback shutdown = _shutdown.get();
if (shutdown != null) {
response.flushBuffer();
if (d == 0)
shutdown.succeeded();
}
}
// else onCompletion will handle it.
}
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class FlowControlStrategyTest method testWindowSizeUpdates.
@Test
public void testWindowSizeUpdates() throws Exception {
final CountDownLatch prefaceLatch = new CountDownLatch(1);
final CountDownLatch stream1Latch = new CountDownLatch(1);
final CountDownLatch stream2Latch = new CountDownLatch(1);
final CountDownLatch settingsLatch = new CountDownLatch(1);
start(new ServerSessionListener.Adapter() {
@Override
public Map<Integer, Integer> onPreface(Session session) {
HTTP2Session serverSession = (HTTP2Session) session;
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, serverSession.getSendWindow());
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, serverSession.getRecvWindow());
prefaceLatch.countDown();
return null;
}
@Override
public void onSettings(Session session, SettingsFrame frame) {
for (Stream stream : session.getStreams()) {
HTTP2Stream serverStream = (HTTP2Stream) stream;
Assert.assertEquals(0, serverStream.getSendWindow());
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, serverStream.getRecvWindow());
}
settingsLatch.countDown();
}
@Override
public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
HTTP2Stream serverStream = (HTTP2Stream) stream;
MetaData.Request request = (MetaData.Request) frame.getMetaData();
if ("GET".equalsIgnoreCase(request.getMethod())) {
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, serverStream.getSendWindow());
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, serverStream.getRecvWindow());
stream1Latch.countDown();
} else {
Assert.assertEquals(0, serverStream.getSendWindow());
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, serverStream.getRecvWindow());
stream2Latch.countDown();
}
return null;
}
});
HTTP2Session clientSession = (HTTP2Session) newClient(new Session.Listener.Adapter());
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, clientSession.getSendWindow());
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, clientSession.getRecvWindow());
Assert.assertTrue(prefaceLatch.await(5, TimeUnit.SECONDS));
MetaData.Request request1 = newRequest("GET", new HttpFields());
FuturePromise<Stream> promise1 = new FuturePromise<>();
clientSession.newStream(new HeadersFrame(request1, null, true), promise1, new Stream.Listener.Adapter());
HTTP2Stream clientStream1 = (HTTP2Stream) promise1.get(5, TimeUnit.SECONDS);
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, clientStream1.getSendWindow());
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, clientStream1.getRecvWindow());
Assert.assertTrue(stream1Latch.await(5, TimeUnit.SECONDS));
// Send a SETTINGS frame that changes the window size.
// This tells the server that its stream send window must be updated,
// so on the client it's the receive window that must be updated.
Map<Integer, Integer> settings = new HashMap<>();
settings.put(SettingsFrame.INITIAL_WINDOW_SIZE, 0);
SettingsFrame frame = new SettingsFrame(settings, false);
FutureCallback callback = new FutureCallback();
clientSession.settings(frame, callback);
callback.get(5, TimeUnit.SECONDS);
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, clientStream1.getSendWindow());
Assert.assertEquals(0, clientStream1.getRecvWindow());
settingsLatch.await(5, TimeUnit.SECONDS);
// Now create a new stream, it must pick up the new value.
MetaData.Request request2 = newRequest("POST", new HttpFields());
FuturePromise<Stream> promise2 = new FuturePromise<>();
clientSession.newStream(new HeadersFrame(request2, null, true), promise2, new Stream.Listener.Adapter());
HTTP2Stream clientStream2 = (HTTP2Stream) promise2.get(5, TimeUnit.SECONDS);
Assert.assertEquals(FlowControlStrategy.DEFAULT_WINDOW_SIZE, clientStream2.getSendWindow());
Assert.assertEquals(0, clientStream2.getRecvWindow());
Assert.assertTrue(stream2Latch.await(5, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.util.FutureCallback in project jetty.project by eclipse.
the class WriteFlusherTest method testClosedNoBlocking.
@Test
public void testClosedNoBlocking() throws Exception {
ByteArrayEndPoint endPoint = new ByteArrayEndPoint(new byte[0], 16);
endPoint.close();
AtomicBoolean incompleteFlush = new AtomicBoolean();
WriteFlusher flusher = new WriteFlusher(endPoint) {
@Override
protected void onIncompleteFlush() {
incompleteFlush.set(true);
}
};
FutureCallback callback = new FutureCallback();
flusher.write(callback, BufferUtil.toBuffer("foo"));
Assert.assertTrue(callback.isDone());
Assert.assertFalse(incompleteFlush.get());
try {
callback.get();
Assert.fail();
} catch (ExecutionException e) {
Throwable cause = e.getCause();
Assert.assertTrue(cause instanceof IOException);
Assert.assertThat(cause.getMessage(), Matchers.containsString("CLOSED"));
}
Assert.assertEquals("", endPoint.takeOutputString());
Assert.assertTrue(flusher.isIdle());
}
Aggregations