Search in sources :

Example 91 with Session

use of org.eclipse.jetty.http2.api.Session in project jetty.project by eclipse.

the class PingTest method testPing.

@Test
public void testPing() throws Exception {
    start(new ServerSessionListener.Adapter());
    final byte[] payload = new byte[8];
    new Random().nextBytes(payload);
    final CountDownLatch latch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter() {

        @Override
        public void onPing(Session session, PingFrame frame) {
            Assert.assertTrue(frame.isReply());
            Assert.assertArrayEquals(payload, frame.getPayload());
            latch.countDown();
        }
    });
    PingFrame frame = new PingFrame(payload, false);
    session.ping(frame, Callback.NOOP);
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Random(java.util.Random) PingFrame(org.eclipse.jetty.http2.frames.PingFrame) CountDownLatch(java.util.concurrent.CountDownLatch) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 92 with Session

use of org.eclipse.jetty.http2.api.Session in project jetty.project by eclipse.

the class PriorityTest method testPriorityAfterHeaders.

@Test
public void testPriorityAfterHeaders() throws Exception {
    CountDownLatch beforeRequests = new CountDownLatch(1);
    CountDownLatch afterRequests = new CountDownLatch(2);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            try {
                beforeRequests.await(5, TimeUnit.SECONDS);
                MetaData.Response metaData = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
                HeadersFrame responseFrame = new HeadersFrame(stream.getId(), metaData, null, true);
                stream.headers(responseFrame, Callback.NOOP);
                afterRequests.countDown();
                return null;
            } catch (InterruptedException x) {
                x.printStackTrace();
                return null;
            }
        }
    });
    CountDownLatch responses = new CountDownLatch(2);
    Stream.Listener.Adapter listener = new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                responses.countDown();
        }
    };
    Session session = newClient(new Session.Listener.Adapter());
    MetaData metaData1 = newRequest("GET", "/one", new HttpFields());
    HeadersFrame headersFrame1 = new HeadersFrame(metaData1, null, true);
    FuturePromise<Stream> promise1 = new FuturePromise<>();
    session.newStream(headersFrame1, promise1, listener);
    Stream stream1 = promise1.get(5, TimeUnit.SECONDS);
    MetaData metaData2 = newRequest("GET", "/two", new HttpFields());
    HeadersFrame headersFrame2 = new HeadersFrame(metaData2, null, true);
    FuturePromise<Stream> promise2 = new FuturePromise<>();
    session.newStream(headersFrame2, promise2, listener);
    Stream stream2 = promise2.get(5, TimeUnit.SECONDS);
    int streamId = session.priority(new PriorityFrame(stream1.getId(), stream2.getId(), 13, false), Callback.NOOP);
    Assert.assertEquals(stream1.getId(), streamId);
    // Give time to the PRIORITY frame to arrive to server.
    Thread.sleep(1000);
    beforeRequests.countDown();
    Assert.assertTrue(afterRequests.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(responses.await(5, TimeUnit.SECONDS));
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) PriorityFrame(org.eclipse.jetty.http2.frames.PriorityFrame) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) Stream(org.eclipse.jetty.http2.api.Stream) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Example 93 with Session

use of org.eclipse.jetty.http2.api.Session in project jetty.project by eclipse.

the class AbstractFlowControlStrategy method updateInitialStreamWindow.

@Override
public void updateInitialStreamWindow(ISession session, int initialStreamWindow, boolean local) {
    int previousInitialStreamWindow;
    if (local) {
        previousInitialStreamWindow = getInitialStreamRecvWindow();
        this.initialStreamRecvWindow = initialStreamWindow;
    } else {
        previousInitialStreamWindow = getInitialStreamSendWindow();
        this.initialStreamSendWindow = initialStreamWindow;
    }
    int delta = initialStreamWindow - previousInitialStreamWindow;
    // SPEC: updates of the initial window size only affect stream windows, not session's.
    for (Stream stream : session.getStreams()) {
        if (local) {
            ((IStream) stream).updateRecvWindow(delta);
            if (LOG.isDebugEnabled())
                LOG.debug("Updated initial stream recv window {} -> {} for {}", previousInitialStreamWindow, initialStreamWindow, stream);
        } else {
            session.onWindowUpdate((IStream) stream, new WindowUpdateFrame(stream.getId(), delta));
        }
    }
}
Also used : WindowUpdateFrame(org.eclipse.jetty.http2.frames.WindowUpdateFrame) Stream(org.eclipse.jetty.http2.api.Stream)

Example 94 with Session

use of org.eclipse.jetty.http2.api.Session in project jetty.project by eclipse.

the class BufferingFlowControlStrategy method onDataConsumed.

@Override
public void onDataConsumed(ISession session, IStream stream, int length) {
    if (length <= 0)
        return;
    float ratio = bufferRatio;
    WindowUpdateFrame windowFrame = null;
    int level = sessionLevel.addAndGet(length);
    int maxLevel = (int) (maxSessionRecvWindow.get() * ratio);
    if (level > maxLevel) {
        if (sessionLevel.compareAndSet(level, 0)) {
            session.updateRecvWindow(level);
            if (LOG.isDebugEnabled())
                LOG.debug("Data consumed, {} bytes, updated session recv window by {}/{} for {}", length, level, maxLevel, session);
            windowFrame = new WindowUpdateFrame(0, level);
        } else {
            if (LOG.isDebugEnabled())
                LOG.debug("Data consumed, {} bytes, concurrent session recv window level {}/{} for {}", length, sessionLevel, maxLevel, session);
        }
    } else {
        if (LOG.isDebugEnabled())
            LOG.debug("Data consumed, {} bytes, session recv window level {}/{} for {}", length, level, maxLevel, session);
    }
    Frame[] windowFrames = Frame.EMPTY_ARRAY;
    if (stream != null) {
        if (stream.isClosed()) {
            if (LOG.isDebugEnabled())
                LOG.debug("Data consumed, {} bytes, ignoring update stream recv window for closed {}", length, stream);
        } else {
            AtomicInteger streamLevel = streamLevels.get(stream);
            if (streamLevel != null) {
                level = streamLevel.addAndGet(length);
                maxLevel = (int) (getInitialStreamRecvWindow() * ratio);
                if (level > maxLevel) {
                    level = streamLevel.getAndSet(0);
                    stream.updateRecvWindow(level);
                    if (LOG.isDebugEnabled())
                        LOG.debug("Data consumed, {} bytes, updated stream recv window by {}/{} for {}", length, level, maxLevel, stream);
                    WindowUpdateFrame frame = new WindowUpdateFrame(stream.getId(), level);
                    if (windowFrame == null)
                        windowFrame = frame;
                    else
                        windowFrames = new Frame[] { frame };
                } else {
                    if (LOG.isDebugEnabled())
                        LOG.debug("Data consumed, {} bytes, stream recv window level {}/{} for {}", length, level, maxLevel, stream);
                }
            }
        }
    }
    if (windowFrame != null)
        session.frames(stream, Callback.NOOP, windowFrame, windowFrames);
}
Also used : WindowUpdateFrame(org.eclipse.jetty.http2.frames.WindowUpdateFrame) WindowUpdateFrame(org.eclipse.jetty.http2.frames.WindowUpdateFrame) Frame(org.eclipse.jetty.http2.frames.Frame) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 95 with Session

use of org.eclipse.jetty.http2.api.Session in project jetty.project by eclipse.

the class IdleTimeoutTest method testServerEnforcingStreamIdleTimeout.

@Test
public void testServerEnforcingStreamIdleTimeout() throws Exception {
    final CountDownLatch timeoutLatch = new CountDownLatch(1);
    start(new ServerSessionListener.Adapter() {

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            stream.setIdleTimeout(idleTimeout);
            return new Stream.Listener.Adapter() {

                @Override
                public boolean onIdleTimeout(Stream stream, Throwable x) {
                    timeoutLatch.countDown();
                    return true;
                }
            };
        }
    });
    final CountDownLatch resetLatch = new CountDownLatch(1);
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    // Stream does not end here, but we won't send any DATA frame.
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
    session.newStream(requestFrame, new Promise.Adapter<>(), new Stream.Listener.Adapter() {

        @Override
        public void onReset(Stream stream, ResetFrame frame) {
            resetLatch.countDown();
        }
    });
    Assert.assertTrue(timeoutLatch.await(5, TimeUnit.SECONDS));
    Assert.assertTrue(resetLatch.await(5, TimeUnit.SECONDS));
    // Stream must be gone.
    Assert.assertTrue(session.getStreams().isEmpty());
    // Session must not be closed, nor disconnected.
    Assert.assertFalse(session.isClosed());
    Assert.assertFalse(((HTTP2Session) session).isDisconnected());
}
Also used : ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) Promise(org.eclipse.jetty.util.Promise) FuturePromise(org.eclipse.jetty.util.FuturePromise) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ServletInputStream(javax.servlet.ServletInputStream) Stream(org.eclipse.jetty.http2.api.Stream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) Session(org.eclipse.jetty.http2.api.Session) Test(org.junit.Test)

Aggregations

Session (org.eclipse.jetty.http2.api.Session)102 CountDownLatch (java.util.concurrent.CountDownLatch)94 Stream (org.eclipse.jetty.http2.api.Stream)93 Test (org.junit.Test)93 HttpFields (org.eclipse.jetty.http.HttpFields)91 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)91 MetaData (org.eclipse.jetty.http.MetaData)88 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)79 FuturePromise (org.eclipse.jetty.util.FuturePromise)76 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)52 Callback (org.eclipse.jetty.util.Callback)52 Promise (org.eclipse.jetty.util.Promise)51 HttpServletResponse (javax.servlet.http.HttpServletResponse)47 HTTP2Session (org.eclipse.jetty.http2.HTTP2Session)39 HttpServletRequest (javax.servlet.http.HttpServletRequest)34 IOException (java.io.IOException)33 ServletException (javax.servlet.ServletException)32 HttpServlet (javax.servlet.http.HttpServlet)30 ISession (org.eclipse.jetty.http2.ISession)27 ByteBuffer (java.nio.ByteBuffer)25