Commit b1840bef authored by Matija Obreza's avatar Matija Obreza

GLIS Rate Limiter aspect (fixes #29)

parent 556351b0
......@@ -16,6 +16,12 @@
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.2.RELEASE</version>
<scope>provided</scope>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
......@@ -35,7 +41,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
......@@ -49,6 +54,11 @@
<version>${org.slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.2-jre</version>
</dependency>
</dependencies>
<build>
......
package org.genesys.glis.v1;
import com.google.common.util.concurrent.RateLimiter;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Aspect to control the rate of calls to GLIS API
*/
@Aspect
public class GlisRateLimiter {
private static final Logger LOG = LoggerFactory.getLogger(GlisRateLimiter.class);
private RateLimiter limiter = RateLimiter.create(20);
public GlisRateLimiter(double permitsPerSecond) {
setPermitsPerSecond(permitsPerSecond);
}
public void setPermitsPerSecond(double permitsPerSecond) {
limiter = RateLimiter.create(permitsPerSecond);
}
@Before("execution(* org.genesys.glis.v1.api.*.*(..))")
public void rateLimit(JoinPoint jp) {
double delay = limiter.acquire();
LOG.debug("Acquired rate limit permission ({} qps) in {} seconds for {}", limiter.getRate(), delay, jp.toLongString());
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment