|
45 | 45 | import io.grpc.netty.GrpcSslContexts; |
46 | 46 | import io.grpc.netty.NegotiationType; |
47 | 47 | import io.grpc.netty.NettyChannelBuilder; |
| 48 | +import io.netty.channel.socket.nio.NioSocketChannel; |
48 | 49 | import io.netty.handler.ssl.SslContext; |
49 | 50 | import io.netty.handler.ssl.SslContextBuilder; |
50 | 51 | import io.quarkus.arc.Arc; |
@@ -206,8 +207,21 @@ public static Channel createChannel(String name, Set<String> perClientIntercepto |
206 | 207 | builder.defaultServiceConfig(map); |
207 | 208 | } |
208 | 209 |
|
209 | | - if (builder instanceof NettyChannelBuilder) { |
| 210 | + if (config.useVertxEventLoop() && builder instanceof NettyChannelBuilder) { |
210 | 211 | NettyChannelBuilder ncBuilder = (NettyChannelBuilder) builder; |
| 212 | + // just use the existing Vertx event loop group, if possible |
| 213 | + Vertx vertx = container.instance(Vertx.class).get(); |
| 214 | + // only support NIO for now, since Vertx::transport is not exposed in the API |
| 215 | + if (vertx != null && vertx.isNativeTransportEnabled()) { |
| 216 | + // see https://github.com/eclipse-vertx/vert.x/pull/5292 |
| 217 | + boolean reuseNettyAllocators = Boolean.getBoolean("vertx.reuseNettyAllocators"); |
| 218 | + if (reuseNettyAllocators) { |
| 219 | + // let Netty Grpc to re-use the default Netty allocator as well |
| 220 | + System.setProperty("io.grpc.netty.useCustomAllocator", "false"); |
| 221 | + } |
| 222 | + ncBuilder = ncBuilder.eventLoopGroup(vertx.nettyEventLoopGroup()) |
| 223 | + .channelType(NioSocketChannel.class); |
| 224 | + } |
211 | 225 | builder = ncBuilder |
212 | 226 | // clients are intercepted using the IOThreadClientInterceptor interceptor which will decide on which |
213 | 227 | // thread the messages should be processed. |
@@ -395,6 +409,11 @@ public boolean useQuarkusGrpcClient() { |
395 | 409 | return false; |
396 | 410 | } |
397 | 411 |
|
| 412 | + @Override |
| 413 | + public boolean useVertxEventLoop() { |
| 414 | + return true; |
| 415 | + } |
| 416 | + |
398 | 417 | @Override |
399 | 418 | public ClientXds xds() { |
400 | 419 | return null; |
|
0 commit comments