Search in sources :

Example 1 with PingCommand

use of org.apache.hc.core5.http2.nio.command.PingCommand in project httpcomponents-core by apache.

the class AbstractH2StreamMultiplexer method processPendingCommands.

private void processPendingCommands() throws IOException, HttpException {
    while (streamMap.size() < remoteConfig.getMaxConcurrentStreams()) {
        final Command command = ioSession.poll();
        if (command == null) {
            break;
        }
        if (command instanceof ShutdownCommand) {
            final ShutdownCommand shutdownCommand = (ShutdownCommand) command;
            if (shutdownCommand.getType() == CloseMode.IMMEDIATE) {
                for (final Iterator<Map.Entry<Integer, H2Stream>> it = streamMap.entrySet().iterator(); it.hasNext(); ) {
                    final Map.Entry<Integer, H2Stream> entry = it.next();
                    final H2Stream stream = entry.getValue();
                    stream.cancel();
                }
                streamMap.clear();
                connState = ConnectionHandshake.SHUTDOWN;
            } else {
                if (connState.compareTo(ConnectionHandshake.ACTIVE) <= 0) {
                    final RawFrame goAway = frameFactory.createGoAway(processedRemoteStreamId, H2Error.NO_ERROR, "Graceful shutdown");
                    commitFrame(goAway);
                    connState = streamMap.isEmpty() ? ConnectionHandshake.SHUTDOWN : ConnectionHandshake.GRACEFUL_SHUTDOWN;
                }
            }
            break;
        } else if (command instanceof PingCommand) {
            final PingCommand pingCommand = (PingCommand) command;
            final AsyncPingHandler handler = pingCommand.getHandler();
            pingHandlers.add(handler);
            final RawFrame ping = frameFactory.createPing(handler.getData());
            commitFrame(ping);
        } else if (command instanceof ExecutableCommand) {
            final int streamId = generateStreamId();
            final H2StreamChannelImpl channel = new H2StreamChannelImpl(streamId, true, initInputWinSize, initOutputWinSize);
            final ExecutableCommand executableCommand = (ExecutableCommand) command;
            final H2StreamHandler streamHandler = createLocallyInitiatedStream(executableCommand, channel, httpProcessor, connMetrics);
            final H2Stream stream = new H2Stream(channel, streamHandler, false);
            streamMap.put(streamId, stream);
            if (streamListener != null) {
                final int initInputWindow = stream.getInputWindow().get();
                streamListener.onInputFlowControl(this, streamId, initInputWindow, initInputWindow);
                final int initOutputWindow = stream.getOutputWindow().get();
                streamListener.onOutputFlowControl(this, streamId, initOutputWindow, initOutputWindow);
            }
            if (stream.isOutputReady()) {
                stream.produceOutput();
            }
            final CancellableDependency cancellableDependency = executableCommand.getCancellableDependency();
            if (cancellableDependency != null) {
                cancellableDependency.setDependency(stream::abort);
            }
            if (!outputQueue.isEmpty()) {
                return;
            }
        }
    }
}
Also used : ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand) CancellableDependency(org.apache.hc.core5.concurrent.CancellableDependency) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand) ExecutableCommand(org.apache.hc.core5.http.nio.command.ExecutableCommand) Command(org.apache.hc.core5.reactor.Command) ShutdownCommand(org.apache.hc.core5.http.nio.command.ShutdownCommand) AsyncPingHandler(org.apache.hc.core5.http2.nio.AsyncPingHandler) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) ExecutableCommand(org.apache.hc.core5.http.nio.command.ExecutableCommand) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with PingCommand

use of org.apache.hc.core5.http2.nio.command.PingCommand in project httpcomponents-core by apache.

the class H2IntegrationTest method testConnectionPing.

@Test
public void testConnectionPing() throws Exception {
    server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
    final InetSocketAddress serverEndpoint = server.start();
    client.start();
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final int n = 10;
    final CountDownLatch latch = new CountDownLatch(n);
    final AtomicInteger count = new AtomicInteger(0);
    for (int i = 0; i < n; i++) {
        streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
        streamEndpoint.execute(new PingCommand(new BasicPingHandler(result -> {
            if (result) {
                count.incrementAndGet();
            }
            latch.countDown();
        })), Command.Priority.NORMAL);
    }
    Assertions.assertTrue(latch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
    Assertions.assertEquals(n, count.get());
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) BasicPingHandler(org.apache.hc.core5.http2.nio.support.BasicPingHandler) CountDownLatch(java.util.concurrent.CountDownLatch) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand) Test(org.junit.Test)

Example 3 with PingCommand

use of org.apache.hc.core5.http2.nio.command.PingCommand in project httpcomponents-core by apache.

the class H2ConnPool method validateSession.

@Override
protected void validateSession(final IOSession ioSession, final Callback<Boolean> callback) {
    if (ioSession.isOpen()) {
        final TimeValue timeValue = validateAfterInactivity;
        if (TimeValue.isNonNegative(timeValue)) {
            final long lastAccessTime = Math.min(ioSession.getLastReadTime(), ioSession.getLastWriteTime());
            final long deadline = lastAccessTime + timeValue.toMilliseconds();
            if (deadline <= System.currentTimeMillis()) {
                final Timeout socketTimeoutMillis = ioSession.getSocketTimeout();
                ioSession.enqueue(new PingCommand(new BasicPingHandler(result -> {
                    ioSession.setSocketTimeout(socketTimeoutMillis);
                    callback.execute(result);
                })), Command.Priority.NORMAL);
                return;
            }
        }
        callback.execute(true);
    } else {
        callback.execute(false);
    }
}
Also used : Timeout(org.apache.hc.core5.util.Timeout) BasicPingHandler(org.apache.hc.core5.http2.nio.support.BasicPingHandler) TimeValue(org.apache.hc.core5.util.TimeValue) PingCommand(org.apache.hc.core5.http2.nio.command.PingCommand)

Aggregations

PingCommand (org.apache.hc.core5.http2.nio.command.PingCommand)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BasicPingHandler (org.apache.hc.core5.http2.nio.support.BasicPingHandler)2 InetSocketAddress (java.net.InetSocketAddress)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CancellableDependency (org.apache.hc.core5.concurrent.CancellableDependency)1 ExecutableCommand (org.apache.hc.core5.http.nio.command.ExecutableCommand)1 ShutdownCommand (org.apache.hc.core5.http.nio.command.ShutdownCommand)1 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)1 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)1 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)1 AsyncPingHandler (org.apache.hc.core5.http2.nio.AsyncPingHandler)1 Command (org.apache.hc.core5.reactor.Command)1 TimeValue (org.apache.hc.core5.util.TimeValue)1 Timeout (org.apache.hc.core5.util.Timeout)1 Test (org.junit.Test)1