Simplify RateLimitExceeded with no retry-duration

- Avoid passing negative durations in error cases
- Drop unused message
- Return a duration for a bad forwarded-for
This commit is contained in:
Ravi Khadiwala
2022-02-22 11:09:39 -06:00
committed by ravi-signal
parent ae3a5c5f5e
commit f5a75c6319
7 changed files with 49 additions and 23 deletions

View File

@@ -635,9 +635,13 @@ public class AccountController {
}
final String mostRecentProxy = ForwardedIpUtil.getMostRecentProxy(forwardedFor)
// Missing/malformed Forwarded-For, cannot calculate a reasonable backoff
// duration
.orElseThrow(() -> new RateLimitExceededException(Duration.ofHours(-1)));
.orElseThrow(() -> {
// Missing/malformed Forwarded-For, so we cannot check for a rate-limit.
// This shouldn't happen, so conservatively assume we're over the rate-limit
// and indicate that the client should retry
logger.error("Missing/bad Forwarded-For, cannot check account {}", uuid.toString());
return new RateLimitExceededException(Duration.ofHours(1));
});
rateLimiters.getCheckAccountExistenceLimiter().validate(mostRecentProxy);

View File

@@ -4,22 +4,26 @@
*/
package org.whispersystems.textsecuregcm.controllers;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.Optional;
public class RateLimitExceededException extends Exception {
private final Optional<Duration> retryDuration;
private final @Nullable
Duration retryDuration;
public RateLimitExceededException(final Duration retryDuration) {
this(null, retryDuration);
/**
* Constructs a new exception indicating when it may become safe to retry
*
* @param retryDuration A duration to wait before retrying, null if no duration can be indicated
*/
public RateLimitExceededException(final @Nullable Duration retryDuration) {
super(null, null, true, false);
this.retryDuration = retryDuration;
}
public RateLimitExceededException(final String message, final Duration retryDuration) {
super(message, null, true, false);
// we won't provide a backoff in the case the duration is negative
this.retryDuration = retryDuration.isNegative() ? Optional.empty() : Optional.of(retryDuration);
public Optional<Duration> getRetryDuration() {
return Optional.ofNullable(retryDuration);
}
public Optional<Duration> getRetryDuration() { return retryDuration; }
}