Search in sources :

Example 16 with WriteBufferWaterMark

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

the class SocketShutdownOutputBySelfTest method testWriteAfterShutdownOutputNoWritabilityChange.

public void testWriteAfterShutdownOutputNoWritabilityChange(Bootstrap cb) throws Throwable {
    final TestHandler h = new TestHandler();
    ServerSocket ss = new ServerSocket();
    Socket s = null;
    SocketChannel ch = null;
    try {
        ss.bind(newSocketAddress());
        cb.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(2, 4));
        ch = (SocketChannel) cb.handler(h).connect(ss.getLocalSocketAddress()).sync().channel();
        assumeFalse(ch instanceof OioSocketChannel);
        assertTrue(ch.isActive());
        assertFalse(ch.isOutputShutdown());
        s = ss.accept();
        byte[] expectedBytes = new byte[] { 1, 2, 3, 4, 5, 6 };
        ChannelFuture writeFuture = ch.write(Unpooled.wrappedBuffer(expectedBytes));
        h.assertWritability(false);
        ch.flush();
        writeFuture.sync();
        h.assertWritability(true);
        for (int i = 0; i < expectedBytes.length; ++i) {
            assertEquals(expectedBytes[i], s.getInputStream().read());
        }
        assertTrue(h.ch.isOpen());
        assertTrue(h.ch.isActive());
        assertFalse(h.ch.isInputShutdown());
        assertFalse(h.ch.isOutputShutdown());
        // Make the connection half-closed and ensure read() returns -1.
        ch.shutdownOutput().sync();
        assertEquals(-1, s.getInputStream().read());
        assertTrue(h.ch.isOpen());
        assertTrue(h.ch.isActive());
        assertFalse(h.ch.isInputShutdown());
        assertTrue(h.ch.isOutputShutdown());
        try {
            // If half-closed, the local endpoint shouldn't be able to write
            ch.writeAndFlush(Unpooled.wrappedBuffer(new byte[] { 2 })).sync();
            fail();
        } catch (Throwable cause) {
            checkThrowable(cause);
        }
        assertNull(h.writabilityQueue.poll());
    } finally {
        if (s != null) {
            s.close();
        }
        if (ch != null) {
            ch.close();
        }
        ss.close();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) OioSocketChannel(io.netty.channel.socket.oio.OioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) OioSocketChannel(io.netty.channel.socket.oio.OioSocketChannel) ServerSocket(java.net.ServerSocket) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket)

Example 17 with WriteBufferWaterMark

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

the class TransportWriteThrottle method install.

@Override
public void install(Channel channel, MemoryTracker memoryTracker) {
    ThrottleLock lock = lockSupplier.get();
    memoryTracker.allocateHeap(HeapEstimator.sizeOf(lock) + WRITE_BUFFER_WATER_MARK_SHALLOW_SIZE);
    channel.attr(LOCK_KEY).set(lock);
    channel.config().setWriteBufferWaterMark(new WriteBufferWaterMark(lowWaterMark, highWaterMark));
    channel.pipeline().addLast(listener);
}
Also used : WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark)

Example 18 with WriteBufferWaterMark

use of io.netty.channel.WriteBufferWaterMark in project grpc-java by grpc.

the class UtilsTest method setAndValidateGeneric.

private static InternalChannelz.SocketOptions setAndValidateGeneric(Channel channel) {
    channel.config().setOption(ChannelOption.SO_LINGER, 3);
    // only applicable for OIO channels:
    channel.config().setOption(ChannelOption.SO_TIMEOUT, 250);
    // Test some arbitrarily chosen options with a non numeric values
    channel.config().setOption(ChannelOption.SO_KEEPALIVE, true);
    WriteBufferWaterMark writeBufWaterMark = new WriteBufferWaterMark(10, 20);
    channel.config().setOption(ChannelOption.WRITE_BUFFER_WATER_MARK, writeBufWaterMark);
    InternalChannelz.SocketOptions socketOptions = Utils.getSocketOptions(channel);
    assertEquals(3, (int) socketOptions.lingerSeconds);
    assertEquals("true", socketOptions.others.get("SO_KEEPALIVE"));
    assertEquals(writeBufWaterMark.toString(), socketOptions.others.get(ChannelOption.WRITE_BUFFER_WATER_MARK.toString()));
    return socketOptions;
}
Also used : SocketOptions(io.grpc.InternalChannelz.SocketOptions) InternalChannelz(io.grpc.InternalChannelz) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark)

Example 19 with WriteBufferWaterMark

use of io.netty.channel.WriteBufferWaterMark in project grpc-java by grpc.

the class NettyServerTest method connectionSettingsPropagated.

@Test(timeout = 60000)
public void connectionSettingsPropagated() throws Exception {
    final int originalLowWaterMark = 2097169;
    final int originalHighWaterMark = 2097211;
    Map<ChannelOption<?>, Object> childChannelOptions = new HashMap<>();
    childChannelOptions.put(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(originalLowWaterMark, originalHighWaterMark));
    class TestChannelHandler extends ChannelHandlerAdapter {

        CountDownLatch countDownLatch = new CountDownLatch(1);

        int lowWaterMark;

        int highWaterMark;

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) {
            Channel channel = ctx.channel();
            WriteBufferWaterMark writeBufferWaterMark = channel.config().getOption(ChannelOption.WRITE_BUFFER_WATER_MARK);
            lowWaterMark = writeBufferWaterMark.low();
            highWaterMark = writeBufferWaterMark.high();
            countDownLatch.countDown();
        }
    }
    final TestChannelHandler channelHandler = new TestChannelHandler();
    class TestProtocolNegotiator implements ProtocolNegotiator {

        Attributes eagAttributes;

        @Override
        public ChannelHandler newHandler(GrpcHttp2ConnectionHandler handler) {
            eagAttributes = handler.getEagAttributes();
            return channelHandler;
        }

        @Override
        public void close() {
        }

        @Override
        public AsciiString scheme() {
            return Utils.HTTP;
        }
    }
    Attributes eagAttributes = Attributes.newBuilder().set(Attributes.Key.create("foo"), "bar").build();
    TestProtocolNegotiator protocolNegotiator = new TestProtocolNegotiator();
    InetSocketAddress addr = new InetSocketAddress(0);
    NettyServer ns = new NettyServer(Arrays.asList(addr), new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap<ChannelOption<?>, Object>(), childChannelOptions, new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, protocolNegotiator, Collections.<ServerStreamTracer.Factory>emptyList(), TransportTracer.getDefaultFactory(), // ignore
    1, // ignore
    false, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    1, // ignore
    true, // ignore
    0, eagAttributes, channelz);
    ns.start(new ServerListener() {

        @Override
        public ServerTransportListener transportCreated(ServerTransport transport) {
            return new NoopServerTransportListener();
        }

        @Override
        public void serverShutdown() {
        }
    });
    Socket socket = new Socket();
    socket.connect(ns.getListenSocketAddress(), /* timeout= */
    8000);
    channelHandler.countDownLatch.await();
    socket.close();
    assertThat(protocolNegotiator.eagAttributes).isSameInstanceAs(eagAttributes);
    assertThat(channelHandler.lowWaterMark).isEqualTo(originalLowWaterMark);
    assertThat(channelHandler.highWaterMark).isEqualTo(originalHighWaterMark);
    ns.shutdown();
}
Also used : ChannelOption(io.netty.channel.ChannelOption) HashMap(java.util.HashMap) ServerTransport(io.grpc.internal.ServerTransport) InetSocketAddress(java.net.InetSocketAddress) Attributes(io.grpc.Attributes) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) ServerListener(io.grpc.internal.ServerListener) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerStreamTracer(io.grpc.ServerStreamTracer) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) CountDownLatch(java.util.concurrent.CountDownLatch) ServerTransportListener(io.grpc.internal.ServerTransportListener) Socket(java.net.Socket) Test(org.junit.Test)

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