All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yandex.cloud.kms.providers.tink.util.RetryUtils Maven / Gradle / Ivy

Go to download

Provider that enables usage of Yandex Cloud KMS from Google Tink cryptographic library

There is a newer version: 2.7
Show newest version
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());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy