-
Notifications
You must be signed in to change notification settings - Fork 992
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
Merged
Merged
Introduce RetryLimiter
#6409
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8abd5ad
minimal impl
jrhee17 3f8fec8
Merge branch 'main' into feat/retry-limiter
jrhee17 0e7a66e
address comments by @ikhoon and @minwoox
jrhee17 74d8996
address comments by @ikhoon and @minwoox
jrhee17 35f7573
Merge branch 'main' into feat/retry-limiter
jrhee17 1df8269
Merge branch 'main' into feat/retry-limiter
jrhee17 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
core/src/main/java/com/linecorp/armeria/client/retry/ConcurrencyBasedRetryLimiter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /* | ||
| * 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, "threshold 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 void handleDecision(ClientRequestContext ctx, RetryDecision decision) { | ||
| } | ||
|
|
||
|
jrhee17 marked this conversation as resolved.
Outdated
|
||
| @Override | ||
| public String toString() { | ||
| return MoreObjects.toStringHelper(this) | ||
| .add("activeRequests", activeRequests) | ||
| .add("maxRequests", maxRequests) | ||
| .toString(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
core/src/main/java/com/linecorp/armeria/client/retry/RetryLimitedException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** | ||
| * An exception thrown when a retry is limited by a {@link RetryLimiter}. | ||
| */ | ||
| public final class RetryLimitedException extends RuntimeException { | ||
|
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. 🛠️ Refactor suggestion | 🟠 Major Missing Per coding guidelines, newly added public classes should have the +import com.linecorp.armeria.common.annotation.UnstableApi;
+
/**
* An exception thrown when a retry is limited by a {@link RetryLimiter}.
*/
+@UnstableApi
public final class RetryLimitedException extends RuntimeException {🤖 Prompt for AI Agents |
||
|
|
||
| private static final long serialVersionUID = 7203512016805562689L; | ||
|
|
||
| private static final RetryLimitedException INSTANCE = new RetryLimitedException(); | ||
|
|
||
| /** | ||
| * Returns an instance of {@link RetryLimitedException}. | ||
| */ | ||
| public static RetryLimitedException of() { | ||
|
jrhee17 marked this conversation as resolved.
|
||
| return INSTANCE; | ||
| } | ||
|
|
||
| private RetryLimitedException() {} | ||
|
|
||
| @Override | ||
| public Throwable fillInStackTrace() { | ||
| return this; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.