Search in sources :

Example 6 with ChannelOption

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project grpc-java by grpc.

the class NettyClientTransportTest method setSoLingerChannelOption.

@Test
public void setSoLingerChannelOption() throws IOException {
    startServer();
    Map<ChannelOption<?>, Object> channelOptions = new HashMap<>();
    // set SO_LINGER option
    int soLinger = 123;
    channelOptions.put(ChannelOption.SO_LINGER, soLinger);
    NettyClientTransport transport = new NettyClientTransport(address, new ReflectiveChannelFactory<>(NioSocketChannel.class), channelOptions, group, newNegotiator(), false, DEFAULT_WINDOW_SIZE, DEFAULT_MAX_MESSAGE_SIZE, GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE, KEEPALIVE_TIME_NANOS_DISABLED, 1L, false, authority, null, /* user agent */
    tooManyPingsRunnable, new TransportTracer(), Attributes.EMPTY, new SocketPicker(), new FakeChannelLogger(), false);
    transports.add(transport);
    callMeMaybe(transport.start(clientTransportListener));
    // verify SO_LINGER has been set
    ChannelConfig config = transport.channel().config();
    assertTrue(config instanceof SocketChannelConfig);
    assertEquals(soLinger, ((SocketChannelConfig) config).getSoLinger());
}
Also used : ChannelOption(io.netty.channel.ChannelOption) HashMap(java.util.HashMap) LocalSocketPicker(io.grpc.netty.NettyChannelBuilder.LocalSocketPicker) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannelConfig(io.netty.channel.socket.SocketChannelConfig) ChannelConfig(io.netty.channel.ChannelConfig) SocketChannelConfig(io.netty.channel.socket.SocketChannelConfig) TransportTracer(io.grpc.internal.TransportTracer) TrackingObjectPoolForTest(io.grpc.netty.NettyTestUtil.TrackingObjectPoolForTest) Test(org.junit.Test)

Example 7 with ChannelOption

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project grpc-java by grpc.

the class NettyClientTransportTest method failingToConstructChannelShouldFailGracefully.

@Test
public void failingToConstructChannelShouldFailGracefully() throws Exception {
    address = TestUtils.testServerAddress(new InetSocketAddress(12345));
    authority = GrpcUtil.authorityFromHostAndPort(address.getHostString(), address.getPort());
    NettyClientTransport transport = new NettyClientTransport(address, new ReflectiveChannelFactory<>(CantConstructChannel.class), new HashMap<ChannelOption<?>, Object>(), group, newNegotiator(), false, DEFAULT_WINDOW_SIZE, DEFAULT_MAX_MESSAGE_SIZE, GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE, KEEPALIVE_TIME_NANOS_DISABLED, 1, false, authority, null, tooManyPingsRunnable, new TransportTracer(), Attributes.EMPTY, new SocketPicker(), new FakeChannelLogger(), false);
    transports.add(transport);
    // Should not throw
    callMeMaybe(transport.start(clientTransportListener));
    // And RPCs and PINGs should fail cleanly, reporting the failure
    Rpc rpc = new Rpc(transport);
    try {
        rpc.waitForResponse();
        fail("Expected exception");
    } catch (Exception ex) {
        if (!(getRootCause(ex) instanceof CantConstructChannelError)) {
            throw new AssertionError("Could not find expected error", ex);
        }
    }
    final SettableFuture<Object> pingResult = SettableFuture.create();
    FakeClock clock = new FakeClock();
    ClientTransport.PingCallback pingCallback = new ClientTransport.PingCallback() {

        @Override
        public void onSuccess(long roundTripTimeNanos) {
            pingResult.set(roundTripTimeNanos);
        }

        @Override
        public void onFailure(Throwable cause) {
            pingResult.setException(cause);
        }
    };
    transport.ping(pingCallback, clock.getScheduledExecutorService());
    assertFalse(pingResult.isDone());
    clock.runDueTasks();
    assertTrue(pingResult.isDone());
    try {
        pingResult.get();
        fail("Expected exception");
    } catch (Exception ex) {
        if (!(getRootCause(ex) instanceof CantConstructChannelError)) {
            throw new AssertionError("Could not find expected error", ex);
        }
    }
}
Also used : ChannelOption(io.netty.channel.ChannelOption) InetSocketAddress(java.net.InetSocketAddress) FakeClock(io.grpc.internal.FakeClock) LocalSocketPicker(io.grpc.netty.NettyChannelBuilder.LocalSocketPicker) ClientTransport(io.grpc.internal.ClientTransport) ManagedClientTransport(io.grpc.internal.ManagedClientTransport) TimeoutException(java.util.concurrent.TimeoutException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SSLException(javax.net.ssl.SSLException) StatusException(io.grpc.StatusException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TransportTracer(io.grpc.internal.TransportTracer) TrackingObjectPoolForTest(io.grpc.netty.NettyTestUtil.TrackingObjectPoolForTest) Test(org.junit.Test)

Example 8 with ChannelOption

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project spring-boot by spring-projects.

the class NettyWebServerFactoryCustomizerTests method verifyConnectionTimeout.

private void verifyConnectionTimeout(NettyReactiveWebServerFactory factory, Integer expected) {
    if (expected == null) {
        then(factory).should(never()).addServerCustomizers(any(NettyServerCustomizer.class));
        return;
    }
    then(factory).should(times(2)).addServerCustomizers(this.customizerCaptor.capture());
    NettyServerCustomizer serverCustomizer = this.customizerCaptor.getAllValues().get(0);
    HttpServer httpServer = serverCustomizer.apply(HttpServer.create());
    Map<ChannelOption<?>, ?> options = httpServer.configuration().options();
    assertThat(options.get(ChannelOption.CONNECT_TIMEOUT_MILLIS)).isEqualTo(expected);
}
Also used : NettyServerCustomizer(org.springframework.boot.web.embedded.netty.NettyServerCustomizer) ChannelOption(io.netty.channel.ChannelOption) HttpServer(reactor.netty.http.server.HttpServer)

Example 9 with ChannelOption

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project java by wavefrontHQ.

the class TcpIngester method run.

public void run() {
    activeListeners.inc();
    ServerBootstrap b = new ServerBootstrap();
    EventLoopGroup parentGroup;
    EventLoopGroup childGroup;
    Class<? extends ServerChannel> socketChannelClass;
    if (Epoll.isAvailable()) {
        logger.fine("Using native socket transport for port " + listeningPort);
        parentGroup = new EpollEventLoopGroup(1);
        childGroup = new EpollEventLoopGroup();
        socketChannelClass = EpollServerSocketChannel.class;
    } else {
        logger.fine("Using NIO socket transport for port " + listeningPort);
        parentGroup = new NioEventLoopGroup(1);
        childGroup = new NioEventLoopGroup();
        socketChannelClass = NioServerSocketChannel.class;
    }
    try {
        b.group(parentGroup, childGroup).channel(socketChannelClass).option(ChannelOption.SO_BACKLOG, 1024).localAddress(listeningPort).childHandler(initializer);
        if (parentChannelOptions != null) {
            for (Map.Entry<ChannelOption<?>, ?> entry : parentChannelOptions.entrySet()) {
                b.option((ChannelOption<Object>) entry.getKey(), entry.getValue());
            }
        }
        if (childChannelOptions != null) {
            for (Map.Entry<ChannelOption<?>, ?> entry : childChannelOptions.entrySet()) {
                b.childOption((ChannelOption<Object>) entry.getKey(), entry.getValue());
            }
        }
        // Start the server.
        ChannelFuture f = b.bind().sync();
        // Wait until the server socket is closed.
        f.channel().closeFuture().sync();
    } catch (final InterruptedException e) {
        logger.log(Level.WARNING, "Interrupted");
        parentGroup.shutdownGracefully();
        childGroup.shutdownGracefully();
        logger.info("Listener on port " + String.valueOf(listeningPort) + " shut down");
    } catch (Exception e) {
        // ChannelFuture throws undeclared checked exceptions, so we need to handle it
        if (e instanceof BindException) {
            logger.severe("Unable to start listener - port " + String.valueOf(listeningPort) + " is already in use!");
        } else {
            logger.log(Level.SEVERE, "TcpIngester exception: ", e);
        }
    } finally {
        activeListeners.dec();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelOption(io.netty.channel.ChannelOption) BindException(java.net.BindException) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) BindException(java.net.BindException) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) Map(java.util.Map) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 10 with ChannelOption

use of org.apache.flink.shaded.netty4.io.netty.channel.ChannelOption in project grpc-java by grpc.

the class NettyClientTransportTest method newTransport.

private NettyClientTransport newTransport(ProtocolNegotiator negotiator, int maxMsgSize, int maxHeaderListSize, String userAgent, boolean enableKeepAlive, long keepAliveTimeNano, long keepAliveTimeoutNano, ChannelFactory<? extends Channel> channelFactory, EventLoopGroup group) {
    if (!enableKeepAlive) {
        keepAliveTimeNano = KEEPALIVE_TIME_NANOS_DISABLED;
    }
    NettyClientTransport transport = new NettyClientTransport(address, channelFactory, new HashMap<ChannelOption<?>, Object>(), group, negotiator, false, DEFAULT_WINDOW_SIZE, maxMsgSize, maxHeaderListSize, keepAliveTimeNano, keepAliveTimeoutNano, false, authority, userAgent, tooManyPingsRunnable, new TransportTracer(), eagAttributes, new SocketPicker(), new FakeChannelLogger(), false);
    transports.add(transport);
    return transport;
}
Also used : ChannelOption(io.netty.channel.ChannelOption) LocalSocketPicker(io.grpc.netty.NettyChannelBuilder.LocalSocketPicker) TransportTracer(io.grpc.internal.TransportTracer)

Aggregations

ChannelOption (io.netty.channel.ChannelOption)19 Test (org.junit.Test)8 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)7 InetSocketAddress (java.net.InetSocketAddress)7 ServerStreamTracer (io.grpc.ServerStreamTracer)6 ServerTransportListener (io.grpc.internal.ServerTransportListener)6 Map (java.util.Map)6 ServerListener (io.grpc.internal.ServerListener)5 ServerTransport (io.grpc.internal.ServerTransport)5 ChannelFuture (io.netty.channel.ChannelFuture)5 SocketAddress (java.net.SocketAddress)5 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)4 EventLoopGroup (io.netty.channel.EventLoopGroup)4 HashMap (java.util.HashMap)4 SocketStats (io.grpc.InternalChannelz.SocketStats)3 TransportTracer (io.grpc.internal.TransportTracer)3 LocalSocketPicker (io.grpc.netty.NettyChannelBuilder.LocalSocketPicker)3 Channel (io.netty.channel.Channel)3 ChannelConfig (io.netty.channel.ChannelConfig)3 ChannelHandler (io.netty.channel.ChannelHandler)3