简体   繁体   中英

gRPC Retry using someApi.withOption

Is it possible to use.withOption to add retryPolicy configurations to a grpc client stub? I've tried something like below but it seems it fails to equals on the key. Is there a better way of doing this please?

val someApi: OfferTokenAPIGrpcKt.OfferTokenAPICoroutineStub

someApi.withOption(CallOptions.Key.create("io.grpc.internal.ManagedChannelServiceConfig.MethodInfo"), createMethodInfo())
.randomGrpcEndpoint()

    fun createMethodInfo(): Map<String, Map<String, Any>> {
        val statusCodes = listOf("UNAVAILABLE", "UNKNOWN")
        val retryPolicyMap = mapOf(
            "maxAttempts" to "5",
            "initialBackoff" to "2s",
            "maxBackoff" to "30s",
            "backoffMultiplier" to "2",
            "retryableStatusCodes" to statusCodes,
        )

        val methodConfig = mapOf("retryPolicy" to retryPolicyMap)

        return methodConfig
    

No, you cannot specify the retry policy per-RPC. CallOptions.Key use reference equality, so simply creating a key with the same name does nothing other than reuse the debug string.

You can specify a service config via ManagedChannelbuilder.defaultServiceConfig() . That allows you to configure a method's retry policy, but that does not allow you to change the settings per-RPC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM