Example usage for io.netty.handler.codec.http2 Http2Connection local

List of usage examples for io.netty.handler.codec.http2 Http2Connection local

Introduction

In this page you can find the example usage for io.netty.handler.codec.http2 Http2Connection local.

Prototype

Endpoint<Http2LocalFlowController> local();

Source Link

Document

Gets a view of this connection from the local Endpoint .

Usage

From source file:com.linecorp.armeria.client.HttpClientPipelineConfigurator.java

License:Apache License

private static void incrementLocalWindowSize(ChannelPipeline pipeline, int delta) {
    try {/*from   ww  w  .  j a v a  2s  . co  m*/
        final Http2Connection connection = pipeline.get(Http2ClientConnectionHandler.class).connection();
        connection.local().flowController().incrementWindowSize(connection.connectionStream(), delta);
    } catch (Http2Exception e) {
        logger.warn("Failed to increment local flowController window size: {}", delta, e);
    }
}

From source file:io.grpc.netty.FixedHttp2ConnectionDecoder.java

License:Apache License

public FixedHttp2ConnectionDecoder(Http2Connection connection, Http2ConnectionEncoder encoder,
        Http2FrameReader frameReader, Http2PromisedRequestVerifier requestVerifier) {
    this.connection = checkNotNull(connection, "connection");
    this.frameReader = checkNotNull(frameReader, "frameReader");
    this.encoder = checkNotNull(encoder, "encoder");
    this.requestVerifier = checkNotNull(requestVerifier, "requestVerifier");
    if (connection.local().flowController() == null) {
        connection.local().flowController(new DefaultHttp2LocalFlowController(connection));
    }/*from   w  ww.  j a v a2 s.c o m*/
    connection.local().flowController().frameWriter(encoder.frameWriter());
}

From source file:io.grpc.netty.NettyClientHandler.java

License:Apache License

@VisibleForTesting
static NettyClientHandler newHandler(final Http2Connection connection, Http2FrameReader frameReader,
        Http2FrameWriter frameWriter, ClientTransportLifecycleManager lifecycleManager,
        KeepAliveManager keepAliveManager, int flowControlWindow, int maxHeaderListSize,
        Supplier<Stopwatch> stopwatchFactory, Runnable tooManyPingsRunnable, TransportTracer transportTracer,
        Attributes eagAttributes, String authority) {
    Preconditions.checkNotNull(connection, "connection");
    Preconditions.checkNotNull(frameReader, "frameReader");
    Preconditions.checkNotNull(lifecycleManager, "lifecycleManager");
    Preconditions.checkArgument(flowControlWindow > 0, "flowControlWindow must be positive");
    Preconditions.checkArgument(maxHeaderListSize > 0, "maxHeaderListSize must be positive");
    Preconditions.checkNotNull(stopwatchFactory, "stopwatchFactory");
    Preconditions.checkNotNull(tooManyPingsRunnable, "tooManyPingsRunnable");
    Preconditions.checkNotNull(eagAttributes, "eagAttributes");
    Preconditions.checkNotNull(authority, "authority");

    Http2FrameLogger frameLogger = new Http2FrameLogger(LogLevel.DEBUG, NettyClientHandler.class);
    frameReader = new Http2InboundFrameLogger(frameReader, frameLogger);
    frameWriter = new Http2OutboundFrameLogger(frameWriter, frameLogger);

    StreamBufferingEncoder encoder = new StreamBufferingEncoder(
            new DefaultHttp2ConnectionEncoder(connection, frameWriter));

    // Create the local flow controller configured to auto-refill the connection window.
    connection.local()
            .flowController(new DefaultHttp2LocalFlowController(connection, DEFAULT_WINDOW_UPDATE_RATIO, true));

    Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, frameReader);

    transportTracer.setFlowControlWindowReader(new TransportTracer.FlowControlReader() {
        final Http2FlowController local = connection.local().flowController();
        final Http2FlowController remote = connection.remote().flowController();

        @Override/*from w  w w.  java  2  s .  com*/
        public TransportTracer.FlowControlWindows read() {
            return new TransportTracer.FlowControlWindows(local.windowSize(connection.connectionStream()),
                    remote.windowSize(connection.connectionStream()));
        }
    });

    Http2Settings settings = new Http2Settings();
    settings.pushEnabled(false);
    settings.initialWindowSize(flowControlWindow);
    settings.maxConcurrentStreams(0);
    settings.maxHeaderListSize(maxHeaderListSize);

    return new NettyClientHandler(decoder, encoder, settings, lifecycleManager, keepAliveManager,
            stopwatchFactory, tooManyPingsRunnable, transportTracer, eagAttributes, authority);
}

From source file:io.grpc.netty.NettyClientHandlerTest.java

License:Apache License

@Override
protected NettyClientHandler newHandler() throws Http2Exception {
    Http2Connection connection = new DefaultHttp2Connection(false);

    // Create and close a stream previous to the nextStreamId.
    Http2Stream stream = connection.local().createStream(streamId - 2, true);
    stream.close();/*from  w  w w .j  a  v  a  2  s . co  m*/

    final Ticker ticker = new Ticker() {
        @Override
        public long read() {
            return nanoTime;
        }
    };
    Supplier<Stopwatch> stopwatchSupplier = new Supplier<Stopwatch>() {
        @Override
        public Stopwatch get() {
            return Stopwatch.createUnstarted(ticker);
        }
    };
    return NettyClientHandler.newHandler(connection, frameReader(), frameWriter(), lifecycleManager,
            mockKeepAliveManager, flowControlWindow, maxHeaderListSize, stopwatchSupplier, tooManyPingsRunnable,
            transportTracer, Attributes.EMPTY, "someauthority");
}

From source file:io.grpc.netty.NettyServerHandler.java

License:Apache License

@VisibleForTesting
static NettyServerHandler newHandler(ChannelPromise channelUnused, Http2FrameReader frameReader,
        Http2FrameWriter frameWriter, ServerTransportListener transportListener,
        List<? extends ServerStreamTracer.Factory> streamTracerFactories, TransportTracer transportTracer,
        int maxStreams, int flowControlWindow, int maxHeaderListSize, int maxMessageSize,
        long keepAliveTimeInNanos, long keepAliveTimeoutInNanos, long maxConnectionIdleInNanos,
        long maxConnectionAgeInNanos, long maxConnectionAgeGraceInNanos, boolean permitKeepAliveWithoutCalls,
        long permitKeepAliveTimeInNanos) {
    Preconditions.checkArgument(maxStreams > 0, "maxStreams must be positive");
    Preconditions.checkArgument(flowControlWindow > 0, "flowControlWindow must be positive");
    Preconditions.checkArgument(maxHeaderListSize > 0, "maxHeaderListSize must be positive");
    Preconditions.checkArgument(maxMessageSize > 0, "maxMessageSize must be positive");

    final Http2Connection connection = new DefaultHttp2Connection(true);
    WeightedFairQueueByteDistributor dist = new WeightedFairQueueByteDistributor(connection);
    dist.allocationQuantum(16 * 1024); // Make benchmarks fast again.
    DefaultHttp2RemoteFlowController controller = new DefaultHttp2RemoteFlowController(connection, dist);
    connection.remote().flowController(controller);
    final KeepAliveEnforcer keepAliveEnforcer = new KeepAliveEnforcer(permitKeepAliveWithoutCalls,
            permitKeepAliveTimeInNanos, TimeUnit.NANOSECONDS);

    // Create the local flow controller configured to auto-refill the connection window.
    connection.local()
            .flowController(new DefaultHttp2LocalFlowController(connection, DEFAULT_WINDOW_UPDATE_RATIO, true));
    frameWriter = new WriteMonitoringFrameWriter(frameWriter, keepAliveEnforcer);
    Http2ConnectionEncoder encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter);
    encoder = new Http2ControlFrameLimitEncoder(encoder, 10000);
    Http2ConnectionDecoder decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, frameReader);

    Http2Settings settings = new Http2Settings();
    settings.initialWindowSize(flowControlWindow);
    settings.maxConcurrentStreams(maxStreams);
    settings.maxHeaderListSize(maxHeaderListSize);

    return new NettyServerHandler(channelUnused, connection, transportListener, streamTracerFactories,
            transportTracer, decoder, encoder, settings, maxMessageSize, keepAliveTimeInNanos,
            keepAliveTimeoutInNanos, maxConnectionIdleInNanos, maxConnectionAgeInNanos,
            maxConnectionAgeGraceInNanos, keepAliveEnforcer);
}

From source file:io.grpc.netty.NettyServerHandler.java

License:Apache License

@Override
public void handlerAdded(final ChannelHandlerContext ctx) throws Exception {
    serverWriteQueue = new WriteQueue(ctx.channel());

    // init max connection age monitor
    if (maxConnectionAgeInNanos != MAX_CONNECTION_AGE_NANOS_DISABLED) {
        maxConnectionAgeMonitor = ctx.executor().schedule(new LogExceptionRunnable(new Runnable() {
            @Override/*w  w w .  j a  v  a  2 s . c om*/
            public void run() {
                if (gracefulShutdown == null) {
                    gracefulShutdown = new GracefulShutdown("max_age", maxConnectionAgeGraceInNanos);
                    gracefulShutdown.start(ctx);
                    ctx.flush();
                }
            }
        }), maxConnectionAgeInNanos, TimeUnit.NANOSECONDS);
    }

    if (maxConnectionIdleManager != null) {
        maxConnectionIdleManager.start(ctx);
    }

    if (keepAliveTimeInNanos != SERVER_KEEPALIVE_TIME_NANOS_DISABLED) {
        keepAliveManager = new KeepAliveManager(new KeepAlivePinger(ctx), ctx.executor(), keepAliveTimeInNanos,
                keepAliveTimeoutInNanos, true /* keepAliveDuringTransportIdle */);
        keepAliveManager.onTransportStarted();
    }

    if (transportTracer != null) {
        assert encoder().connection().equals(decoder().connection());
        final Http2Connection connection = encoder().connection();
        transportTracer.setFlowControlWindowReader(new TransportTracer.FlowControlReader() {
            private final Http2FlowController local = connection.local().flowController();
            private final Http2FlowController remote = connection.remote().flowController();

            @Override
            public TransportTracer.FlowControlWindows read() {
                assert ctx.executor().inEventLoop();
                return new TransportTracer.FlowControlWindows(local.windowSize(connection.connectionStream()),
                        remote.windowSize(connection.connectionStream()));
            }
        });
    }

    super.handlerAdded(ctx);
}

From source file:io.vertx.core.http.impl.Http2ClientConnection.java

License:Open Source License

synchronized HttpClientStream createStream() throws Http2Exception {
    Http2Connection conn = handler.connection();
    Http2Stream stream = conn.local().createStream(conn.local().incrementAndGetNextStreamId(), false);
    boolean writable = handler.encoder().flowController().isWritable(stream);
    Http2ClientStream clientStream = new Http2ClientStream(this, stream, writable);
    streams.put(clientStream.stream.id(), clientStream);
    return clientStream;
}

From source file:org.jooby.internal.netty.NettyPush.java

License:Apache License

@Override
public void push(final String method, final String path, final Map<String, Object> headers) {
    ctx.channel().eventLoop().execute(() -> {
        AsciiString streamIdHeader = HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text();
        Http2Connection connection = encoder.connection();
        int nextStreamId = connection.local().incrementAndGetNextStreamId();
        Http2Headers h2headers = new DefaultHttp2Headers().path(path).method(method).authority(authority)
                .scheme(scheme);/*from www .j  a v  a  2s  . c o m*/
        headers.forEach((n, v) -> h2headers.add(n, v.toString()));
        encoder.writePushPromise(ctx, streamId, nextStreamId, h2headers, 0, ctx.newPromise());

        // TODO: Is there another way of handling a push promise?
        DefaultFullHttpRequest pushRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
                HttpMethod.valueOf(method.toUpperCase()), path, Unpooled.EMPTY_BUFFER,
                new DefaultHttpHeaders(false).set(streamIdHeader, nextStreamId), EmptyHttpHeaders.INSTANCE);
        ctx.pipeline().fireChannelRead(pushRequest);
        ctx.pipeline().fireChannelReadComplete();
    });
}