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());
}
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;
}
}
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;
}
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);
}
}
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();
}
}
}
Aggregations