Search in sources :

Example 21 with ResetFrame

use of org.eclipse.jetty.http2.frames.ResetFrame in project jetty.project by eclipse.

the class SmallThreadPoolLoadTest method test.

private boolean test(Session session, CountDownLatch latch) throws Exception {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    // Choose a random method
    boolean download = random.nextBoolean();
    HttpMethod method = download ? HttpMethod.GET : HttpMethod.POST;
    int maxContentLength = 128 * 1024;
    int contentLength = random.nextInt(maxContentLength) + 1;
    long requestId = requestIds.incrementAndGet();
    MetaData.Request request = newRequest(method.asString(), "/" + requestId, new HttpFields());
    if (download)
        request.getFields().put("X-Download", String.valueOf(contentLength));
    HeadersFrame requestFrame = new HeadersFrame(request, null, download);
    FuturePromise<Stream> promise = new FuturePromise<>();
    CountDownLatch requestLatch = new CountDownLatch(1);
    AtomicBoolean reset = new AtomicBoolean();
    session.newStream(requestFrame, promise, new Stream.Listener.Adapter() {

        @Override
        public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream())
                requestLatch.countDown();
        }

        @Override
        public void onData(Stream stream, DataFrame frame, Callback callback) {
            callback.succeeded();
            if (frame.isEndStream())
                requestLatch.countDown();
        }

        @Override
        public void onReset(Stream stream, ResetFrame frame) {
            reset.set(true);
            requestLatch.countDown();
        }
    });
    if (!download) {
        Stream stream = promise.get(5, TimeUnit.SECONDS);
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(contentLength), true), Callback.NOOP);
    }
    boolean success = requestLatch.await(5, TimeUnit.SECONDS);
    if (success)
        latch.countDown();
    else
        logger.warn("Request {} took too long{}Server:{}{}{}Client:{}{}", requestId, System.lineSeparator(), System.lineSeparator(), server.dump(), System.lineSeparator(), System.lineSeparator(), client.dump());
    return !reset.get();
}
Also used : FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callback(org.eclipse.jetty.util.Callback) MetaData(org.eclipse.jetty.http.MetaData) HttpFields(org.eclipse.jetty.http.HttpFields) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) IntStream(java.util.stream.IntStream) Stream(org.eclipse.jetty.http2.api.Stream) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) HttpMethod(org.eclipse.jetty.http.HttpMethod)

Example 22 with ResetFrame

use of org.eclipse.jetty.http2.frames.ResetFrame in project jetty.project by eclipse.

the class IdleTimeoutTest method testStreamIdleTimeoutIsNotEnforcedWhenSending.

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

        @Override
        public Stream.Listener onNewStream(Stream stream, HeadersFrame frame) {
            MetaData.Response response = new MetaData.Response(HttpVersion.HTTP_2, 200, new HttpFields());
            stream.headers(new HeadersFrame(stream.getId(), response, null, true), Callback.NOOP);
            return null;
        }

        @Override
        public void onReset(Session session, ResetFrame frame) {
            resetLatch.countDown();
        }
    });
    Session session = newClient(new Session.Listener.Adapter());
    MetaData.Request metaData = newRequest("GET", new HttpFields());
    HeadersFrame requestFrame = new HeadersFrame(metaData, null, false);
    FuturePromise<Stream> promise = new FuturePromise<Stream>() {

        @Override
        public void succeeded(Stream stream) {
            stream.setIdleTimeout(idleTimeout);
            super.succeeded(stream);
        }
    };
    session.newStream(requestFrame, promise, new Stream.Listener.Adapter());
    final Stream stream = promise.get(5, TimeUnit.SECONDS);
    Callback.Completable completable1 = new Callback.Completable();
    sleep(idleTimeout / 2);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), completable1);
    completable1.thenCompose(nil -> {
        Callback.Completable completable2 = new Callback.Completable();
        sleep(idleTimeout / 2);
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), completable2);
        return completable2;
    }).thenRun(() -> {
        sleep(idleTimeout / 2);
        stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), true), Callback.NOOP);
    });
    Assert.assertFalse(resetLatch.await(1, TimeUnit.SECONDS));
}
Also used : ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) HttpVersion(org.eclipse.jetty.http.HttpVersion) TimeoutException(java.util.concurrent.TimeoutException) FlowControlStrategy(org.eclipse.jetty.http2.FlowControlStrategy) ByteBuffer(java.nio.ByteBuffer) Stream(org.eclipse.jetty.http2.api.Stream) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) MetaData(org.eclipse.jetty.http.MetaData) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) HttpFields(org.eclipse.jetty.http.HttpFields) Callback(org.eclipse.jetty.util.Callback) InvocationType(org.eclipse.jetty.util.thread.Invocable.InvocationType) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) GoAwayFrame(org.eclipse.jetty.http2.frames.GoAwayFrame) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) Promise(org.eclipse.jetty.util.Promise) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) HTTP2Session(org.eclipse.jetty.http2.HTTP2Session) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Session(org.eclipse.jetty.http2.api.Session) FuturePromise(org.eclipse.jetty.util.FuturePromise) Log(org.eclipse.jetty.util.log.Log) Assert(org.junit.Assert) ServerSessionListener(org.eclipse.jetty.http2.api.server.ServerSessionListener) FuturePromise(org.eclipse.jetty.util.FuturePromise) DataFrame(org.eclipse.jetty.http2.frames.DataFrame) CountDownLatch(java.util.concurrent.CountDownLatch) HeadersFrame(org.eclipse.jetty.http2.frames.HeadersFrame) HttpServletResponse(javax.servlet.http.HttpServletResponse) Callback(org.eclipse.jetty.util.Callback) 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)

Example 23 with ResetFrame

use of org.eclipse.jetty.http2.frames.ResetFrame in project jetty.project by eclipse.

the class HTTP2Session method createRemoteStream.

protected IStream createRemoteStream(int streamId) {
    // SPEC: exceeding max concurrent streams is treated as stream error.
    while (true) {
        int remoteCount = remoteStreamCount.get();
        int maxCount = getMaxRemoteStreams();
        if (maxCount >= 0 && remoteCount >= maxCount) {
            reset(new ResetFrame(streamId, ErrorCode.REFUSED_STREAM_ERROR.code), Callback.NOOP);
            return null;
        }
        if (remoteStreamCount.compareAndSet(remoteCount, remoteCount + 1))
            break;
    }
    IStream stream = newStream(streamId, false);
    // SPEC: duplicate stream is treated as connection error.
    if (streams.putIfAbsent(streamId, stream) == null) {
        updateLastStreamId(streamId);
        stream.setIdleTimeout(getStreamIdleTimeout());
        flowControl.onStreamCreated(stream);
        if (LOG.isDebugEnabled())
            LOG.debug("Created remote {}", stream);
        return stream;
    } else {
        close(ErrorCode.PROTOCOL_ERROR.code, "duplicate_stream", Callback.NOOP);
        return null;
    }
}
Also used : ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame) EndPoint(org.eclipse.jetty.io.EndPoint)

Example 24 with ResetFrame

use of org.eclipse.jetty.http2.frames.ResetFrame in project jetty.project by eclipse.

the class ResetBodyParser method onReset.

private boolean onReset(int error) {
    ResetFrame frame = new ResetFrame(getStreamId(), error);
    reset();
    notifyReset(frame);
    return true;
}
Also used : ResetFrame(org.eclipse.jetty.http2.frames.ResetFrame)

Example 25 with ResetFrame

use of org.eclipse.jetty.http2.frames.ResetFrame 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

ResetFrame (org.eclipse.jetty.http2.frames.ResetFrame)24 Test (org.junit.Test)21 Stream (org.eclipse.jetty.http2.api.Stream)20 HeadersFrame (org.eclipse.jetty.http2.frames.HeadersFrame)20 CountDownLatch (java.util.concurrent.CountDownLatch)19 MetaData (org.eclipse.jetty.http.MetaData)19 HttpFields (org.eclipse.jetty.http.HttpFields)18 ServerSessionListener (org.eclipse.jetty.http2.api.server.ServerSessionListener)18 Session (org.eclipse.jetty.http2.api.Session)16 Callback (org.eclipse.jetty.util.Callback)13 FuturePromise (org.eclipse.jetty.util.FuturePromise)12 DataFrame (org.eclipse.jetty.http2.frames.DataFrame)11 IOException (java.io.IOException)10 HttpServletResponse (javax.servlet.http.HttpServletResponse)10 ByteBuffer (java.nio.ByteBuffer)9 ServletOutputStream (javax.servlet.ServletOutputStream)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)9 ISession (org.eclipse.jetty.http2.ISession)9 ServletException (javax.servlet.ServletException)8 WriteListener (javax.servlet.WriteListener)8