This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @ResponseStatus(code = HttpStatus.TOO_MANY_REQUESTS, reason = "Too many requests") | |
| public class ThrottlingException extends RuntimeException {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //this is the defaul implementation | |
| @Throttling | |
| public void serviceMethod() {} | |
| //annotation above is equivalent to this snippet | |
| @Throttling(type = ThrottlingType.RemoteAddr, limit = 1, timeUnit = TimeUnit.SECONDS) | |
| public void serviceMethod() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <repositories> | |
| <repository> | |
| <id>spring-boot-throttling-repo</id> | |
| <url>https://raw.github.com/weddini/spring-boot-throttling/mvn-repo/</url> | |
| <snapshots> | |
| <enabled>true</enabled> | |
| <updatePolicy>always</updatePolicy> | |
| </snapshots> | |
| </repository> | |
| </repositories> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // if you are allowing 5 requests per second | |
| final RateLimiter rateLimiter = RateLimiter.create(5.0); | |
| void throttler() { | |
| rateLimiter.acquire(); // may wait | |
| doSomething(); | |
| } | |
| // if you are allowing 5000 bytes per second | |
| final RateLimiter rateLimiter = RateLimiter.create(5000.0); | |
| void submitPacket(byte[] packet) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| RateLimiter rateLimiter = RateLimiter.create(10.0); // rate is "10 permits per second" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!-- https://mvnrepository.com/artifact/com.google.guava/guava --> | |
| <dependency> | |
| <groupId>com.google.guava</groupId> | |
| <artifactId>guava</artifactId> | |
| <version>19.0</version> | |
| </dependency> |