use of io.envoyproxy.envoy.config.route.v3.RetryPolicy.RetryBackOff in project grpc-java by grpc.
the class ClientXdsClient method parseRetryPolicy.
// Return null if we ignore the given policy.
@Nullable
private static StructOrError<RetryPolicy> parseRetryPolicy(io.envoyproxy.envoy.config.route.v3.RetryPolicy retryPolicyProto) {
int maxAttempts = 2;
if (retryPolicyProto.hasNumRetries()) {
maxAttempts = retryPolicyProto.getNumRetries().getValue() + 1;
}
Duration initialBackoff = Durations.fromMillis(25);
Duration maxBackoff = Durations.fromMillis(250);
if (retryPolicyProto.hasRetryBackOff()) {
RetryBackOff retryBackOff = retryPolicyProto.getRetryBackOff();
if (!retryBackOff.hasBaseInterval()) {
return StructOrError.fromError("No base_interval specified in retry_backoff");
}
Duration originalInitialBackoff = initialBackoff = retryBackOff.getBaseInterval();
if (Durations.compare(initialBackoff, Durations.ZERO) <= 0) {
return StructOrError.fromError("base_interval in retry_backoff must be positive");
}
if (Durations.compare(initialBackoff, Durations.fromMillis(1)) < 0) {
initialBackoff = Durations.fromMillis(1);
}
if (retryBackOff.hasMaxInterval()) {
maxBackoff = retryPolicyProto.getRetryBackOff().getMaxInterval();
if (Durations.compare(maxBackoff, originalInitialBackoff) < 0) {
return StructOrError.fromError("max_interval in retry_backoff cannot be less than base_interval");
}
if (Durations.compare(maxBackoff, Durations.fromMillis(1)) < 0) {
maxBackoff = Durations.fromMillis(1);
}
} else {
maxBackoff = Durations.fromNanos(Durations.toNanos(initialBackoff) * 10);
}
}
Iterable<String> retryOns = Splitter.on(',').omitEmptyStrings().trimResults().split(retryPolicyProto.getRetryOn());
ImmutableList.Builder<Code> retryableStatusCodesBuilder = ImmutableList.builder();
for (String retryOn : retryOns) {
Code code;
try {
code = Code.valueOf(retryOn.toUpperCase(Locale.US).replace('-', '_'));
} catch (IllegalArgumentException e) {
// unsupported value, such as "5xx"
continue;
}
if (!SUPPORTED_RETRYABLE_CODES.contains(code)) {
// unsupported value
continue;
}
retryableStatusCodesBuilder.add(code);
}
List<Code> retryableStatusCodes = retryableStatusCodesBuilder.build();
return StructOrError.fromStruct(RetryPolicy.create(maxAttempts, retryableStatusCodes, initialBackoff, maxBackoff, /* perAttemptRecvTimeout= */
null));
}
Aggregations