Skip to content

Instantly share code, notes, and snippets.

View HeshanSudarshana's full-sized avatar

Heshan Sudarshana HeshanSudarshana

View GitHub Profile
@HeshanSudarshana
HeshanSudarshana / springBootThrottlingException.java
Created December 4, 2018 05:35
spring-boot-throttling Exception
@ResponseStatus(code = HttpStatus.TOO_MANY_REQUESTS, reason = "Too many requests")
public class ThrottlingException extends RuntimeException {}
@HeshanSudarshana
HeshanSudarshana / springBootThrottlingImplementation.java
Last active December 4, 2018 05:04
spring-boot-throttling implementation
//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() {}
@HeshanSudarshana
HeshanSudarshana / spring_boot_throttling.xml
Created December 4, 2018 04:22
spring-boot-throttling Java maven dependency
<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>
@HeshanSudarshana
HeshanSudarshana / rateLimitterImplementation.java
Last active October 9, 2019 07:24
RateLimitter Implementation
// 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) {
@HeshanSudarshana
HeshanSudarshana / rateLimitterConstructor.java
Created December 4, 2018 02:35
Constructor Java RateLimitter
RateLimiter rateLimiter = RateLimiter.create(10.0); // rate is "10 permits per second"
@HeshanSudarshana
HeshanSudarshana / pom.xml
Last active December 3, 2018 18:51
RateLimiter Java maven dependency
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>