Skip to content

Commit

Permalink
invoke rate limiter within try-catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtfulCoder committed Jul 7, 2021
1 parent b26809f commit c300a52
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ public <R> R processRequest(
tags.put("request", resourceRequestName);
tags.put("scheme", MetacatContextManager.getContext().getScheme());
registry.counter(requestCounterId.withTags(tags)).increment();
checkRequestRateLimit(name, resourceRequestName, tags);

try {
// check rate limit in try-catch block in case ratelimiter throws exception.
// those exceptions can be tracked correctly, by the existing finally block that logs metrics.
checkRequestRateLimit(name, resourceRequestName, tags);
log.info("### Calling method: {} for {}", resourceRequestName, name);
return supplier.get();
} catch (UnsupportedOperationException e) {
Expand Down Expand Up @@ -211,6 +213,9 @@ public <R> R processRequest(
e.getCause() == null ? "" : e.getCause().getMessage(), resourceRequestName, name);
log.error(message, e);
throw e;
} catch (MetacatTooManyRequestsException e) {
collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
throw e;
} catch (Exception e) {
collectRequestExceptionMetrics(tags, e.getClass().getSimpleName());
final String message = String.format("%s.%s -- %s failed for %s", e.getMessage(),
Expand Down Expand Up @@ -296,10 +301,7 @@ private void checkRequestRateLimit(@NonNull final QualifiedName name,
log.warn(errorMsg);
registry.counter(requestRateLimitExceededId.withTags(tags)).increment();
if (this.config.isRateLimiterEnforced()) {
final MetacatTooManyRequestsException ex =
new MetacatTooManyRequestsException(errorMsg);
this.collectRequestExceptionMetrics(tags, ex.getClass().getSimpleName());
throw ex;
throw new MetacatTooManyRequestsException(errorMsg);
}
}
}
Expand Down

0 comments on commit c300a52

Please sign in to comment.