From 5a4008595caa3f79071d97aab7f8c799dc7366bc Mon Sep 17 00:00:00 2001 From: asdf2014 Date: Mon, 20 Apr 2020 16:23:03 +0800 Subject: [PATCH 1/3] Support option and childOption for NettyServer --- .../src/main/java/io/grpc/netty/NettyServer.java | 12 ++++++++++++ .../java/io/grpc/netty/NettyServerBuilder.java | 16 ++++++++++++++-- .../io/grpc/netty/NettyClientTransportTest.java | 1 + .../test/java/io/grpc/netty/NettyServerTest.java | 4 ++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/netty/src/main/java/io/grpc/netty/NettyServer.java b/netty/src/main/java/io/grpc/netty/NettyServer.java index e3e4ccd77a0..036534b10b4 100644 --- a/netty/src/main/java/io/grpc/netty/NettyServer.java +++ b/netty/src/main/java/io/grpc/netty/NettyServer.java @@ -69,6 +69,7 @@ class NettyServer implements InternalServer, InternalWithLogId { private final SocketAddress address; private final ChannelFactory channelFactory; private final Map, ?> channelOptions; + private final Map, ?> childChannelOptions; private final ProtocolNegotiator protocolNegotiator; private final int maxStreamsPerConnection; private final ObjectPool bossGroupPool; @@ -99,6 +100,7 @@ class NettyServer implements InternalServer, InternalWithLogId { NettyServer( SocketAddress address, ChannelFactory channelFactory, Map, ?> channelOptions, + Map, ?> childChannelOptions, ObjectPool bossGroupPool, ObjectPool workerGroupPool, boolean forceHeapBuffer, @@ -115,6 +117,8 @@ class NettyServer implements InternalServer, InternalWithLogId { this.channelFactory = checkNotNull(channelFactory, "channelFactory"); checkNotNull(channelOptions, "channelOptions"); this.channelOptions = new HashMap, Object>(channelOptions); + checkNotNull(childChannelOptions, "childChannelOptions"); + this.childChannelOptions = new HashMap, Object>(childChannelOptions); this.bossGroupPool = checkNotNull(bossGroupPool, "bossGroupPool"); this.workerGroupPool = checkNotNull(workerGroupPool, "workerGroupPool"); this.forceHeapBuffer = forceHeapBuffer; @@ -168,6 +172,14 @@ public void start(ServerListener serverListener) throws IOException { if (channelOptions != null) { for (Map.Entry, ?> entry : channelOptions.entrySet()) { + @SuppressWarnings("unchecked") + ChannelOption key = (ChannelOption) entry.getKey(); + b.option(key, entry.getValue()); + } + } + + if (childChannelOptions != null) { + for (Map.Entry, ?> entry : childChannelOptions.entrySet()) { @SuppressWarnings("unchecked") ChannelOption key = (ChannelOption) entry.getKey(); b.childOption(key, entry.getValue()); diff --git a/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java b/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java index 1f48a331a3f..d8a8e67efc2 100644 --- a/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java +++ b/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java @@ -85,6 +85,7 @@ public final class NettyServerBuilder extends AbstractServerImplBuilder channelFactory = Utils.DEFAULT_SERVER_CHANNEL_FACTORY; private final Map, Object> channelOptions = new HashMap<>(); + private final Map, Object> childChannelOptions = new HashMap<>(); private ObjectPool bossEventLoopGroupPool = DEFAULT_BOSS_EVENT_LOOP_GROUP_POOL; private ObjectPool workerEventLoopGroupPool = @@ -189,10 +190,21 @@ public NettyServerBuilder channelFactory(ChannelFactory * Specifies a channel option. As the underlying channel as well as network implementation may * ignore this value applications should consider it a hint. * + * @since 1.28.1 + */ + public NettyServerBuilder withOption(ChannelOption option, T value) { + this.channelOptions.put(option, value); + return this; + } + + /** + * Specifies a child channel option. As the underlying channel as well as network implementation may + * ignore this value applications should consider it a hint. + * * @since 1.9.0 */ public NettyServerBuilder withChildOption(ChannelOption option, T value) { - this.channelOptions.put(option, value); + this.childChannelOptions.put(option, value); return this; } @@ -549,7 +561,7 @@ protected List buildTransportServers( List transportServers = new ArrayList<>(listenAddresses.size()); for (SocketAddress listenAddress : listenAddresses) { NettyServer transportServer = new NettyServer( - listenAddress, channelFactory, channelOptions, bossEventLoopGroupPool, + listenAddress, channelFactory, channelOptions, childChannelOptions, bossEventLoopGroupPool, workerEventLoopGroupPool, forceHeapBuffer, negotiator, streamTracerFactories, getTransportTracerFactory(), maxConcurrentCallsPerConnection, flowControlWindow, maxMessageSize, maxHeaderListSize, keepAliveTimeInNanos, keepAliveTimeoutInNanos, diff --git a/netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java b/netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java index 07979ab11ad..48cd0603672 100644 --- a/netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java +++ b/netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java @@ -769,6 +769,7 @@ private void startServer(int maxStreamsPerConnection, int maxHeaderListSize) thr TestUtils.testServerAddress(new InetSocketAddress(0)), new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap, Object>(), + new HashMap, Object>(), new FixedObjectPool<>(group), new FixedObjectPool<>(group), false, negotiator, Collections.emptyList(), TransportTracer.getDefaultFactory(), diff --git a/netty/src/test/java/io/grpc/netty/NettyServerTest.java b/netty/src/test/java/io/grpc/netty/NettyServerTest.java index a5b8e8ecf1a..9905e74308d 100644 --- a/netty/src/test/java/io/grpc/netty/NettyServerTest.java +++ b/netty/src/test/java/io/grpc/netty/NettyServerTest.java @@ -92,6 +92,7 @@ class TestProtocolNegotiator implements ProtocolNegotiator { addr, new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap, Object>(), + new HashMap, Object>(), new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, @@ -137,6 +138,7 @@ public void getPort_notStarted() { addr, new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap, Object>(), + new HashMap, Object>(), new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, @@ -175,6 +177,7 @@ public void childChannelOptions() throws Exception { NettyServer ns = new NettyServer( addr, new ReflectiveChannelFactory<>(NioServerSocketChannel.class), + new HashMap, Object>(), channelOptions, new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), @@ -227,6 +230,7 @@ public void channelzListenSocket() throws Exception { addr, new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap, Object>(), + new HashMap, Object>(), new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, From 942551e875b6a003de7f0771a662c2abfeaefa44 Mon Sep 17 00:00:00 2001 From: asdf2014 Date: Mon, 20 Apr 2020 18:08:22 +0800 Subject: [PATCH 2/3] Fix line is longer than 100 characters --- .../java/io/grpc/netty/NettyServerBuilder.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java b/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java index d8a8e67efc2..6e93d20dd7d 100644 --- a/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java +++ b/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java @@ -198,8 +198,8 @@ public NettyServerBuilder withOption(ChannelOption option, T value) { } /** - * Specifies a child channel option. As the underlying channel as well as network implementation may - * ignore this value applications should consider it a hint. + * Specifies a child channel option. As the underlying channel as well as network implementation + * may ignore this value applications should consider it a hint. * * @since 1.9.0 */ @@ -561,12 +561,13 @@ protected List buildTransportServers( List transportServers = new ArrayList<>(listenAddresses.size()); for (SocketAddress listenAddress : listenAddresses) { NettyServer transportServer = new NettyServer( - listenAddress, channelFactory, channelOptions, childChannelOptions, bossEventLoopGroupPool, - workerEventLoopGroupPool, forceHeapBuffer, negotiator, streamTracerFactories, - getTransportTracerFactory(), maxConcurrentCallsPerConnection, flowControlWindow, - maxMessageSize, maxHeaderListSize, keepAliveTimeInNanos, keepAliveTimeoutInNanos, - maxConnectionIdleInNanos, maxConnectionAgeInNanos, maxConnectionAgeGraceInNanos, - permitKeepAliveWithoutCalls, permitKeepAliveTimeInNanos, getChannelz()); + listenAddress, channelFactory, channelOptions, childChannelOptions, + bossEventLoopGroupPool, workerEventLoopGroupPool, forceHeapBuffer, negotiator, + streamTracerFactories, getTransportTracerFactory(), maxConcurrentCallsPerConnection, + flowControlWindow, maxMessageSize, maxHeaderListSize, keepAliveTimeInNanos, + keepAliveTimeoutInNanos, maxConnectionIdleInNanos, maxConnectionAgeInNanos, + maxConnectionAgeGraceInNanos, permitKeepAliveWithoutCalls, permitKeepAliveTimeInNanos, + getChannelz()); transportServers.add(transportServer); } return Collections.unmodifiableList(transportServers); From bdc8468ac6aaf7503d9abf22eb9659edb6a2a6e3 Mon Sep 17 00:00:00 2001 From: asdf2014 Date: Tue, 21 Apr 2020 10:40:28 +0800 Subject: [PATCH 3/3] Patch comments --- netty/src/main/java/io/grpc/netty/NettyServerBuilder.java | 2 +- netty/src/test/java/io/grpc/netty/NettyServerTest.java | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java b/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java index 6e93d20dd7d..bb10b2189de 100644 --- a/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java +++ b/netty/src/main/java/io/grpc/netty/NettyServerBuilder.java @@ -190,7 +190,7 @@ public NettyServerBuilder channelFactory(ChannelFactory * Specifies a channel option. As the underlying channel as well as network implementation may * ignore this value applications should consider it a hint. * - * @since 1.28.1 + * @since 1.30.0 */ public NettyServerBuilder withOption(ChannelOption option, T value) { this.channelOptions.put(option, value); diff --git a/netty/src/test/java/io/grpc/netty/NettyServerTest.java b/netty/src/test/java/io/grpc/netty/NettyServerTest.java index 9905e74308d..0e19b0875e4 100644 --- a/netty/src/test/java/io/grpc/netty/NettyServerTest.java +++ b/netty/src/test/java/io/grpc/netty/NettyServerTest.java @@ -163,9 +163,9 @@ public void childChannelOptions() throws Exception { final int originalLowWaterMark = 2097169; final int originalHighWaterMark = 2097211; - Map, Object> channelOptions = new HashMap<>(); + Map, Object> childChannelOptions = new HashMap<>(); - channelOptions.put(ChannelOption.WRITE_BUFFER_WATER_MARK, + childChannelOptions.put(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(originalLowWaterMark, originalHighWaterMark)); final AtomicInteger lowWaterMark = new AtomicInteger(0); @@ -178,7 +178,7 @@ public void childChannelOptions() throws Exception { addr, new ReflectiveChannelFactory<>(NioServerSocketChannel.class), new HashMap, Object>(), - channelOptions, + childChannelOptions, new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false,