Search in sources :

Example 1 with FutureCallback

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;
}
Also used : FutureCallback(org.eclipse.jetty.util.FutureCallback)

Example 2 with FutureCallback

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());
}
Also used : FutureCallback(org.eclipse.jetty.util.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with FutureCallback

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.
    }
}
Also used : HttpChannelState(org.eclipse.jetty.server.HttpChannelState) Handler(org.eclipse.jetty.server.Handler) FutureCallback(org.eclipse.jetty.util.FutureCallback)

Example 4 with FutureCallback

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));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) HashMap(java.util.HashMap) FuturePromise(org.eclipse.jetty.util.FuturePromise) CountDownLatch(java.util.concurrent.CountDownLatch) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SettingsFrame(org.eclipse.jetty.http2.frames.SettingsFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) HTTP2Stream(org.eclipse.jetty.http2.HTTP2Stream) Stream(org.eclipse.jetty.http2.api.Stream) Map(java.util.Map) HashMap(java.util.HashMap) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FutureCallback(org.eclipse.jetty.util.FutureCallback) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) ISession(org.eclipse.jetty.http2.ISession) Test(org.junit.Test)

Example 5 with FutureCallback

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(org.eclipse.jetty.util.FutureCallback) Test(org.junit.Test)

Aggregations

FutureCallback (org.eclipse.jetty.util.FutureCallback)17 Test (org.junit.Test)13 ExecutionException (java.util.concurrent.ExecutionException)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 IOException (java.io.IOException)4 InterruptedIOException (java.io.InterruptedIOException)4 ByteBuffer (java.nio.ByteBuffer)4 TimeoutException (java.util.concurrent.TimeoutException)2 HttpFields (org.eclipse.jetty.http.HttpFields)2 MetaData (org.eclipse.jetty.http.MetaData)2 ISession (org.eclipse.jetty.http2.ISession)2 Session (org.eclipse.jetty.http2.api.Session)2 Stream (org.eclipse.jetty.http2.api.Stream)2 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)2 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)2 FuturePromise (org.eclipse.jetty.util.FuturePromise)2 Socket (java.net.Socket)1 ServerSocketChannel (java.nio.channels.ServerSocketChannel)1 SocketChannel (java.nio.channels.SocketChannel)1 HashMap (java.util.HashMap)1