Search in sources :

Example 46 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project async-http-client by AsyncHttpClient.

the class HttpStaticFileServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new ChunkedWriteHandler());
    pipeline.addLast(new HttpStaticFileServerHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 47 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project async-http-client by AsyncHttpClient.

the class ChannelManager method configureBootstraps.

public void configureBootstraps(NettyRequestSender requestSender) {
    final AsyncHttpClientHandler httpHandler = new HttpHandler(config, this, requestSender);
    wsHandler = new WebSocketHandler(config, this, requestSender);
    final LoggingHandler loggingHandler = new LoggingHandler(LogLevel.TRACE);
    httpBootstrap.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) {
            ChannelPipeline pipeline = ch.pipeline().addLast(HTTP_CLIENT_CODEC, newHttpClientCodec()).addLast(INFLATER_HANDLER, newHttpContentDecompressor()).addLast(CHUNKED_WRITER_HANDLER, new ChunkedWriteHandler()).addLast(AHC_HTTP_HANDLER, httpHandler);
            if (LOGGER.isTraceEnabled()) {
                pipeline.addFirst(LOGGING_HANDLER, loggingHandler);
            }
            if (config.getHttpAdditionalChannelInitializer() != null)
                config.getHttpAdditionalChannelInitializer().accept(ch);
        }
    });
    wsBootstrap.handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) {
            ChannelPipeline pipeline = ch.pipeline().addLast(HTTP_CLIENT_CODEC, newHttpClientCodec()).addLast(AHC_WS_HANDLER, wsHandler);
            if (config.isEnableWebSocketCompression()) {
                pipeline.addBefore(AHC_WS_HANDLER, WS_COMPRESSOR_HANDLER, WebSocketClientCompressionHandler.INSTANCE);
            }
            if (LOGGER.isDebugEnabled()) {
                pipeline.addFirst(LOGGING_HANDLER, loggingHandler);
            }
            if (config.getWsAdditionalChannelInitializer() != null)
                config.getWsAdditionalChannelInitializer().accept(ch);
        }
    });
}
Also used : HttpHandler(org.asynchttpclient.netty.handler.HttpHandler) LoggingHandler(io.netty.handler.logging.LoggingHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) WebSocketHandler(org.asynchttpclient.netty.handler.WebSocketHandler) AsyncHttpClientHandler(org.asynchttpclient.netty.handler.AsyncHttpClientHandler)

Example 48 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project ambry by linkedin.

the class MockChannelHandlerContext method completeRequestWithDelayedCloseTest.

/**
 * Tests the invocation of DELAYED_CLOSE when post failures happen in {@link NettyResponseChannel}.
 */
@Test
public void completeRequestWithDelayedCloseTest() throws Exception {
    Properties properties = new Properties();
    long delayMs = 500;
    properties.setProperty(NettyConfig.NETTY_SERVER_CLOSE_DELAY_TIMEOUT_MS, String.valueOf(delayMs));
    NettyConfig nettyConfig = new NettyConfig(new VerifiableProperties(properties));
    MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
    processor.setNettyConfig(nettyConfig);
    ChunkedWriteHandler chunkedWriteHandler = new ChunkedWriteHandler();
    EmbeddedChannel channel = new EmbeddedChannel(chunkedWriteHandler, processor);
    RestServiceErrorCode REST_ERROR_CODE = RestServiceErrorCode.BadRequest;
    String content = "@@randomContent@@@";
    HttpHeaders httpHeaders = new DefaultHttpHeaders();
    httpHeaders.set(MockNettyMessageProcessor.REST_SERVICE_ERROR_CODE_HEADER_NAME, REST_ERROR_CODE);
    httpHeaders.set(MockNettyMessageProcessor.INCLUDE_EXCEPTION_MESSAGE_IN_RESPONSE_HEADER_NAME, "true");
    HttpRequest httpRequest = RestTestUtils.createFullRequest(HttpMethod.POST, TestingUri.OnResponseCompleteWithRestException.toString(), httpHeaders, content.getBytes());
    channel.writeInbound(httpRequest);
    HttpResponse response = channel.readOutbound();
    assertEquals("Unexpected response status", getExpectedHttpResponseStatus(REST_ERROR_CODE), response.status());
    // channel should not be closed right away.
    assertTrue("Channel closed on the server", channel.isActive());
    // wait for delayed time * 2 times (to rule out timing out on border) and then check again.
    Thread.sleep(delayMs * 2);
    channel.runPendingTasks();
    assertFalse("Channel not closed on the server", channel.isActive());
    assertEquals("delayed close scheduled counter mismatch", 1, processor.getNettyMetrics().delayedCloseScheduledCount.getCount());
    assertEquals("delayed close executed counter mismatch", 1, processor.getNettyMetrics().delayedCloseExecutedCount.getCount());
    assertEquals("delayed close expired counter mismatch", 1, processor.getNettyMetrics().delayedCloseActivatedCount.getCount());
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) VerifiableProperties(com.github.ambry.config.VerifiableProperties) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Properties(java.util.Properties) VerifiableProperties(com.github.ambry.config.VerifiableProperties) NettyConfig(com.github.ambry.config.NettyConfig) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) DefaultHttpHeaders(io.netty.handler.codec.http.DefaultHttpHeaders) Test(org.junit.Test)

Example 49 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project ambry by linkedin.

the class MockChannelHandlerContext method createEmbeddedChannel.

/**
 * Creates a new {@link EmbeddedChannel} with a {@link ChunkedWriteHandler} and {@link MockNettyMessageProcessor} in
 * the pipeline.
 * @return the created {@link EmbeddedChannel}.
 */
private EmbeddedChannel createEmbeddedChannel() {
    ChunkedWriteHandler chunkedWriteHandler = new ChunkedWriteHandler();
    MockNettyMessageProcessor processor = new MockNettyMessageProcessor();
    return new EmbeddedChannel(chunkedWriteHandler, processor);
}
Also used : ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel)

Example 50 with ChunkedWriteHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.stream.ChunkedWriteHandler in project ambry by linkedin.

the class NettyPerfClient method start.

/**
 * Starts the NettyPerfClient.
 * @throws InterruptedException
 */
protected void start() {
    logger.info("Starting NettyPerfClient");
    reporter.start();
    group = new NioEventLoopGroup(concurrency);
    perfClientStartTime = System.currentTimeMillis();
    for (String host : hosts) {
        logger.info("Connecting to {}:{}", host, port);
        // create a new bootstrap with a fixed remote address for each host. This is the simplest way to support
        // reconnection on failure. All bootstraps will share the same event loop group.
        Bootstrap bootstrap = new Bootstrap().group(group).channel(NioSocketChannel.class).remoteAddress(host, port);
        bootstrap.handler(new ChannelInitializer<SocketChannel>() {

            @Override
            public void initChannel(SocketChannel ch) {
                logger.info("Initializing the channel to {}:{}", host, port);
                if (sslFactory != null) {
                    ch.pipeline().addLast(new SslHandler(sslFactory.createSSLEngine(host, port, SSLFactory.Mode.CLIENT)));
                }
                ch.pipeline().addLast(new HttpClientCodec()).addLast(new ChunkedWriteHandler()).addLast(new ResponseHandler(bootstrap));
            }
        });
        for (int i = 0; i < concurrency; i++) {
            ChannelFuture future = bootstrap.connect();
            future.addListener(channelConnectListener);
        }
        hostToRequestCount.put(host, new AtomicLong(0));
        hostToSleepTime.put(host, sleepTimeInMs);
    }
    if (backgroundScheduler != null) {
        backgroundScheduler.scheduleAtFixedRate(updater, 0, 1, TimeUnit.SECONDS);
        logger.info("Background scheduler is instantiated to update sleep time.");
    }
    isRunning = true;
    logger.info("Created {} channel(s) per remote host", concurrency);
    logger.info("NettyPerfClient started");
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) SslHandler(io.netty.handler.ssl.SslHandler) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) AtomicLong(java.util.concurrent.atomic.AtomicLong) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) Bootstrap(io.netty.bootstrap.Bootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)57 ChannelPipeline (io.netty.channel.ChannelPipeline)28 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)18 SslHandler (io.netty.handler.ssl.SslHandler)13 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)12 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)12 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)11 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)11 ByteBuf (io.netty.buffer.ByteBuf)10 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)10 LoggingHandler (io.netty.handler.logging.LoggingHandler)10 SocketChannel (io.netty.channel.socket.SocketChannel)8 ChannelFuture (io.netty.channel.ChannelFuture)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 Test (org.junit.Test)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)5 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)5 SslContext (io.netty.handler.ssl.SslContext)5 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)4