Skip to content

Commit

Permalink
Refactor retry strategies (#4039)
Browse files Browse the repository at this point in the history
* Refactor the retry strategies

This change uses a single class to implement the core logic of all the
retries strategies and adds extension points to tailor the behavior
when needed.

* Rename to BaseRetryStrategy and make it abstract

* Remove previous implementations and rename the new ones
  • Loading branch information
sugmanue committed May 27, 2023
1 parent 46e5194 commit 4fbcdb6
Show file tree
Hide file tree
Showing 7 changed files with 500 additions and 832 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,15 @@ public void visit(Method method) {
}
} else if (isBuildable && method.getName().equals("toBuilder") && method.getSignature().startsWith("()")) {
// This is a buildable toBuilder
constructorsInvokedFromToBuilder.computeIfAbsent(getDottedClassName(), n -> new HashMap<>());
toBuilderModifiedFields.computeIfAbsent(getDottedClassName(), n -> new HashMap<>());
String dottedClassName = getDottedClassName();
constructorsInvokedFromToBuilder.computeIfAbsent(dottedClassName, n -> new HashMap<>());
toBuilderModifiedFields.computeIfAbsent(dottedClassName, n -> new HashMap<>());
inBuildableToBuilder = true;
inBuilderConstructor = false;

if (method.isAbstract()) {
// Ignore abstract toBuilder methods, we will still validate the actual implementations.
ignoredBuildables.add(dottedClassName);
}
registerIgnoredFields();
} else {
inBuildableToBuilder = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
* perceived availability for the clients to be close to 1.0.
*/
class AdaptiveRetryStrategyResourceConstrainedTest {
static final int DEFAULT_EXCEPTION_TOKEN_COST = 5;

@Test
void seemsToBeCorrectAndThreadSafe() {
Expand All @@ -61,12 +62,12 @@ void seemsToBeCorrectAndThreadSafe() {
int parallelism = serverWorkers + clientWorkers;
ExecutorService executor = Executors.newFixedThreadPool(parallelism);
Server server = new Server(serverWorkers, executor);
RateLimiterTokenBucketStore store = RateLimiterTokenBucketStore.builder().build();
AdaptiveRetryStrategy strategy = DefaultAdaptiveRetryStrategy
.builder()
// We don't care about how many attempts we allow to, that logic is tested somewhere else.
// so we give the strategy plenty of room for retries.
.maxAttempts(20)
.tokenBucketExceptionCost(DEFAULT_EXCEPTION_TOKEN_COST)
.tokenBucketStore(TokenBucketStore.builder().tokenBucketMaxCapacity(10_000).build())
// Just wait for the rate limiter delays.
.backoffStrategy(BackoffStrategy.retryImmediately())
Expand Down
Loading

0 comments on commit 4fbcdb6

Please sign in to comment.