mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-20 06:08:03 +01:00
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:
committed by
ravi-signal
parent
ae3a5c5f5e
commit
f5a75c6319
@@ -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);
|
||||
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user