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..bb10b2189de 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.30.0 + */ + 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,12 +561,13 @@ protected List buildTransportServers( List transportServers = new ArrayList<>(listenAddresses.size()); for (SocketAddress listenAddress : listenAddresses) { NettyServer transportServer = new NettyServer( - listenAddress, channelFactory, channelOptions, 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); 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..0e19b0875e4 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, @@ -161,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); @@ -175,7 +177,8 @@ public void childChannelOptions() throws Exception { NettyServer ns = new NettyServer( addr, new ReflectiveChannelFactory<>(NioServerSocketChannel.class), - channelOptions, + new HashMap, Object>(), + childChannelOptions, new FixedObjectPool<>(eventLoop), new FixedObjectPool<>(eventLoop), false, @@ -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,