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

@@ -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)));
}

View File

@@ -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 {

View File

@@ -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));
}
}
}