Search in sources :

Example 6 with ChannelConfig

use of io.netty.channel.ChannelConfig in project riposte by Nike-Inc.

the class SmartHttpContentDecompressorTest method newContentDecoder_works_as_expected.

@DataProvider(value = { "GZIP", "X_GZIP", "DEFLATE", "X_DEFLATE", "CONTENT_ENCODING_THAT_DOES_NOT_REPRESENT_COMPRESSED_PAYLOAD", "ENDPOINT_DOES_NOT_WANT_DECOMPRESS", "NULL_ENDPOINT" })
@Test
public void newContentDecoder_works_as_expected(NewContentDecoderScenario scenario) throws Exception {
    // given
    SmartHttpContentDecompressor decompressor = new SmartHttpContentDecompressor();
    TestUtil.ChannelHandlerContextMocks mocks = TestUtil.mockChannelHandlerContext();
    Whitebox.setInternalState(decompressor, "ctx", mocks.mockContext);
    ChannelMetadata channelMetadata = new ChannelMetadata(false);
    ChannelConfig channelConfigMock = mock(ChannelConfig.class);
    doReturn(scenario.endpoint).when(mocks.mockHttpProcessingState).getEndpointForExecution();
    doReturn(channelMetadata).when(mocks.mockChannel).metadata();
    doReturn(channelConfigMock).when(mocks.mockChannel).config();
    // when
    EmbeddedChannel result = decompressor.newContentDecoder(scenario.contentEncoding);
    // then
    if (scenario.expectValidDecompressor) {
        assertThat(result).isNotNull();
    } else {
        assertThat(result).isNull();
    }
}
Also used : TestUtil(com.nike.riposte.server.testutils.TestUtil) ChannelMetadata(io.netty.channel.ChannelMetadata) ChannelConfig(io.netty.channel.ChannelConfig) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 7 with ChannelConfig

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

the class BootstrapTest method testChannelOptionOrderPreserve.

@Test
public void testChannelOptionOrderPreserve() throws InterruptedException {
    final BlockingQueue<ChannelOption<?>> options = new LinkedBlockingQueue<ChannelOption<?>>();
    class ChannelConfigValidator extends DefaultChannelConfig {

        ChannelConfigValidator(Channel channel) {
            super(channel);
        }

        @Override
        public <T> boolean setOption(ChannelOption<T> option, T value) {
            options.add(option);
            return super.setOption(option, value);
        }
    }
    final CountDownLatch latch = new CountDownLatch(1);
    final Bootstrap bootstrap = new Bootstrap().handler(new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(Channel ch) {
            latch.countDown();
        }
    }).group(groupA).channelFactory(new ChannelFactory<Channel>() {

        @Override
        public Channel newChannel() {
            return new LocalChannel() {

                private ChannelConfigValidator config;

                @Override
                public synchronized ChannelConfig config() {
                    if (config == null) {
                        config = new ChannelConfigValidator(this);
                    }
                    return config;
                }
            };
        }
    }).option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 1).option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 2);
    bootstrap.register().syncUninterruptibly();
    latch.await();
    // Check the order is the same as what we defined before.
    assertSame(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, options.take());
    assertSame(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, options.take());
}
Also used : ChannelOption(io.netty.channel.ChannelOption) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) ServerChannel(io.netty.channel.ServerChannel) Channel(io.netty.channel.Channel) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultChannelConfig(io.netty.channel.DefaultChannelConfig) ChannelConfig(io.netty.channel.ChannelConfig) DefaultChannelConfig(io.netty.channel.DefaultChannelConfig) Test(org.junit.jupiter.api.Test)

Example 8 with ChannelConfig

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

the class AbstractOioByteChannel method doRead.

@Override
protected void doRead() {
    final ChannelConfig config = config();
    if (isInputShutdown() || !readPending) {
        // during the same read loop readPending was set to false.
        return;
    }
    // In OIO we should set readPending to false even if the read was not successful so we can schedule
    // another read on the event loop if no reads are done.
    readPending = false;
    final ChannelPipeline pipeline = pipeline();
    final ByteBufAllocator allocator = config.getAllocator();
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
    allocHandle.reset(config);
    ByteBuf byteBuf = null;
    boolean close = false;
    boolean readData = false;
    try {
        byteBuf = allocHandle.allocate(allocator);
        do {
            allocHandle.lastBytesRead(doReadBytes(byteBuf));
            if (allocHandle.lastBytesRead() <= 0) {
                if (!byteBuf.isReadable()) {
                    // nothing was read. release the buffer.
                    byteBuf.release();
                    byteBuf = null;
                    close = allocHandle.lastBytesRead() < 0;
                    if (close) {
                        // There is nothing left to read as we received an EOF.
                        readPending = false;
                    }
                }
                break;
            } else {
                readData = true;
            }
            final int available = available();
            if (available <= 0) {
                break;
            }
            // Oio collects consecutive read operations into 1 ByteBuf before propagating up the pipeline.
            if (!byteBuf.isWritable()) {
                final int capacity = byteBuf.capacity();
                final int maxCapacity = byteBuf.maxCapacity();
                if (capacity == maxCapacity) {
                    allocHandle.incMessagesRead(1);
                    readPending = false;
                    pipeline.fireChannelRead(byteBuf);
                    byteBuf = allocHandle.allocate(allocator);
                } else {
                    final int writerIndex = byteBuf.writerIndex();
                    if (writerIndex + available > maxCapacity) {
                        byteBuf.capacity(maxCapacity);
                    } else {
                        byteBuf.ensureWritable(available);
                    }
                }
            }
        } while (allocHandle.continueReading());
        if (byteBuf != null) {
            // it because allocHandle.continueReading() returned false.
            if (byteBuf.isReadable()) {
                readPending = false;
                pipeline.fireChannelRead(byteBuf);
            } else {
                byteBuf.release();
            }
            byteBuf = null;
        }
        if (readData) {
            allocHandle.readComplete();
            pipeline.fireChannelReadComplete();
        }
        if (close) {
            closeOnRead(pipeline);
        }
    } catch (Throwable t) {
        handleReadException(pipeline, byteBuf, t, close, allocHandle);
    } finally {
        if (readPending || config.isAutoRead() || !readData && isActive()) {
            // Reading 0 bytes could mean there is a SocketTimeout and no data was actually read, so we
            // should execute read() again because no data may have been read.
            read();
        }
    }
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) RecvByteBufAllocator(io.netty.channel.RecvByteBufAllocator) ChannelConfig(io.netty.channel.ChannelConfig) ByteBuf(io.netty.buffer.ByteBuf) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 9 with ChannelConfig

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

the class GlobalChannelTrafficShapingHandler method channelRead.

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
    long size = calculateSize(msg);
    long now = TrafficCounter.milliSecondFromNano();
    if (size > 0) {
        // compute the number of ms to wait before reopening the channel
        long waitGlobal = trafficCounter.readTimeToWait(size, getReadLimit(), maxTime, now);
        Integer key = ctx.channel().hashCode();
        PerChannel perChannel = channelQueues.get(key);
        long wait = 0;
        if (perChannel != null) {
            wait = perChannel.channelTrafficCounter.readTimeToWait(size, readChannelLimit, maxTime, now);
            if (readDeviationActive) {
                // now try to balance between the channels
                long maxLocalRead;
                maxLocalRead = perChannel.channelTrafficCounter.cumulativeReadBytes();
                long maxGlobalRead = cumulativeReadBytes.get();
                if (maxLocalRead <= 0) {
                    maxLocalRead = 0;
                }
                if (maxGlobalRead < maxLocalRead) {
                    maxGlobalRead = maxLocalRead;
                }
                wait = computeBalancedWait(maxLocalRead, maxGlobalRead, wait);
            }
        }
        if (wait < waitGlobal) {
            wait = waitGlobal;
        }
        wait = checkWaitReadTime(ctx, wait, now);
        if (wait >= MINIMAL_WAIT) {
            // At least 10ms seems a minimal
            // time in order to try to limit the traffic
            // Only AutoRead AND HandlerActive True means Context Active
            Channel channel = ctx.channel();
            ChannelConfig config = channel.config();
            if (logger.isDebugEnabled()) {
                logger.debug("Read Suspend: " + wait + ':' + config.isAutoRead() + ':' + isHandlerActive(ctx));
            }
            if (config.isAutoRead() && isHandlerActive(ctx)) {
                config.setAutoRead(false);
                channel.attr(READ_SUSPENDED).set(true);
                // Create a Runnable to reactive the read if needed. If one was create before it will just be
                // reused to limit object creation
                Attribute<Runnable> attr = channel.attr(REOPEN_TASK);
                Runnable reopenTask = attr.get();
                if (reopenTask == null) {
                    reopenTask = new ReopenReadTimerTask(ctx);
                    attr.set(reopenTask);
                }
                ctx.executor().schedule(reopenTask, wait, TimeUnit.MILLISECONDS);
                if (logger.isDebugEnabled()) {
                    logger.debug("Suspend final status => " + config.isAutoRead() + ':' + isHandlerActive(ctx) + " will reopened at: " + wait);
                }
            }
        }
    }
    informReadOperation(ctx, now);
    ctx.fireChannelRead(msg);
}
Also used : ChannelConfig(io.netty.channel.ChannelConfig) Channel(io.netty.channel.Channel)

Example 10 with ChannelConfig

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

the class AbstractTrafficShapingHandler method channelRead.

@Override
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
    long size = calculateSize(msg);
    long now = TrafficCounter.milliSecondFromNano();
    if (size > 0) {
        // compute the number of ms to wait before reopening the channel
        long wait = trafficCounter.readTimeToWait(size, readLimit, maxTime, now);
        wait = checkWaitReadTime(ctx, wait, now);
        if (wait >= MINIMAL_WAIT) {
            // At least 10ms seems a minimal
            // time in order to try to limit the traffic
            // Only AutoRead AND HandlerActive True means Context Active
            Channel channel = ctx.channel();
            ChannelConfig config = channel.config();
            if (logger.isDebugEnabled()) {
                logger.debug("Read suspend: " + wait + ':' + config.isAutoRead() + ':' + isHandlerActive(ctx));
            }
            if (config.isAutoRead() && isHandlerActive(ctx)) {
                config.setAutoRead(false);
                channel.attr(READ_SUSPENDED).set(true);
                // Create a Runnable to reactive the read if needed. If one was create before it will just be
                // reused to limit object creation
                Attribute<Runnable> attr = channel.attr(REOPEN_TASK);
                Runnable reopenTask = attr.get();
                if (reopenTask == null) {
                    reopenTask = new ReopenReadTimerTask(ctx);
                    attr.set(reopenTask);
                }
                ctx.executor().schedule(reopenTask, wait, TimeUnit.MILLISECONDS);
                if (logger.isDebugEnabled()) {
                    logger.debug("Suspend final status => " + config.isAutoRead() + ':' + isHandlerActive(ctx) + " will reopened at: " + wait);
                }
            }
        }
    }
    informReadOperation(ctx, now);
    ctx.fireChannelRead(msg);
}
Also used : ChannelConfig(io.netty.channel.ChannelConfig) Channel(io.netty.channel.Channel)

Aggregations

ChannelConfig (io.netty.channel.ChannelConfig)10 Channel (io.netty.channel.Channel)4 ByteBuf (io.netty.buffer.ByteBuf)3 ChannelOption (io.netty.channel.ChannelOption)3 Test (org.junit.Test)3 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelMetadata (io.netty.channel.ChannelMetadata)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 DefaultChannelConfig (io.netty.channel.DefaultChannelConfig)2 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)2 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)2 TestUtil (com.nike.riposte.server.testutils.TestUtil)1 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)1 InternalChannelz (io.grpc.InternalChannelz)1 TransportTracer (io.grpc.internal.TransportTracer)1 LocalSocketPicker (io.grpc.netty.NettyChannelBuilder.LocalSocketPicker)1 NativeSocketOptions (io.grpc.netty.NettySocketSupport.NativeSocketOptions)1 TrackingObjectPoolForTest (io.grpc.netty.NettyTestUtil.TrackingObjectPoolForTest)1 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 Unpooled (io.netty.buffer.Unpooled)1