-
Notifications
You must be signed in to change notification settings - Fork 988
Introduce RetryLimiter
#6409
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce RetryLimiter
#6409
Changes from all commits
8abd5ad
3f8fec8
0e7a66e
74d8996
35f7573
1df8269
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * 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 static com.google.common.base.Preconditions.checkArgument; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| import com.google.common.base.MoreObjects; | ||
|
|
||
| import com.linecorp.armeria.client.ClientRequestContext; | ||
|
|
||
| final class ConcurrencyBasedRetryLimiter implements RetryLimiter { | ||
|
|
||
| private final long maxRequests; | ||
| private final AtomicLong activeRequests = new AtomicLong(); | ||
|
|
||
| ConcurrencyBasedRetryLimiter(long maxRequests) { | ||
| checkArgument(maxRequests > 0, "maxRequests must be positive: %s.", maxRequests); | ||
| this.maxRequests = maxRequests; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean shouldRetry(ClientRequestContext ctx) { | ||
| final long cnt = activeRequests.incrementAndGet(); | ||
| if (cnt > maxRequests) { | ||
| activeRequests.decrementAndGet(); | ||
| return false; | ||
| } | ||
| ctx.log().whenComplete().thenRun(activeRequests::decrementAndGet); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return MoreObjects.toStringHelper(this) | ||
| .add("activeRequests", activeRequests) | ||
| .add("maxRequests", maxRequests) | ||
| .toString(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,44 @@ | ||||||||||||||
| /* | ||||||||||||||
| * 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 com.linecorp.armeria.common.Flags; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * An exception thrown when a retry is limited by a {@link RetryLimiter}. | ||||||||||||||
| */ | ||||||||||||||
| public final class RetryLimitedException extends RuntimeException { | ||||||||||||||
|
|
||||||||||||||
| private static final long serialVersionUID = 7203512016805562689L; | ||||||||||||||
|
|
||||||||||||||
| private static final RetryLimitedException INSTANCE = new RetryLimitedException(false); | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Returns an instance of {@link RetryLimitedException} sampled by {@link Flags#verboseExceptionSampler()}. | ||||||||||||||
| */ | ||||||||||||||
| public static RetryLimitedException of() { | ||||||||||||||
jrhee17 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
| return isSampled() ? new RetryLimitedException(true) : INSTANCE; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| private RetryLimitedException(boolean enableSuppression) { | ||||||||||||||
| super(null, null, enableSuppression, isSampled()); | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+37
to
+39
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential bug: INSTANCE may retain stack trace unintentionally. When The private RetryLimitedException(boolean enableSuppression) {
- super(null, null, enableSuppression, isSampled());
+ super(null, null, enableSuppression, enableSuppression);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||
|
|
||||||||||||||
| private static boolean isSampled() { | ||||||||||||||
| return Flags.verboseExceptionSampler().isSampled(RetryLimitedException.class); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Missing
@UnstableApiannotation on new public class.Per coding guidelines, newly added public classes should have the
@UnstableApiannotation.🤖 Prompt for AI Agents