mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-20 06:28:01 +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
@@ -54,7 +54,8 @@ public class CardinalityRateLimiter {
|
||||
});
|
||||
|
||||
if (rateLimitExceeded) {
|
||||
throw new RateLimitExceededException(Duration.ofSeconds(getRemainingTtl(key)));
|
||||
long remainingTtl = getRemainingTtl(key);
|
||||
throw new RateLimitExceededException(remainingTtl >= 0 ? Duration.ofSeconds(remainingTtl) : null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +67,14 @@ public class CardinalityRateLimiter {
|
||||
return ttl;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the remaining ttl for the specified key
|
||||
*
|
||||
* @param key with timeout to check
|
||||
* @return the ttl, or negative in the case of error
|
||||
*/
|
||||
public long getRemainingTtl(final String key) {
|
||||
// ttl() returns -2 if key does not exist, -1 if key has no expiration
|
||||
return cacheCluster.withCluster(connection -> connection.sync().ttl(getHllKey(key)));
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ public class LockingRateLimiter extends RateLimiter {
|
||||
public void validate(String key, int amount) throws RateLimitExceededException {
|
||||
if (!acquireLock(key)) {
|
||||
meter.mark();
|
||||
throw new RateLimitExceededException("Locked", Duration.ZERO);
|
||||
throw new RateLimitExceededException(Duration.ZERO);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
@@ -57,7 +57,7 @@ public class RateLimiter {
|
||||
setBucket(key, bucket);
|
||||
} else {
|
||||
meter.mark();
|
||||
throw new RateLimitExceededException(key + " , " + amount, bucket.getTimeUntilSpaceAvailable(amount));
|
||||
throw new RateLimitExceededException(bucket.getTimeUntilSpaceAvailable(amount));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user