Skip to content

Commit

Permalink
Migrate gRPC extension config to @ConfigMapping
Browse files Browse the repository at this point in the history
- Consolidate gRPC configuration into nested interfaces
  • Loading branch information
gastaldi committed Jan 28, 2025
1 parent 02dda9d commit 5c2cb4c
Show file tree
Hide file tree
Showing 34 changed files with 966 additions and 836 deletions.
2 changes: 1 addition & 1 deletion docs/src/main/asciidoc/grpc-service-consumption.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public class StreamingEndpoint {

For each gRPC service you inject in your application, you can configure the following attributes:

include::{generated-dir}/config/quarkus-grpc_quarkus.grpc-client.adoc[opts=optional, leveloffset=+1]
include::{generated-dir}/config/quarkus-grpc_quarkus.grpc.clients.adoc[opts=optional, leveloffset=+1]

The `client-name` is the name set in the `@GrpcClient` or derived from the injection point if not explicitly defined.

Expand Down
3 changes: 0 additions & 3 deletions extensions/grpc/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package io.quarkus.grpc.deployment;

import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigMapping(prefix = "quarkus.grpc")
@ConfigRoot(phase = ConfigPhase.BUILD_TIME)
public class GrpcBuildTimeConfig {
public interface GrpcBuildTimeConfig {

/**
* Configuration gRPC dev mode.
*/
@ConfigItem
@ConfigDocSection(generated = true)
public GrpcDevModeConfig devMode;
GrpcDevModeConfig devMode();

@ConfigGroup
interface GrpcDevModeConfig {

/**
* Start gRPC server in dev mode even if no gRPC services are implemented.
* By default set to `true` to ease incremental development of new services using dev mode.
*/
@WithDefault("true")
boolean forceServerStart();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void registerStorkInterceptor(BuildProducer<AdditionalBeanBuildItem> beans) {
@BuildStep
@Record(ExecutionTime.STATIC_INIT)
void setUpStork(GrpcStorkRecorder storkRecorder, GrpcClientBuildTimeConfig config) {
storkRecorder.init(config.storkProactiveConnections);
storkRecorder.init(config.storkProactiveConnections());
}

@BuildStep
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ ServiceStartBuildItem initializeServer(GrpcServerRecorder recorder,
}

if (!bindables.isEmpty()
|| (LaunchMode.current() == LaunchMode.DEVELOPMENT && buildTimeConfig.devMode.forceServerStart)) {
|| (LaunchMode.current() == LaunchMode.DEVELOPMENT && buildTimeConfig.devMode().forceServerStart())) {
//Uses mainrouter when the 'quarkus.http.root-path' is not '/'
Map<Integer, Handler<RoutingContext>> securityHandlers = null;
final RuntimeValue<Router> routerRuntimeValue;
Expand Down Expand Up @@ -746,14 +746,14 @@ void addHealthChecks(GrpcServerBuildTimeConfig config,
BuildProducer<AdditionalBeanBuildItem> beans) {
boolean healthEnabled = false;
if (!bindables.isEmpty()) {
healthEnabled = config.mpHealthEnabled;
healthEnabled = config.mpHealthEnabled();

if (config.grpcHealthEnabled) {
if (config.grpcHealthEnabled()) {
beans.produce(AdditionalBeanBuildItem.unremovableOf(GrpcHealthEndpoint.class));
healthEnabled = true;
}
healthBuildItems.produce(new HealthBuildItem("io.quarkus.grpc.runtime.health.GrpcHealthCheck",
config.mpHealthEnabled));
config.mpHealthEnabled()));
}
if (healthEnabled || LaunchMode.current() == LaunchMode.DEVELOPMENT) {
beans.produce(AdditionalBeanBuildItem.unremovableOf(GrpcHealthStorage.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
public class InProcessGrpcServerBuilderProvider implements GrpcBuilderProvider<InProcessServerBuilder> {
@Override
public boolean providesServer(GrpcServerConfiguration configuration) {
return Enabled.isEnabled(configuration.inProcess);
return Enabled.isEnabled(configuration.inProcess());
}

@Override
public ServerBuilder<InProcessServerBuilder> createServerBuilder(Vertx vertx, GrpcServerConfiguration configuration,
LaunchMode launchMode) {
ServerBuilder<InProcessServerBuilder> builder = InProcessServerBuilder.forName(configuration.inProcess.name);
ServerBuilder<InProcessServerBuilder> builder = InProcessServerBuilder.forName(configuration.inProcess().name());
// wrap with Vert.x context, so that the context interceptors work
VertxInternal vxi = (VertxInternal) vertx;
Executor delegate = vertx.nettyEventLoopGroup();
Expand Down Expand Up @@ -65,12 +65,12 @@ public boolean serverAlreadyExists() {

@Override
public String serverInfo(String host, int port, GrpcServerConfiguration configuration) {
return "InProcess gRPC server [" + configuration.inProcess.name + "]";
return "InProcess gRPC server [" + configuration.inProcess().name() + "]";
}

@Override
public boolean providesChannel(GrpcClientConfiguration configuration) {
return Enabled.isEnabled(configuration.inProcess);
return Enabled.isEnabled(configuration.inProcess());
}

@Override
Expand All @@ -85,11 +85,11 @@ public String adjustHost(String host) {

@Override
public ManagedChannelBuilder<?> createChannelBuilder(GrpcClientConfiguration configuration, String target) {
return InProcessChannelBuilder.forName(configuration.inProcess.name).directExecutor();
return InProcessChannelBuilder.forName(configuration.inProcess().name()).directExecutor();
}

@Override
public String channelInfo(GrpcClientConfiguration configuration) {
return "InProcess [" + configuration.inProcess.name + "]";
return "InProcess [" + configuration.inProcess().name() + "]";
}
}
5 changes: 1 addition & 4 deletions extensions/grpc/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
<groupId>io.smallrye.common</groupId>
<artifactId>smallrye-common-vertx-context</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
Expand Down Expand Up @@ -170,9 +170,6 @@
<version>${project.version}</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AlegacyConfigRoot=true</arg>
</compilerArgs>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import io.quarkus.grpc.reflection.service.ReflectionServiceV1alpha;
import io.quarkus.grpc.runtime.config.GrpcConfiguration;
import io.quarkus.grpc.runtime.config.GrpcServerConfiguration;
import io.quarkus.grpc.runtime.config.GrpcServerNettyConfig;
import io.quarkus.grpc.runtime.devmode.DevModeInterceptor;
import io.quarkus.grpc.runtime.devmode.GrpcHotReplacementInterceptor;
import io.quarkus.grpc.runtime.devmode.GrpcServerReloader;
Expand Down Expand Up @@ -111,10 +110,10 @@ public void initializeGrpcServer(RuntimeValue<Vertx> vertxSupplier,
}

Vertx vertx = vertxSupplier.getValue();
GrpcServerConfiguration configuration = cfg.server;
GrpcServerConfiguration configuration = cfg.server();
GrpcBuilderProvider<?> provider = GrpcBuilderProvider.findServerBuilderProvider(configuration);

if (configuration.useSeparateServer) {
if (configuration.useSeparateServer()) {
if (provider == null) {
LOGGER.warn(
"Using legacy gRPC support, with separate new HTTP server instance. " +
Expand Down Expand Up @@ -150,8 +149,8 @@ private void buildGrpcServer(Vertx vertx, GrpcServerConfiguration configuration,
Map<Integer, Handler<RoutingContext>> securityHandlers) {

GrpcServerOptions options = new GrpcServerOptions();
if (!configuration.maxInboundMessageSize.isEmpty()) {
options.setMaxMessageSize(configuration.maxInboundMessageSize.getAsInt());
if (!configuration.maxInboundMessageSize().isEmpty()) {
options.setMaxMessageSize(configuration.maxInboundMessageSize().getAsInt());
}
GrpcServer server = GrpcServer.server(vertx, options);
List<ServerInterceptor> globalInterceptors = grpcContainer.getSortedGlobalInterceptors();
Expand All @@ -178,7 +177,7 @@ private void buildGrpcServer(Vertx vertx, GrpcServerConfiguration configuration,
definitions.add(service.definition);
}

boolean reflectionServiceEnabled = configuration.enableReflectionService || launchMode == LaunchMode.DEVELOPMENT;
boolean reflectionServiceEnabled = configuration.enableReflectionService() || launchMode == LaunchMode.DEVELOPMENT;

if (reflectionServiceEnabled) {
LOGGER.info("Registering gRPC reflection service");
Expand Down Expand Up @@ -281,7 +280,7 @@ private void prodStart(GrpcContainer grpcContainer, Vertx vertx, GrpcServerConfi
vertx.deployVerticle(
() -> new GrpcServerVerticle(configuration, grpcContainer, provider, launchMode, blockingMethodsPerService,
virtualMethodsPerService),
new DeploymentOptions().setInstances(configuration.instances),
new DeploymentOptions().setInstances(configuration.instances()),
result -> {
if (result.failed()) {
startResult.completeExceptionally(result.cause());
Expand All @@ -306,13 +305,13 @@ private void prodStart(GrpcContainer grpcContainer, Vertx vertx, GrpcServerConfi

private void postStartup(GrpcServerConfiguration configuration, GrpcBuilderProvider<?> provider, boolean test) {
initHealthStorage();
int port = test ? testPort(configuration) : configuration.port;
int port = test ? testPort(configuration) : configuration.port();
String msg = "Started ";
if (provider != null)
msg += provider.serverInfo(configuration.host, port, configuration);
msg += provider.serverInfo(configuration.host(), port, configuration);
else
msg += String.format("gRPC server on %s:%d [%s]",
configuration.host, port, "TLS enabled: " + !configuration.plainText);
configuration.host(), port, "TLS enabled: " + !configuration.plainText());
LOGGER.info(msg);
}

Expand Down Expand Up @@ -387,26 +386,26 @@ private void devModeStart(GrpcContainer grpcContainer, Vertx vertx, GrpcServerCo
}

private void applyNettySettings(GrpcServerConfiguration configuration, VertxServerBuilder builder) {
if (configuration.netty != null) {
GrpcServerNettyConfig config = configuration.netty;
if (configuration.netty() != null) {
GrpcServerConfiguration.GrpcServerNettyConfig config = configuration.netty();
NettyServerBuilder nettyServerBuilder = builder.nettyBuilder();

config.keepAliveTime.ifPresent(
config.keepAliveTime().ifPresent(
duration -> nettyServerBuilder.keepAliveTime(duration.toNanos(), TimeUnit.NANOSECONDS));

config.permitKeepAliveTime.ifPresent(
config.permitKeepAliveTime().ifPresent(
duration -> nettyServerBuilder.permitKeepAliveTime(duration.toNanos(), TimeUnit.NANOSECONDS));
config.permitKeepAliveWithoutCalls.ifPresent(nettyServerBuilder::permitKeepAliveWithoutCalls);
config.permitKeepAliveWithoutCalls().ifPresent(nettyServerBuilder::permitKeepAliveWithoutCalls);
}
}

@SuppressWarnings("rawtypes")
private void applyTransportSecurityConfig(GrpcServerConfiguration configuration, ServerBuilder builder) {
if (configuration.transportSecurity != null) {
File cert = configuration.transportSecurity.certificate
if (configuration.transportSecurity() != null) {
File cert = configuration.transportSecurity().certificate()
.map(File::new)
.orElse(null);
File key = configuration.transportSecurity.key
File key = configuration.transportSecurity().key()
.map(File::new)
.orElse(null);
if (cert != null || key != null) {
Expand Down Expand Up @@ -529,15 +528,15 @@ private Map.Entry<Integer, Server> buildServer(Vertx vertx, GrpcServerConfigurat
Map<String, List<String>> virtualMethodsPerService,
GrpcContainer grpcContainer, LaunchMode launchMode) {

int port = launchMode == LaunchMode.TEST ? configuration.testPort : configuration.port;
int port = launchMode == LaunchMode.TEST ? configuration.testPort() : configuration.port();

AtomicBoolean usePlainText = new AtomicBoolean();

ServerBuilder builder;
if (provider != null) {
builder = provider.createServerBuilder(vertx, configuration, launchMode);
} else {
VertxServerBuilder vsBuilder = VertxServerBuilder.forAddress(vertx, configuration.host, port);
VertxServerBuilder vsBuilder = VertxServerBuilder.forAddress(vertx, configuration.host(), port);
// add Vert.x specific stuff here
vsBuilder.useSsl(options -> {
try {
Expand All @@ -556,20 +555,20 @@ private Map.Entry<Integer, Server> buildServer(Vertx vertx, GrpcServerConfigurat
builder = vsBuilder;
}

if (configuration.maxInboundMessageSize.isPresent()) {
builder.maxInboundMessageSize(configuration.maxInboundMessageSize.getAsInt());
if (configuration.maxInboundMessageSize().isPresent()) {
builder.maxInboundMessageSize(configuration.maxInboundMessageSize().getAsInt());
}

if (configuration.maxInboundMetadataSize.isPresent()) {
builder.maxInboundMetadataSize(configuration.maxInboundMetadataSize.getAsInt());
if (configuration.maxInboundMetadataSize().isPresent()) {
builder.maxInboundMetadataSize(configuration.maxInboundMetadataSize().getAsInt());
}

Optional<Duration> handshakeTimeout = configuration.handshakeTimeout;
Optional<Duration> handshakeTimeout = configuration.handshakeTimeout();
handshakeTimeout.ifPresent(duration -> builder.handshakeTimeout(duration.toMillis(), TimeUnit.MILLISECONDS));

applyTransportSecurityConfig(configuration, builder);

boolean reflectionServiceEnabled = configuration.enableReflectionService || launchMode == LaunchMode.DEVELOPMENT;
boolean reflectionServiceEnabled = configuration.enableReflectionService() || launchMode == LaunchMode.DEVELOPMENT;
List<GrpcServiceDefinition> toBeRegistered = collectServiceDefinitions(grpcContainer.getServices());
List<ServerServiceDefinition> definitions = new ArrayList<>();

Expand All @@ -594,9 +593,9 @@ private Map.Entry<Integer, Server> buildServer(Vertx vertx, GrpcServerConfigurat

String msg = "Starting ";
if (provider != null)
msg += provider.serverInfo(configuration.host, port, configuration);
msg += provider.serverInfo(configuration.host(), port, configuration);
else
msg += String.format("gRPC server on %s:%d [TLS enabled: %s]", configuration.host, port, !usePlainText.get());
msg += String.format("gRPC server on %s:%d [TLS enabled: %s]", configuration.host(), port, !usePlainText.get());
LOGGER.debug(msg);

return new AbstractMap.SimpleEntry<>(port, builder.build());
Expand All @@ -610,8 +609,8 @@ private Map.Entry<Integer, Server> buildServer(Vertx vertx, GrpcServerConfigurat
*/
private CompressionInterceptor prepareCompressionInterceptor(GrpcServerConfiguration configuration) {
CompressionInterceptor compressionInterceptor = null;
if (configuration.compression.isPresent()) {
compressionInterceptor = new CompressionInterceptor(configuration.compression.get());
if (configuration.compression().isPresent()) {
compressionInterceptor = new CompressionInterceptor(configuration.compression().get());
}
return compressionInterceptor;
}
Expand Down
Loading

0 comments on commit 5c2cb4c

Please sign in to comment.