Time rate limiter validation calls.
This commit is contained in:
parent
9afc433db4
commit
11196436e9
|
@ -19,6 +19,7 @@ package org.whispersystems.textsecuregcm.limits;
|
||||||
import com.codahale.metrics.Meter;
|
import com.codahale.metrics.Meter;
|
||||||
import com.codahale.metrics.MetricRegistry;
|
import com.codahale.metrics.MetricRegistry;
|
||||||
import com.codahale.metrics.SharedMetricRegistries;
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
|
import com.codahale.metrics.Timer;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -41,6 +42,7 @@ public class RateLimiter {
|
||||||
private final ObjectMapper mapper = SystemMapper.getMapper();
|
private final ObjectMapper mapper = SystemMapper.getMapper();
|
||||||
|
|
||||||
private final Meter meter;
|
private final Meter meter;
|
||||||
|
private final Timer validateTimer;
|
||||||
protected final ReplicatedJedisPool cacheClient;
|
protected final ReplicatedJedisPool cacheClient;
|
||||||
protected final FaultTolerantRedisCluster cacheCluster;
|
protected final FaultTolerantRedisCluster cacheCluster;
|
||||||
protected final String name;
|
protected final String name;
|
||||||
|
@ -62,6 +64,7 @@ public class RateLimiter {
|
||||||
MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
|
|
||||||
this.meter = metricRegistry.meter(name(getClass(), name, "exceeded"));
|
this.meter = metricRegistry.meter(name(getClass(), name, "exceeded"));
|
||||||
|
this.validateTimer = metricRegistry.timer(name(getClass(), name, "validate"));
|
||||||
this.cacheClient = cacheClient;
|
this.cacheClient = cacheClient;
|
||||||
this.cacheCluster = cacheCluster;
|
this.cacheCluster = cacheCluster;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -72,13 +75,15 @@ public class RateLimiter {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void validate(String key, int amount) throws RateLimitExceededException {
|
public void validate(String key, int amount) throws RateLimitExceededException {
|
||||||
LeakyBucket bucket = getBucket(key);
|
try (final Timer.Context ignored = validateTimer.time()) {
|
||||||
|
LeakyBucket bucket = getBucket(key);
|
||||||
|
|
||||||
if (bucket.add(amount)) {
|
if (bucket.add(amount)) {
|
||||||
setBucket(key, bucket);
|
setBucket(key, bucket);
|
||||||
} else {
|
} else {
|
||||||
meter.mark();
|
meter.mark();
|
||||||
throw new RateLimitExceededException(key + " , " + amount);
|
throw new RateLimitExceededException(key + " , " + amount);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue