Skip to content

Commit

Permalink
Review retry
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed May 7, 2024
1 parent 8e7e27d commit ba09f9e
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public interface Retrier {
* @param response Response
* @return true: retry, false: no need to retry
*/
boolean isRetryable(Response response);
default boolean isRetryable(Response response) {
RetryPolicy policy = getPolicy();
return policy != null && (policy.isRetry(response.getCode()) || policy.isRetry(response.getThrowable()));
}

/**
* Execute retry logic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ default Retrier get(RetryPolicy policy) {
}
AtomicReference<Retrier> reference = RETRIERS.computeIfAbsent(policy.getId(), n -> new AtomicReference<>());
Retrier retrier = reference.get();
if (retrier != null && retrier.getPolicy().getVersion() >= policy.getVersion()) {
long version = retrier == null ? Long.MIN_VALUE : retrier.getPolicy().getVersion();
if (version >= policy.getVersion()) {
return retrier;
}
Retrier newLimiter = create(policy);
Retrier newRetrier = create(policy);
while (true) {
retrier = reference.get();
if (retrier == null || retrier.getPolicy().getVersion() < policy.getVersion()) {
if (reference.compareAndSet(retrier, newLimiter)) {
retrier = newLimiter;
break;
}
version = retrier == null ? Long.MIN_VALUE : retrier.getPolicy().getVersion();
if (version >= policy.getVersion()) {
return retrier;
} else if (reference.compareAndSet(retrier, newRetrier)) {
return newRetrier;
}
}
return retrier;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,6 @@ public Resilience4jRetrier(RetryPolicy policy) {
retry = registry.retry(policy.getId().toString());
}

/**
* {@inheritDoc}
*/
@Override
public boolean isRetryable(Response response) {
return policy.isRetry(response.getCode()) || policy.isRetry(response.getThrowable());
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,6 @@ public SpringRetrier(RetryPolicy policy) {
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
}

/**
* {@inheritDoc}
*/
@Override
public boolean isRetryable(Response response) {
return policy.isRetry(response.getCode()) || policy.isRetry(response.getThrowable());
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit ba09f9e

Please sign in to comment.