Search in sources :

Example 1 with CancellableDependency

use of org.apache.hc.core5.concurrent.CancellableDependency 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 CancellableDependency

use of org.apache.hc.core5.concurrent.CancellableDependency in project httpcomponents-core by apache.

the class H2MultiplexingRequester method execute.

private void execute(final AsyncClientExchangeHandler exchangeHandler, final HandlerFactory<AsyncPushConsumer> pushHandlerFactory, final CancellableDependency cancellableDependency, final Timeout timeout, final HttpContext context) {
    Args.notNull(exchangeHandler, "Exchange handler");
    Args.notNull(timeout, "Timeout");
    Args.notNull(context, "Context");
    try {
        exchangeHandler.produceRequest((request, entityDetails, httpContext) -> {
            final String scheme = request.getScheme();
            final URIAuthority authority = request.getAuthority();
            if (authority == null) {
                throw new ProtocolException("Request authority not specified");
            }
            final HttpHost target = new HttpHost(scheme, authority);
            connPool.getSession(target, timeout, new FutureCallback<IOSession>() {

                @Override
                public void completed(final IOSession ioSession) {
                    ioSession.enqueue(new RequestExecutionCommand(new AsyncClientExchangeHandler() {

                        @Override
                        public void releaseResources() {
                            exchangeHandler.releaseResources();
                        }

                        @Override
                        public void produceRequest(final RequestChannel channel, final HttpContext httpContext) throws HttpException, IOException {
                            channel.sendRequest(request, entityDetails, httpContext);
                        }

                        @Override
                        public int available() {
                            return exchangeHandler.available();
                        }

                        @Override
                        public void produce(final DataStreamChannel channel) throws IOException {
                            exchangeHandler.produce(channel);
                        }

                        @Override
                        public void consumeInformation(final HttpResponse response, final HttpContext httpContext) throws HttpException, IOException {
                            exchangeHandler.consumeInformation(response, httpContext);
                        }

                        @Override
                        public void consumeResponse(final HttpResponse response, final EntityDetails entityDetails, final HttpContext httpContext) throws HttpException, IOException {
                            exchangeHandler.consumeResponse(response, entityDetails, httpContext);
                        }

                        @Override
                        public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
                            exchangeHandler.updateCapacity(capacityChannel);
                        }

                        @Override
                        public void consume(final ByteBuffer src) throws IOException {
                            exchangeHandler.consume(src);
                        }

                        @Override
                        public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
                            exchangeHandler.streamEnd(trailers);
                        }

                        @Override
                        public void cancel() {
                            exchangeHandler.cancel();
                        }

                        @Override
                        public void failed(final Exception cause) {
                            exchangeHandler.failed(cause);
                        }
                    }, pushHandlerFactory, cancellableDependency, context), Command.Priority.NORMAL);
                    if (!ioSession.isOpen()) {
                        exchangeHandler.failed(new ConnectionClosedException());
                    }
                }

                @Override
                public void failed(final Exception ex) {
                    exchangeHandler.failed(ex);
                }

                @Override
                public void cancelled() {
                    exchangeHandler.cancel();
                }
            });
        }, context);
    } catch (final IOException | HttpException ex) {
        exchangeHandler.failed(ex);
    }
}
Also used : ProtocolException(org.apache.hc.core5.http.ProtocolException) URIAuthority(org.apache.hc.core5.net.URIAuthority) AsyncClientExchangeHandler(org.apache.hc.core5.http.nio.AsyncClientExchangeHandler) HttpContext(org.apache.hc.core5.http.protocol.HttpContext) HttpResponse(org.apache.hc.core5.http.HttpResponse) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) IOException(java.io.IOException) RequestExecutionCommand(org.apache.hc.core5.http.nio.command.RequestExecutionCommand) ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) HttpException(org.apache.hc.core5.http.HttpException) ProtocolException(org.apache.hc.core5.http.ProtocolException) IOException(java.io.IOException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) CapacityChannel(org.apache.hc.core5.http.nio.CapacityChannel) HttpHost(org.apache.hc.core5.http.HttpHost) EntityDetails(org.apache.hc.core5.http.EntityDetails) IOSession(org.apache.hc.core5.reactor.IOSession) HttpException(org.apache.hc.core5.http.HttpException) RequestChannel(org.apache.hc.core5.http.nio.RequestChannel)

Aggregations

IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CancellableDependency (org.apache.hc.core5.concurrent.CancellableDependency)1 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)1 EntityDetails (org.apache.hc.core5.http.EntityDetails)1 HttpException (org.apache.hc.core5.http.HttpException)1 HttpHost (org.apache.hc.core5.http.HttpHost)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 ProtocolException (org.apache.hc.core5.http.ProtocolException)1 AsyncClientExchangeHandler (org.apache.hc.core5.http.nio.AsyncClientExchangeHandler)1 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)1 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)1 RequestChannel (org.apache.hc.core5.http.nio.RequestChannel)1 ExecutableCommand (org.apache.hc.core5.http.nio.command.ExecutableCommand)1 RequestExecutionCommand (org.apache.hc.core5.http.nio.command.RequestExecutionCommand)1 ShutdownCommand (org.apache.hc.core5.http.nio.command.ShutdownCommand)1 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)1