use of com.b2international.commons.exceptions.TooManyRequestsException in project snow-owl by b2ihealthcare.
the class RateLimitingRequest method execute.
@Override
public R execute(ServiceProvider context) {
final User user = context.service(User.class);
if (user != User.SYSTEM) {
// rate limit only non-system user requests
final String username = user.getUsername();
final RateLimitConsumption consumption = context.service(RateLimiter.class).consume(username);
if (consumption.isConsumed()) {
context.service(ResponseHeaders.class).set("X-Rate-Limit-Remaining", Long.toString(consumption.getRemainingTokens()));
} else {
throw new TooManyRequestsException(consumption.getSecondsToWait());
}
}
return next(context);
}
Aggregations