Skip to content

Commit

Permalink
feat: Load client config from arbitrary config block (#1974)
Browse files Browse the repository at this point in the history
  • Loading branch information
johanandren authored Sep 9, 2024
1 parent ebf4411 commit ee25c69
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
25 changes: 15 additions & 10 deletions runtime/src/main/scala/akka/grpc/GrpcClientSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,22 @@ object GrpcClientSettings {
* @param clientName of the client configuration to lookup config from the ActorSystem's config
*/
def fromConfig(clientName: String)(implicit actorSystem: ClassicActorSystemProvider): GrpcClientSettings = {
val system = actorSystem.classicSystem
val akkaGrpcClientConfig = system.settings.config.getConfig("akka.grpc.client")
val clientConfig = {
// Use config named "*" by default
val defaultServiceConfig = akkaGrpcClientConfig.getConfig("\"*\"")
require(
akkaGrpcClientConfig.hasPath(s""""$clientName""""),
s"Config path `akka.grpc.client.$clientName` does not exist")
akkaGrpcClientConfig.getConfig(s""""$clientName"""").withFallback(defaultServiceConfig)
}
val akkaGrpcClientConfig = actorSystem.classicSystem.settings.config.getConfig("akka.grpc.client")
fromConfig(clientName, akkaGrpcClientConfig)
}

/**
* Look up client settings from the given configuration. Will look for an entry with the given name client name
* directly in the config block. Each client configuration falls back to the defaults defined in reference.conf
*
* @param clientName of the client name to lookup config from the given config
*/
def fromConfig(clientName: String, config: Config)(
implicit actorSystem: ClassicActorSystemProvider): GrpcClientSettings = {
// Use config named "*" by default
val defaultServiceConfig = actorSystem.classicSystem.settings.config.getConfig("akka.grpc.client.\"*\"")
require(config.hasPath(s""""$clientName""""), s"Config path `akka.grpc.client.$clientName` does not exist")
val clientConfig = config.getConfig(s""""$clientName"""").withFallback(defaultServiceConfig)
GrpcClientSettings.fromConfig(clientConfig)
}

Expand Down
16 changes: 16 additions & 0 deletions runtime/src/test/scala/akka/grpc/GrpcClientSettingsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ class GrpcClientSettingsSpec extends AnyWordSpec with Matchers with ScalaFutures
settings.serviceDiscovery should not be null
} finally actorSystem.terminate()
}

"allow a user provided config object" in {
val config = ConfigFactory.parseString("""
"service-with-user-config" {
host = "my-host"
port = 43
service-discovery {
mechanism = "static"
}
}
""")
val settings = GrpcClientSettings.fromConfig("service-with-user-config", config)
settings.defaultPort should ===(43)
settings.useTls should ===(true) // from system default config

}
}

override def afterAll(): Unit = {
Expand Down

0 comments on commit ee25c69

Please sign in to comment.