Search in sources :

Example 11 with WriteBufferWaterMark

use of io.netty.channel.WriteBufferWaterMark in project netty by netty.

the class Http2MultiplexCodecTest method settingChannelOptsAndAttrsOnBootstrap.

@Test
public void settingChannelOptsAndAttrsOnBootstrap() {
    AttributeKey<String> key = AttributeKey.newInstance("foo");
    WriteBufferWaterMark mark = new WriteBufferWaterMark(1024, 4096);
    Http2StreamChannelBootstrap b = new Http2StreamChannelBootstrap();
    b.parentChannel(parentChannel).handler(childChannelInitializer).option(ChannelOption.AUTO_READ, false).option(ChannelOption.WRITE_BUFFER_WATER_MARK, mark).attr(key, "bar");
    Channel channel = b.connect().channel();
    assertFalse(channel.config().isAutoRead());
    assertSame(mark, channel.config().getWriteBufferWaterMark());
    assertEquals("bar", channel.attr(key).get());
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) AsciiString(io.netty.util.AsciiString) Test(org.junit.Test)

Example 12 with WriteBufferWaterMark

use of io.netty.channel.WriteBufferWaterMark in project ambry by linkedin.

the class ChannelWriteCallback method handleRequest.

/**
 * Handles a {@link HttpRequest}. If content is awaited, handles some state maintenance. Else handles the request
 * according to a predefined flow based on the uri.
 * @param httpRequest the {@link HttpRequest} that needs to be handled.
 * @throws Exception
 */
private void handleRequest(HttpRequest httpRequest) throws Exception {
    writeCallbacksToVerify.clear();
    request = new NettyRequest(httpRequest, ctx.channel(), nettyMetrics, Collections.emptySet());
    restResponseChannel = new NettyResponseChannel(ctx, nettyMetrics);
    restResponseChannel.setRequest(request);
    restResponseChannel.setHeader(RestUtils.Headers.CONTENT_TYPE, "application/octet-stream");
    TestingUri uri = TestingUri.getTestingURI(request.getUri());
    switch(uri) {
        case Close:
            restResponseChannel.close();
            assertFalse("Request channel is not closed", request.isOpen());
            break;
        case CopyHeaders:
            copyHeaders(httpRequest);
            restResponseChannel.onResponseComplete(null);
            assertFalse("Request channel is not closed", request.isOpen());
            break;
        case ImmediateResponseComplete:
            int chunkCount = httpRequest.headers().getInt(CHUNK_COUNT_HEADER_NAME, -1);
            if (chunkCount > 0) {
                restResponseChannel.onResponseComplete(new RestServiceException("Invalid value for header : [" + CHUNK_COUNT_HEADER_NAME + "]. Can only be 0 for [/" + uri + "]", RestServiceErrorCode.BadRequest));
            } else if (chunkCount == 0) {
                restResponseChannel.setHeader(RestUtils.Headers.CONTENT_LENGTH, 0);
            }
            restResponseChannel.onResponseComplete(null);
            assertEquals("ResponseStatus differs from default", ResponseStatus.Ok, restResponseChannel.getStatus());
            assertFalse("Request channel is not closed", request.isOpen());
            break;
        case FillWriteBuffer:
            WriteBufferWaterMark writeBufferWaterMark = new WriteBufferWaterMark(1, 2);
            ctx.channel().config().setWriteBufferWaterMark(writeBufferWaterMark);
            break;
        case ModifyResponseMetadataAfterWrite:
            restResponseChannel.write(ByteBuffer.wrap(new byte[0]), null);
            restResponseChannel.setHeader(RestUtils.Headers.CONTENT_TYPE, "application/octet-stream");
            break;
        case MultipleClose:
            restResponseChannel.onResponseComplete(null);
            assertFalse("Request channel is not closed", request.isOpen());
            restResponseChannel.close();
            restResponseChannel.close();
            break;
        case MultipleOnResponseComplete:
            restResponseChannel.onResponseComplete(null);
            assertFalse("Request channel is not closed", request.isOpen());
            restResponseChannel.onResponseComplete(null);
            break;
        case OnResponseCompleteWithRestException:
            String errorCodeStr = (String) request.getArgs().get(REST_SERVICE_ERROR_CODE_HEADER_NAME);
            RestServiceErrorCode errorCode = RestServiceErrorCode.valueOf(errorCodeStr);
            restResponseChannel.onResponseComplete(new RestServiceException(errorCodeStr, errorCode));
            assertEquals("ResponseStatus does not reflect error", ResponseStatus.getResponseStatus(errorCode), restResponseChannel.getStatus());
            assertFalse("Request channel is not closed", request.isOpen());
            break;
        case OnResponseCompleteWithNonRestException:
            restResponseChannel.onResponseComplete(new RuntimeException(TestingUri.OnResponseCompleteWithNonRestException.toString()));
            assertEquals("ResponseStatus does not reflect error", ResponseStatus.InternalServerError, restResponseChannel.getStatus());
            assertFalse("Request channel is not closed", request.isOpen());
            break;
        case OnResponseCompleteWithEarlyClientTermination:
            restResponseChannel.onResponseComplete(Utils.convertToClientTerminationException(new Exception()));
            assertEquals("ResponseStatus does not reflect error", ResponseStatus.InternalServerError, restResponseChannel.getStatus());
            assertFalse("Request channel is not closed", request.isOpen());
            break;
        case ResponseFailureMidway:
            ChannelWriteCallback callback = new ChannelWriteCallback();
            callback.setFuture(restResponseChannel.write(ByteBuffer.wrap(TestingUri.ResponseFailureMidway.toString().getBytes()), callback));
            writeCallbacksToVerify.add(callback);
            restResponseChannel.onResponseComplete(new Exception());
            // this should close the channel and the test will check for that.
            break;
        case ResponseWithContentLength:
            chunkCount = httpRequest.headers().getInt(CHUNK_COUNT_HEADER_NAME, -1);
            if (chunkCount == -1) {
                restResponseChannel.onResponseComplete(new RestServiceException("Request should contain header : [" + CHUNK_COUNT_HEADER_NAME + "]", RestServiceErrorCode.BadRequest));
            } else {
                restResponseChannel.setHeader(RestUtils.Headers.CONTENT_LENGTH, chunkCount * CHUNK.length);
                if (chunkCount == 0) {
                    // special case check
                    callback = new ChannelWriteCallback();
                    callback.setFuture(restResponseChannel.write(ByteBuffer.allocate(0), callback));
                    writeCallbacksToVerify.add(callback);
                } else {
                    for (int i = 0; i < chunkCount; i++) {
                        callback = new ChannelWriteCallback();
                        callback.setFuture(restResponseChannel.write(ByteBuffer.wrap(CHUNK), callback));
                        writeCallbacksToVerify.add(callback);
                    }
                }
                restResponseChannel.onResponseComplete(null);
            }
            assertFalse("Request channel is not closed", request.isOpen());
            break;
        case SetNullHeader:
            setNullHeaders();
            break;
        case SetRequest:
            setRequestTest();
            break;
        case SetStatus:
            restResponseChannel.setStatus(ResponseStatus.valueOf(httpRequest.headers().get(STATUS_HEADER_NAME)));
            restResponseChannel.onResponseComplete(null);
            assertFalse("Request channel is not closed", request.isOpen());
            break;
        case WriteAfterClose:
            restResponseChannel.close();
            assertFalse("Request channel is not closed", request.isOpen());
            callback = new ChannelWriteCallback();
            callback.setFuture(restResponseChannel.write(ByteBuffer.wrap(TestingUri.WriteAfterClose.toString().getBytes()), callback));
            writeCallbacksToVerify.add(callback);
            break;
        case WriteFailureWithThrowable:
            callback = new ChannelWriteCallback();
            callback.setFuture(restResponseChannel.write(ByteBuffer.wrap(TestingUri.WriteFailureWithThrowable.toString().getBytes()), callback));
            writeCallbacksToVerify.add(callback);
            break;
        case WriteMoreThanContentLength:
            chunkCount = httpRequest.headers().getInt(CHUNK_COUNT_HEADER_NAME, -1);
            if (chunkCount == -1) {
                restResponseChannel.onResponseComplete(new RestServiceException("Request should contain header : [" + CHUNK_COUNT_HEADER_NAME + "]", RestServiceErrorCode.BadRequest));
            } else {
                restResponseChannel.setHeader(RestUtils.Headers.CONTENT_LENGTH, chunkCount * CHUNK.length);
                // write one more chunk than required.
                for (int i = 0; i <= chunkCount; i++) {
                    callback = new ChannelWriteCallback();
                    callback.setFuture(restResponseChannel.write(ByteBuffer.wrap(CHUNK), callback));
                    writeCallbacksToVerify.add(callback);
                }
                restResponseChannel.onResponseComplete(null);
            }
            break;
    }
}
Also used : WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) TimeoutException(java.util.concurrent.TimeoutException) ParseException(java.text.ParseException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with WriteBufferWaterMark

use of io.netty.channel.WriteBufferWaterMark in project atomix by atomix.

the class NettyMessagingService method bootstrapClient.

private Bootstrap bootstrapClient(Endpoint endpoint) {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    bootstrap.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(10 * 32 * 1024, 10 * 64 * 1024));
    bootstrap.option(ChannelOption.SO_RCVBUF, 1024 * 1024);
    bootstrap.option(ChannelOption.SO_SNDBUF, 1024 * 1024);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000);
    bootstrap.group(clientGroup);
    // TODO: Make this faster:
    // http://normanmaurer.me/presentations/2014-facebook-eng-netty/slides.html#37.0
    bootstrap.channel(clientChannelClass);
    bootstrap.remoteAddress(endpoint.host(), endpoint.port());
    if (enableNettyTls) {
        bootstrap.handler(new SslClientCommunicationChannelInitializer());
    } else {
        bootstrap.handler(new BasicChannelInitializer());
    }
    return bootstrap;
}
Also used : Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark)

Example 14 with WriteBufferWaterMark

use of io.netty.channel.WriteBufferWaterMark in project pinpoint by naver.

the class DefaultChannelFactory method setupClientOption.

private void setupClientOption(final NettyChannelBuilder channelBuilder) {
    channelBuilder.keepAliveTime(clientOption.getKeepAliveTime(), TimeUnit.MILLISECONDS);
    channelBuilder.keepAliveTimeout(clientOption.getKeepAliveTimeout(), TimeUnit.MILLISECONDS);
    channelBuilder.keepAliveWithoutCalls(clientOption.isKeepAliveWithoutCalls());
    channelBuilder.maxInboundMetadataSize(clientOption.getMaxHeaderListSize());
    channelBuilder.maxInboundMessageSize(clientOption.getMaxInboundMessageSize());
    channelBuilder.flowControlWindow(clientOption.getFlowControlWindow());
    channelBuilder.idleTimeout(clientOption.getIdleTimeoutMillis(), TimeUnit.MILLISECONDS);
    // ChannelOption
    channelBuilder.withOption(ChannelOption.TCP_NODELAY, true);
    channelBuilder.withOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, clientOption.getConnectTimeout());
    final WriteBufferWaterMark writeBufferWaterMark = new WriteBufferWaterMark(clientOption.getWriteBufferLowWaterMark(), clientOption.getWriteBufferHighWaterMark());
    channelBuilder.withOption(ChannelOption.WRITE_BUFFER_WATER_MARK, writeBufferWaterMark);
    if (logger.isInfoEnabled()) {
        logger.info("Set clientOption {}. name={}", clientOption, factoryName);
    }
}
Also used : WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark)

Example 15 with WriteBufferWaterMark

use of io.netty.channel.WriteBufferWaterMark in project netty by netty.

the class SocketConditionalWritabilityTest method testConditionalWritability.

public void testConditionalWritability(ServerBootstrap sb, Bootstrap cb) throws Throwable {
    Channel serverChannel = null;
    Channel clientChannel = null;
    try {
        final int expectedBytes = 100 * 1024 * 1024;
        final int maxWriteChunkSize = 16 * 1024;
        final CountDownLatch latch = new CountDownLatch(1);
        sb.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024, 16 * 1024));
        sb.childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(new ChannelDuplexHandler() {

                    private int bytesWritten;

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) {
                        ReferenceCountUtil.release(msg);
                        writeRemainingBytes(ctx);
                    }

                    @Override
                    public void flush(ChannelHandlerContext ctx) {
                        if (ctx.channel().isWritable()) {
                            writeRemainingBytes(ctx);
                        } else {
                            ctx.flush();
                        }
                    }

                    @Override
                    public void channelWritabilityChanged(ChannelHandlerContext ctx) {
                        if (ctx.channel().isWritable()) {
                            writeRemainingBytes(ctx);
                        }
                        ctx.fireChannelWritabilityChanged();
                    }

                    private void writeRemainingBytes(ChannelHandlerContext ctx) {
                        while (ctx.channel().isWritable() && bytesWritten < expectedBytes) {
                            int chunkSize = Math.min(expectedBytes - bytesWritten, maxWriteChunkSize);
                            bytesWritten += chunkSize;
                            ctx.write(ctx.alloc().buffer(chunkSize).writeZero(chunkSize));
                        }
                        ctx.flush();
                    }
                });
            }
        });
        serverChannel = sb.bind().syncUninterruptibly().channel();
        cb.handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    private int totalRead;

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) {
                        ctx.writeAndFlush(ctx.alloc().buffer(1).writeByte(0));
                    }

                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) {
                        if (msg instanceof ByteBuf) {
                            totalRead += ((ByteBuf) msg).readableBytes();
                            if (totalRead == expectedBytes) {
                                latch.countDown();
                            }
                        }
                        ReferenceCountUtil.release(msg);
                    }
                });
            }
        });
        clientChannel = cb.connect(serverChannel.localAddress()).syncUninterruptibly().channel();
        latch.await();
    } finally {
        if (serverChannel != null) {
            serverChannel.close();
        }
        if (clientChannel != null) {
            clientChannel.close();
        }
    }
}
Also used : Channel(io.netty.channel.Channel) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Aggregations

WriteBufferWaterMark (io.netty.channel.WriteBufferWaterMark)19 Channel (io.netty.channel.Channel)7 Bootstrap (io.netty.bootstrap.Bootstrap)5 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)5 ChannelFuture (io.netty.channel.ChannelFuture)5 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)4 InetSocketAddress (java.net.InetSocketAddress)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 ChannelPipeline (io.netty.channel.ChannelPipeline)3 EventLoopGroup (io.netty.channel.EventLoopGroup)3 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)3 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)3 SslContext (io.netty.handler.ssl.SslContext)3 IOException (java.io.IOException)3 ByteBuf (io.netty.buffer.ByteBuf)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)2 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)2 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)2 ChannelInitializer (io.netty.channel.ChannelInitializer)2 ChannelOption (io.netty.channel.ChannelOption)2