-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement retry and timeout policy for gRPC client.
Signed-off-by: Artur Souza <asouza.pro@gmail.com>
- Loading branch information
1 parent
76aec01
commit 75bbd77
Showing
8 changed files
with
248 additions
and
63 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
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
109 changes: 109 additions & 0 deletions
109
sdk/src/main/java/io/dapr/internal/resiliency/RetryPolicy.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,109 @@ | ||
/* | ||
* Copyright 2023 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.internal.resiliency; | ||
|
||
import io.dapr.config.Properties; | ||
import io.dapr.exceptions.DaprException; | ||
import io.grpc.Status; | ||
import io.grpc.StatusRuntimeException; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* Retriable policy for SDK communication to Dapr API. | ||
*/ | ||
public final class RetryPolicy { | ||
|
||
private static final int MIN_BACKOFF_MILLIS = 500; | ||
|
||
private static final int MAX_BACKOFF_SECONDS = 5; | ||
|
||
private final int maxRetries = Properties.MAX_RETRIES.get(); | ||
|
||
/** | ||
* Applies the retry policy to an expected Mono action. | ||
* @param response Response | ||
* @param <T> Type expected for the action's response | ||
* @return action with retry | ||
*/ | ||
public <T> Mono<T> apply(Mono<T> response) { | ||
if (this.maxRetries <= 0) { | ||
return response; | ||
} | ||
|
||
return response.retryWhen( | ||
reactor.util.retry.Retry.backoff(maxRetries, Duration.ofMillis(MIN_BACKOFF_MILLIS)) | ||
.maxBackoff(Duration.ofSeconds(MAX_BACKOFF_SECONDS)) | ||
.filter(throwable -> isRetriableGrpcError(throwable))) | ||
.onErrorMap(throwable -> findDaprException(throwable)); | ||
} | ||
|
||
/** | ||
* Applies the retry policy to an expected Flux action. | ||
* @param response Response | ||
* @param <T> Type expected for the action's response | ||
* @return action with retry | ||
*/ | ||
public <T> Flux<T> apply(Flux<T> response) { | ||
if (this.maxRetries <= 0) { | ||
return response; | ||
} | ||
|
||
return response.retryWhen( | ||
reactor.util.retry.Retry.backoff(maxRetries, Duration.ofMillis(500)) | ||
.maxBackoff(Duration.ofSeconds(5)) | ||
.filter(throwable -> isRetriableGrpcError(throwable))) | ||
.onErrorMap(throwable -> findDaprException(throwable)); | ||
} | ||
|
||
private static boolean isRetriableGrpcError(Throwable throwable) { | ||
Status grpcStatus = findGrpcStatusCode(throwable); | ||
if (grpcStatus == null) { | ||
return false; | ||
} | ||
|
||
switch (grpcStatus.getCode()) { | ||
case DEADLINE_EXCEEDED: | ||
case UNAVAILABLE: | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
private static Status findGrpcStatusCode(Throwable throwable) { | ||
while (throwable != null) { | ||
if (throwable instanceof StatusRuntimeException) { | ||
return ((StatusRuntimeException) throwable).getStatus(); | ||
} | ||
|
||
throwable = throwable.getCause(); | ||
} | ||
return null; | ||
} | ||
|
||
private static Throwable findDaprException(Throwable throwable) { | ||
Throwable original = throwable; | ||
while (throwable != null) { | ||
if (throwable instanceof DaprException) { | ||
return throwable; | ||
} | ||
|
||
throwable = throwable.getCause(); | ||
} | ||
return original; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
sdk/src/main/java/io/dapr/internal/resiliency/TimeoutPolicy.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,40 @@ | ||
/* | ||
* Copyright 2023 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.internal.resiliency; | ||
|
||
import io.dapr.config.Properties; | ||
import io.grpc.CallOptions; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* Retriable policy for SDK communication to Dapr API. | ||
*/ | ||
public final class TimeoutPolicy { | ||
|
||
private final int timeoutSeconds = Properties.TIMEOUT_SECONDS.get(); | ||
|
||
/** | ||
* Applies the timeout policy to a gRPC call options. | ||
* @param options Call options | ||
* @return Call options with retry policy applied | ||
*/ | ||
public CallOptions apply(CallOptions options) { | ||
if (this.timeoutSeconds <= 0) { | ||
return options; | ||
} | ||
|
||
return options.withDeadlineAfter(this.timeoutSeconds, TimeUnit.SECONDS); | ||
} | ||
} |
Oops, something went wrong.