Skip to content

Commit d3a8d95

Browse files
authored
Merge pull request #46198 from franz1981/grpc_netty_client
Grpc netty client improvements
2 parents 58ff745 + bf12ff2 commit d3a8d95

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/config/GrpcClientConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public interface GrpcClientConfiguration {
2424
@WithDefault("false")
2525
boolean useQuarkusGrpcClient();
2626

27+
/**
28+
* Use Vert.x event loop(s) for gRPC client, if it's using the previous Java gRPC support.
29+
*/
30+
@WithDefault("true")
31+
boolean useVertxEventLoop();
32+
2733
/**
2834
* Configure XDS usage, if enabled.
2935
*/

extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/supports/Channels.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import io.grpc.netty.GrpcSslContexts;
4646
import io.grpc.netty.NegotiationType;
4747
import io.grpc.netty.NettyChannelBuilder;
48+
import io.netty.channel.socket.nio.NioSocketChannel;
4849
import io.netty.handler.ssl.SslContext;
4950
import io.netty.handler.ssl.SslContextBuilder;
5051
import io.quarkus.arc.Arc;
@@ -206,8 +207,21 @@ public static Channel createChannel(String name, Set<String> perClientIntercepto
206207
builder.defaultServiceConfig(map);
207208
}
208209

209-
if (builder instanceof NettyChannelBuilder) {
210+
if (config.useVertxEventLoop() && builder instanceof NettyChannelBuilder) {
210211
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+
}
211225
builder = ncBuilder
212226
// clients are intercepted using the IOThreadClientInterceptor interceptor which will decide on which
213227
// thread the messages should be processed.
@@ -395,6 +409,11 @@ public boolean useQuarkusGrpcClient() {
395409
return false;
396410
}
397411

412+
@Override
413+
public boolean useVertxEventLoop() {
414+
return true;
415+
}
416+
398417
@Override
399418
public ClientXds xds() {
400419
return null;

0 commit comments

Comments
 (0)