Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the capability to specify the Throttle duration #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
int limit() default 1;



/**
* Returns ThrottlingType {@see ThrottlingType}
* Used to evaluate method execution context.
Expand All @@ -47,6 +49,15 @@
*/
TimeUnit timeUnit() default TimeUnit.SECONDS;


/**
* Returns throttling duration
* Default value is 1, it means 1 second considering the default TimeUnit
*
* @return The throttle duration to measure the number of calls
*/
long duration() default 1L;

/**
* Returns header name for type = {@code ThrottlingType.HeaderValue}
* Header value will be evaluated in the request-scoped bean {@link javax.servlet.http.HttpServletRequest}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public class ThrottlingGauge {
private final ArrayList<Long> callTimestamps;
private final ReadWriteLock lock;

public ThrottlingGauge(TimeUnit timeUnit, int throttleLimit) {
public ThrottlingGauge(TimeUnit timeUnit, long duration, int throttleLimit) {
this.throttleLimit = throttleLimit;
mills = timeUnit.toMillis(1);
mills = timeUnit.toMillis(duration);
callTimestamps = new ArrayList<>();
lock = new ReentrantReadWriteLock(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public class ThrottlingKey {
private final ThrottlingType type;
private final TimeUnit timeUnit;
private final String evaluatedValue;
private final long duration;

private ThrottlingKey(Method method, int limit, ThrottlingType type, TimeUnit timeUnit, String evaluatedValue) {
private ThrottlingKey(Method method, int limit, ThrottlingType type, TimeUnit timeUnit, String evaluatedValue, long duration) {
this.method = method;
this.limit = limit;
this.type = type;
this.timeUnit = timeUnit;
this.evaluatedValue = evaluatedValue;
this.duration = duration;
}

public static Builder builder() {
Expand All @@ -49,6 +51,8 @@ public String getEvaluatedValue() {
return evaluatedValue;
}

public long getDuration() {return duration; }

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand All @@ -60,6 +64,7 @@ public boolean equals(Object o) {
if (!method.equals(that.method)) return false;
if (type != that.type) return false;
if (timeUnit != that.timeUnit) return false;
if (duration != that.duration) return false;
return evaluatedValue != null ? evaluatedValue.equals(that.evaluatedValue) : that.evaluatedValue == null;
}

Expand All @@ -70,6 +75,7 @@ public int hashCode() {
result = 31 * result + type.hashCode();
result = 31 * result + timeUnit.hashCode();
result = 31 * result + (evaluatedValue != null ? evaluatedValue.hashCode() : 0);
result = (int) (31 * result + timeUnit.toMillis(duration));
return result;
}

Expand All @@ -79,6 +85,7 @@ public String toString() {
"method=" + method +
", limit=" + limit +
", type=" + type +
", duration = " + duration +
", timeUnit=" + timeUnit +
", evaluatedValue='" + evaluatedValue + '\'' +
'}';
Expand All @@ -90,6 +97,7 @@ public static class Builder {
private ThrottlingType type;
private TimeUnit timeUnit;
private String evaluatedValue;
private long duration;

public Builder method(Method method) {
this.method = method;
Expand All @@ -100,6 +108,7 @@ public Builder annotation(Throttling throttling) {
this.limit = throttling.limit();
this.type = throttling.type();
this.timeUnit = throttling.timeUnit();
this.duration = throttling.duration();
return this;
}

Expand All @@ -109,7 +118,7 @@ public Builder evaluatedValue(String evaluatedValue) {
}

public ThrottlingKey build() {
return new ThrottlingKey(method, limit, type, timeUnit, evaluatedValue);
return new ThrottlingKey(method, limit, type, timeUnit, evaluatedValue, duration);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ThrottlingServiceImpl implements ThrottlingService {
private final Log logger = LogFactory.getLog(getClass());

private final Cache<ThrottlingKey, ThrottlingGauge> cache;
private final CacheLoader<ThrottlingKey, ThrottlingGauge> gaugeLoader = key -> new ThrottlingGauge(key.getTimeUnit(), key.getLimit());
private final CacheLoader<ThrottlingKey, ThrottlingGauge> gaugeLoader = key -> new ThrottlingGauge(key.getTimeUnit(), key.getDuration(),key.getLimit());


public ThrottlingServiceImpl(int cacheSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ThrottlingGaugeTest {

@Test
public void testThrottlingGauge() throws InterruptedException {
ThrottlingGauge gauge = new ThrottlingGauge(TimeUnit.SECONDS, 1);
ThrottlingGauge gauge = new ThrottlingGauge(TimeUnit.SECONDS, 1L, 1);

gauge.removeEldest();
Assert.isTrue(gauge.throttle(), "Should be ok with the first call");
Expand Down