-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
314 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/config/GrpcConfigBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.quarkus.grpc.runtime.config; | ||
|
||
import static io.vertx.core.net.SocketAddress.inetSocketAddress; | ||
import static java.lang.Integer.MAX_VALUE; | ||
import static java.net.InetAddress.getByName; | ||
|
||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
|
||
import io.quarkus.runtime.LaunchMode; | ||
import io.quarkus.runtime.configuration.ConfigBuilder; | ||
import io.smallrye.config.ConfigSourceContext; | ||
import io.smallrye.config.ConfigSourceFactory.ConfigurableConfigSourceFactory; | ||
import io.smallrye.config.SmallRyeConfigBuilder; | ||
import io.smallrye.config.common.MapBackedConfigSource; | ||
import io.vertx.core.net.SocketAddress; | ||
|
||
public class GrpcConfigBuilder implements ConfigBuilder { | ||
@Override | ||
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) { | ||
return builder.withSources(new RandomPortConfigSourceFactory()); | ||
} | ||
|
||
private static class RandomPortConfigSourceFactory implements ConfigurableConfigSourceFactory<GrpcServerConfig> { | ||
@Override | ||
public Iterable<ConfigSource> getConfigSources(final ConfigSourceContext context, final GrpcServerConfig config) { | ||
Map<String, String> randomPorts = new HashMap<>(); | ||
|
||
String host = config.host(); | ||
if (LaunchMode.current().equals(LaunchMode.TEST)) { | ||
if (config.testPort() == 0) { | ||
String port = assignRandomPort(inetSocketAddress(0, host)); | ||
randomPorts.put("quarkus.grpc.server.test-port", port); | ||
randomPorts.put("quarkus.grpc.server.port", port); | ||
} | ||
} else { | ||
if (config.port() == 0) { | ||
String port = assignRandomPort(inetSocketAddress(0, host)); | ||
randomPorts.put("quarkus.grpc.server.port", port); | ||
} | ||
} | ||
return randomPorts.isEmpty() ? Collections.emptyList() | ||
: List.of(new MapBackedConfigSource("Quarkus GRPC Random Ports", randomPorts, MAX_VALUE - 1000) { | ||
}); | ||
} | ||
|
||
private static String assignRandomPort(SocketAddress socketAddress) { | ||
try (ServerSocket serverSocket = new ServerSocket(socketAddress.port(), 0, getByName(socketAddress.host()))) { | ||
return serverSocket.getLocalPort() + ""; | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
extensions/grpc/runtime/src/main/java/io/quarkus/grpc/runtime/config/GrpcServerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.quarkus.grpc.runtime.config; | ||
|
||
import static io.quarkus.runtime.annotations.ConfigPhase.RUN_TIME; | ||
|
||
import java.util.Map; | ||
|
||
import io.quarkus.runtime.annotations.ConfigDocIgnore; | ||
import io.quarkus.runtime.annotations.ConfigRoot; | ||
import io.smallrye.config.ConfigMapping; | ||
import io.smallrye.config.WithDefault; | ||
import io.smallrye.config.WithParentName; | ||
|
||
@ConfigMapping(prefix = "quarkus.grpc.server") | ||
@ConfigRoot(phase = RUN_TIME) | ||
public interface GrpcServerConfig { | ||
@ConfigDocIgnore | ||
@WithDefault("9000") | ||
int port(); | ||
|
||
@ConfigDocIgnore | ||
@WithDefault("9001") | ||
int testPort(); | ||
|
||
@ConfigDocIgnore | ||
@WithDefault("0.0.0.0") | ||
String host(); | ||
|
||
@ConfigDocIgnore | ||
@WithParentName | ||
Map<String, String> properties(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.