Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.Endpoint;
import com.linecorp.armeria.client.SimpleDecoratingClient;
import com.linecorp.armeria.client.retry.limiter.RetryLimiter;
import com.linecorp.armeria.common.HttpHeaderNames;
import com.linecorp.armeria.common.HttpRequest;
import com.linecorp.armeria.common.Request;
Expand Down Expand Up @@ -199,8 +200,9 @@ protected final boolean setResponseTimeout(ClientRequestContext ctx) {
* {@code currentAttemptNo} exceeds the {@code maxAttempts} or the {@code nextDelay} is after
* the moment which timeout happens.
*/
protected final long getNextDelay(ClientRequestContext ctx, Backoff backoff) {
return getNextDelay(ctx, backoff, -1);
protected final long getNextDelay(ClientRequestContext ctx, @Nullable RetryLimiter limiter,
Backoff backoff) {
return getNextDelay(ctx, limiter, backoff, -1);
}

/**
Expand All @@ -214,7 +216,8 @@ protected final long getNextDelay(ClientRequestContext ctx, Backoff backoff) {
* the moment which timeout happens.
*/
@SuppressWarnings("MethodMayBeStatic") // Intentionally left non-static for better user experience.
protected final long getNextDelay(ClientRequestContext ctx, Backoff backoff, long millisAfterFromServer) {
protected final long getNextDelay(ClientRequestContext ctx, @Nullable RetryLimiter limiter,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: AbstractRetryingClient is public so this and the change above are breaking changes. Let us note it in the PR description.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But these are protected methods, do they also count as public api?

Copy link
Contributor

@schiemon schiemon Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this class is also not final people could inherit from it and use those methods. I cannot think of a use-case for it but theoretically it is possible. In the doc comments we are also not advising against it 🤷

Backoff backoff, long millisAfterFromServer) {
requireNonNull(ctx, "ctx");
requireNonNull(backoff, "backoff");
final State state = state(ctx);
Expand All @@ -237,6 +240,9 @@ protected final long getNextDelay(ClientRequestContext ctx, Backoff backoff, lon
return -1;
}

if (!RetryLimiterExecutor.shouldRetry(limiter, ctx, currentAttemptNo)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in Armeria it is a convention to put the ctx as the first argument

return -1;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have metrics about this but afaik it does not seem easy to add metrics here, maybe we could add some debug logging?

}
return nextDelay;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.linecorp.armeria.client.retry.limiter.RetryLimiter;
import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.Response;
import com.linecorp.armeria.common.RpcResponse;
Expand Down Expand Up @@ -87,25 +88,30 @@ static <T extends Response> RetryConfigBuilder<T> builder0(
private final RetryRule fromRetryRuleWithContent;
@Nullable
private RetryRuleWithContent<T> fromRetryRule;
@Nullable
private RetryLimiter retryLimiter;

RetryConfig(RetryRule retryRule, int maxTotalAttempts, long responseTimeoutMillisForEachAttempt) {
this(requireNonNull(retryRule, "retryRule"), null,
maxTotalAttempts, responseTimeoutMillisForEachAttempt, 0);
RetryConfig(RetryRule retryRule, @Nullable RetryLimiter retryLimiter, int maxTotalAttempts,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the change below are also breaking

long responseTimeoutMillisForEachAttempt) {
this(requireNonNull(retryRule, "retryRule"), null, retryLimiter,
maxTotalAttempts, responseTimeoutMillisForEachAttempt, 0);
checkArguments(maxTotalAttempts, responseTimeoutMillisForEachAttempt);
}

RetryConfig(
RetryRuleWithContent<T> retryRuleWithContent,
@Nullable RetryLimiter retryLimiter,
int maxContentLength,
int maxTotalAttempts,
long responseTimeoutMillisForEachAttempt) {
this(null, requireNonNull(retryRuleWithContent, "retryRuleWithContent"),
maxTotalAttempts, responseTimeoutMillisForEachAttempt, maxContentLength);
this(null, requireNonNull(retryRuleWithContent, "retryRuleWithContent"), retryLimiter,
maxTotalAttempts, responseTimeoutMillisForEachAttempt, maxContentLength);
}

private RetryConfig(
@Nullable RetryRule retryRule,
@Nullable RetryRuleWithContent<T> retryRuleWithContent,
@Nullable RetryLimiter retryLimiter,
int maxTotalAttempts,
long responseTimeoutMillisForEachAttempt,
int maxContentLength) {
Expand All @@ -120,6 +126,7 @@ private RetryConfig(
} else {
fromRetryRuleWithContent = RetryRuleUtil.fromRetryRuleWithContent(retryRuleWithContent);
}
this.retryLimiter = retryLimiter;
}

private static void checkArguments(int maxTotalAttempts, long responseTimeoutMillisForEachAttempt) {
Expand All @@ -145,9 +152,12 @@ public RetryConfigBuilder<T> toBuilder() {
assert retryRule != null;
builder = builder0(retryRule);
}
return builder
.maxTotalAttempts(maxTotalAttempts)
.responseTimeoutMillisForEachAttempt(responseTimeoutMillisForEachAttempt);
builder.maxTotalAttempts(maxTotalAttempts)
.responseTimeoutMillisForEachAttempt(responseTimeoutMillisForEachAttempt);
if (retryLimiter != null) {
builder.limiter(retryLimiter);
}
return builder;
}

/**
Expand Down Expand Up @@ -183,6 +193,15 @@ public RetryRuleWithContent<T> retryRuleWithContent() {
return retryRuleWithContent;
}

/**
* Returns the {@link RetryLimiter} which was specified with
* {@link RetryConfigBuilder#limiter(RetryLimiter)}.
*/
@Nullable
public RetryLimiter retryLimiter() {
return retryLimiter;
}

/**
* Returns the {@code maxContentLength}, which is non-zero only if a {@link RetryRuleWithContent} is used.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;

import com.linecorp.armeria.client.retry.limiter.RetryLimiter;
import com.linecorp.armeria.common.Flags;
import com.linecorp.armeria.common.Response;
import com.linecorp.armeria.common.annotation.Nullable;
Expand All @@ -37,6 +38,8 @@ public final class RetryConfigBuilder<T extends Response> {
private int maxTotalAttempts = Flags.defaultMaxTotalAttempts();
private long responseTimeoutMillisForEachAttempt = Flags.defaultResponseTimeoutMillis();
private int maxContentLength;
@Nullable
private RetryLimiter retryLimiter;

@Nullable
private final RetryRule retryRule;
Expand All @@ -50,6 +53,7 @@ public final class RetryConfigBuilder<T extends Response> {
this.retryRule = requireNonNull(retryRule, "retryRule");
retryRuleWithContent = null;
maxContentLength = 0;
retryLimiter = null;
}

/**
Expand All @@ -59,6 +63,7 @@ public final class RetryConfigBuilder<T extends Response> {
retryRule = null;
this.retryRuleWithContent = requireNonNull(retryRuleWithContent, "retryRuleWithContent");
maxContentLength = Integer.MAX_VALUE;
retryLimiter = null;
}

/**
Expand Down Expand Up @@ -111,16 +116,27 @@ public RetryConfigBuilder<T> responseTimeoutForEachAttempt(Duration responseTime
return this;
}

/**
* Sets the specified {@link RetryLimiter} to be used for retry budgeting.
*/
public RetryConfigBuilder<T> limiter(RetryLimiter retryLimiter) {
requireNonNull(retryLimiter, "retryLimiter");
this.retryLimiter = retryLimiter;
return this;
}

/**
* Returns a newly-created {@link RetryConfig} from this {@link RetryConfigBuilder}'s values.
*/
public RetryConfig<T> build() {
if (retryRule != null) {
return new RetryConfig<>(retryRule, maxTotalAttempts, responseTimeoutMillisForEachAttempt);
return new RetryConfig<>(retryRule, retryLimiter, maxTotalAttempts,
responseTimeoutMillisForEachAttempt);
}
assert retryRuleWithContent != null;
return new RetryConfig<>(
retryRuleWithContent,
retryLimiter,
maxContentLength,
maxTotalAttempts,
responseTimeoutMillisForEachAttempt);
Expand All @@ -139,6 +155,7 @@ ToStringHelper toStringHelper() {
.add("retryRuleWithContent", retryRuleWithContent)
.add("maxTotalAttempts", maxTotalAttempts)
.add("responseTimeoutMillisForEachAttempt", responseTimeoutMillisForEachAttempt)
.add("maxContentLength", maxContentLength);
.add("maxContentLength", maxContentLength)
.add("retryLimiter", retryLimiter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Copyright 2025 LY Corporation
*
* LY Corporation licenses this file to you 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:
*
* https://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 com.linecorp.armeria.client.retry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.retry.limiter.RetryLimiter;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.logging.RequestLog;

/**
* Executes {@link RetryLimiter} operations with proper exception handling.
* <p>
* This class provides static utility methods to safely execute retry limiter operations.
* It handles null limiters gracefully and catches any exceptions thrown by the limiter
* to prevent them from affecting the retry logic.
* </p>
* <p>
* When a limiter throws an exception, it is logged as an error and the operation
* defaults to allowing the retry to proceed (returning {@code true} for {@link #shouldRetry}
* and doing nothing for {@link #onCompletedAttempt}).
* </p>
*/
final class RetryLimiterExecutor {

private RetryLimiterExecutor() {}

private static final Logger logger = LoggerFactory.getLogger(RetryLimiterExecutor.class);

/**
* Determines whether a retry should be attempted based on the provided limiter.
* <p>
* This method safely executes the limiter's {@link RetryLimiter#shouldRetry} method.
* If the limiter is null, this method returns {@code true} to allow retries.
* If the limiter throws an exception, it is logged and {@code true} is returned
* to ensure retries can still proceed.
* </p>
*
* @param limiter the retry limiter to consult, or {@code null} if no limiter is configured
* @param ctx the client request context
* @param numAttemptsSoFar the number of attempts made so far (0-based)
* @return {@code true} if a retry should be attempted, {@code false} otherwise.
* Returns {@code true} if the limiter is null or throws an exception.
*/
public static boolean shouldRetry(@Nullable RetryLimiter limiter, ClientRequestContext ctx,
int numAttemptsSoFar) {
try {
if (limiter != null) {
return limiter.shouldRetry(ctx, numAttemptsSoFar);
} else {
return true;
}
} catch (Throwable t) {
logger.error("Failed to execute RetryLimiter.shouldRetry: limiter={}, attempts={}", limiter,
numAttemptsSoFar, t);
return true;
}
}

/**
* Notifies the limiter that an attempt has been completed.
* <p>
* This method safely executes the limiter's {@link RetryLimiter#onCompletedAttempt} method.
* If the limiter is null, this method does nothing.
* If the limiter throws an exception, it is logged but does not affect the retry flow.
* </p>
*
* @param limiter the retry limiter to notify, or {@code null} if no limiter is configured
* @param ctx the client request context
* @param requestLog the request log containing details about the completed attempt
* @param numAttemptsSoFar the number of attempts made so far (0-based)
*/
public static void onCompletedAttempt(@Nullable RetryLimiter limiter, ClientRequestContext ctx,
RequestLog requestLog, int numAttemptsSoFar) {
try {
if (limiter != null) {
limiter.onCompletedAttempt(ctx, requestLog, numAttemptsSoFar);
}
} catch (Throwable t) {
logger.error("Failed to execute RetryLimiter.onCompletedAttempt: limiter={}, attempts={}", limiter,
numAttemptsSoFar, t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.linecorp.armeria.client.ClientRequestContext;
import com.linecorp.armeria.client.HttpClient;
import com.linecorp.armeria.client.ResponseTimeoutException;
import com.linecorp.armeria.client.retry.limiter.RetryLimiter;
import com.linecorp.armeria.common.AggregatedHttpResponse;
import com.linecorp.armeria.common.AggregationOptions;
import com.linecorp.armeria.common.HttpHeaderNames;
Expand Down Expand Up @@ -362,10 +363,11 @@ private void handleResponseWithoutContent(RetryConfig<HttpResponse> config, Clie
}
try {
final RetryRule retryRule = retryRule(config);
final RetryLimiter limiter = config.retryLimiter();
final CompletionStage<RetryDecision> f = retryRule.shouldRetry(derivedCtx, responseCause);
f.handle((decision, shouldRetryCause) -> {
warnIfExceptionIsRaised(retryRule, shouldRetryCause);
handleRetryDecision(decision, ctx, derivedCtx, rootReqDuplicator,
handleRetryDecision(decision, limiter, ctx, derivedCtx, rootReqDuplicator,
originalReq, returnedRes, future, response);
return null;
});
Expand Down Expand Up @@ -412,8 +414,9 @@ private void handleStreamingResponse(RetryConfig<HttpResponse> retryConfig, Clie
.handle((decision, cause) -> {
warnIfExceptionIsRaised(ruleWithContent, cause);
truncatingHttpResponse.abort();
handleRetryDecision(decision, ctx, derivedCtx, rootReqDuplicator,
originalReq, returnedRes, future, duplicated);
handleRetryDecision(decision, retryConfig.retryLimiter(), ctx,
derivedCtx, rootReqDuplicator, originalReq,
returnedRes, future, duplicated);
return null;
});
} catch (Throwable cause) {
Expand Down Expand Up @@ -449,9 +452,9 @@ private void handleAggregatedResponse(RetryConfig<HttpResponse> retryConfig, Cli
ruleWithContent.shouldRetry(derivedCtx, aggregatedRes.toHttpResponse(), null)
.handle((decision, cause) -> {
warnIfExceptionIsRaised(ruleWithContent, cause);
handleRetryDecision(
decision, ctx, derivedCtx, rootReqDuplicator, originalReq,
returnedRes, future, aggregatedRes.toHttpResponse());
handleRetryDecision(decision, retryConfig.retryLimiter(), ctx, derivedCtx,
rootReqDuplicator, originalReq, returnedRes, future,
aggregatedRes.toHttpResponse());
return null;
});
} catch (Throwable cause) {
Expand Down Expand Up @@ -521,14 +524,20 @@ private static void handleException(ClientRequestContext ctx,
ctx.logBuilder().endResponse(cause);
}

private void handleRetryDecision(@Nullable RetryDecision decision, ClientRequestContext ctx,
ClientRequestContext derivedCtx, HttpRequestDuplicator rootReqDuplicator,
HttpRequest originalReq, HttpResponse returnedRes,
CompletableFuture<HttpResponse> future, HttpResponse originalRes) {
private void handleRetryDecision(@Nullable RetryDecision decision, @Nullable RetryLimiter limiter,
ClientRequestContext ctx, ClientRequestContext derivedCtx,
HttpRequestDuplicator rootReqDuplicator, HttpRequest originalReq,
HttpResponse returnedRes, CompletableFuture<HttpResponse> future,
HttpResponse originalRes) {
// Notify the retry limiter that this attempt has completed
final RetryConfig<HttpResponse> config = mappedRetryConfig(ctx);
RetryLimiterExecutor.onCompletedAttempt(config.retryLimiter(), ctx, derivedCtx.log().partial(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do this but then you also have to cover the exceptional case with handleException

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test case for this would be great

getTotalAttempts(ctx));

final Backoff backoff = decision != null ? decision.backoff() : null;
if (backoff != null) {
final long millisAfter = useRetryAfter ? getRetryAfterMillis(derivedCtx) : -1;
final long nextDelay = getNextDelay(ctx, backoff, millisAfter);
final long nextDelay = getNextDelay(ctx, limiter, backoff, millisAfter);
if (nextDelay >= 0) {
abortResponse(originalRes, derivedCtx);
scheduleNextRetry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,12 @@ private void doExecute0(ClientRequestContext ctx, RpcRequest req,
res.handle((unused1, cause) -> {
try {
assert retryRule != null;
RetryLimiterExecutor.onCompletedAttempt(retryConfig.retryLimiter(), ctx, ctx.log().partial(),
totalAttempts);
retryRule.shouldRetry(derivedCtx, res, cause).handle((decision, unused3) -> {
final Backoff backoff = decision != null ? decision.backoff() : null;
if (backoff != null) {
final long nextDelay = getNextDelay(derivedCtx, backoff);
final long nextDelay = getNextDelay(derivedCtx, retryConfig.retryLimiter(), backoff);
if (nextDelay < 0) {
onRetryComplete(ctx, derivedCtx, res, future);
return null;
Expand Down
Loading
Loading