Skip to content

Commit df0bf16

Browse files
Use h2c when configuring gRPC clients with http:// URLs
This allows service meshes to manage mTLS on backfila's behalf
1 parent 1d5e282 commit df0bf16

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

service/src/main/kotlin/app/cash/backfila/client/GrpcCallbackConnectorProvider.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import misk.client.HttpClientConfigUrlProvider
1212
import misk.client.HttpClientFactory
1313
import misk.client.HttpClientsConfig
1414
import misk.moshi.adapter
15+
import okhttp3.Protocol
1516

1617
@Singleton
1718
class GrpcCallbackConnectorProvider @Inject constructor(
@@ -49,16 +50,22 @@ class GrpcCallbackConnectorProvider @Inject constructor(
4950
val headers: List<HttpHeader>? = extraData!!.headers
5051

5152
val httpClientEndpointConfig = httpClientsConfig[url]
52-
var okHttpClient = httpClientFactory.create(httpClientEndpointConfig)
53+
val baseUrl = httpClientConfigUrlProvider.getUrl(httpClientEndpointConfig)
54+
55+
val okHttpClientBuilder = httpClientFactory.create(httpClientEndpointConfig).newBuilder()
5356
if (!headers.isNullOrEmpty()) {
54-
okHttpClient = okHttpClient.newBuilder()
55-
.addInterceptor(OkHttpClientSpecifiedHeadersInterceptor(headers))
56-
.build()
57+
okHttpClientBuilder.addInterceptor(OkHttpClientSpecifiedHeadersInterceptor(headers))
58+
}
59+
60+
// Since gRPC uses HTTP/2, force h2c when calling an unencrypted endpoint
61+
if (baseUrl.startsWith("http://")) {
62+
okHttpClientBuilder.protocols(listOf(Protocol.H2_PRIOR_KNOWLEDGE))
63+
} else {
64+
okHttpClientBuilder.protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1))
5765
}
5866

59-
val baseUrl = httpClientConfigUrlProvider.getUrl(httpClientEndpointConfig)
6067
val grpcClient = GrpcClient.Builder()
61-
.client(okHttpClient)
68+
.client(okHttpClientBuilder.build())
6269
.baseUrl(baseUrl)
6370
.build()
6471
val api = grpcClient.create(BackfilaClientServiceClient::class)

0 commit comments

Comments
 (0)