com.yandex.cloud.kms.providers.tink.util.RetryUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kms-provider-tink Show documentation
Show all versions of kms-provider-tink Show documentation
Provider that enables usage of Yandex Cloud KMS from Google Tink cryptographic library
package com.yandex.cloud.kms.providers.tink.util;
import com.yandex.cloud.kms.providers.tink.config.RetryConfig;
import io.grpc.Status;
import io.grpc.StatusRuntimeException;
import net.jodah.failsafe.RetryPolicy;
import java.time.temporal.ChronoUnit;
import java.util.HashSet;
import java.util.Set;
public class RetryUtils {
private final static Set retryExceptionCodes = new HashSet() {{
add(Status.UNAVAILABLE.getCode());
add(Status.DEADLINE_EXCEEDED.getCode());
add(Status.INTERNAL.getCode());
}};
public static RetryPolicy createRetryPolicy(RetryConfig retryConfig) {
if (retryConfig == null) {
retryConfig = new RetryConfig();
}
return new RetryPolicy()
.handleIf((res, ex) -> {
if (!(ex instanceof StatusRuntimeException)) {
return false;
}
StatusRuntimeException exception = (StatusRuntimeException) ex;
return retryExceptionCodes.contains(exception.getStatus().getCode());
})
.withBackoff(retryConfig.getDelay(), retryConfig.getMaxDelay(), ChronoUnit.MILLIS)
.withMaxRetries(retryConfig.getMaxRetry());
}
}