add tests
This commit is contained in:
+4
-2
@@ -5,6 +5,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
@@ -44,10 +45,11 @@ public class RateLimitInterceptor implements HandlerInterceptor {
|
||||
|
||||
} else {
|
||||
|
||||
long waitForRefill = probe.getNanosToWaitForRefill() % 1_000_000_000;
|
||||
long waitForRefill = probe.getNanosToWaitForRefill() / 1_000_000_000;
|
||||
|
||||
response.sendError(HttpStatus.TOO_MANY_REQUESTS.value(), "You have exhausted your API Request Quota"); // 429
|
||||
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
|
||||
response.addHeader(HEADER_RETRY_AFTER, String.valueOf(waitForRefill));
|
||||
response.sendError(HttpStatus.TOO_MANY_REQUESTS.value(), "You have exhausted your API Request Quota"); // 429
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
+18
-24
@@ -5,34 +5,28 @@ import java.time.Duration;
|
||||
import io.github.bucket4j.Bandwidth;
|
||||
import io.github.bucket4j.Refill;
|
||||
|
||||
enum PricingPlan {
|
||||
public enum PricingPlan {
|
||||
|
||||
FREE {
|
||||
FREE(20),
|
||||
|
||||
@Override
|
||||
Bandwidth getLimit() {
|
||||
return Bandwidth.classic(20, Refill.intervally(20, Duration.ofHours(1)));
|
||||
}
|
||||
},
|
||||
BASIC(40),
|
||||
|
||||
BASIC {
|
||||
|
||||
@Override
|
||||
Bandwidth getLimit() {
|
||||
return Bandwidth.classic(40, Refill.intervally(40, Duration.ofHours(1)));
|
||||
}
|
||||
},
|
||||
|
||||
PROFESSIONAL {
|
||||
|
||||
@Override
|
||||
Bandwidth getLimit() {
|
||||
return Bandwidth.classic(100, Refill.intervally(100, Duration.ofHours(1)));
|
||||
}
|
||||
};
|
||||
|
||||
abstract Bandwidth getLimit();
|
||||
PROFESSIONAL(100);;
|
||||
|
||||
private int bucketCapacity;
|
||||
|
||||
private PricingPlan(int bucketCapacity) {
|
||||
this.bucketCapacity = bucketCapacity;
|
||||
}
|
||||
|
||||
Bandwidth getLimit() {
|
||||
return Bandwidth.classic(bucketCapacity, Refill.intervally(bucketCapacity, Duration.ofHours(1)));
|
||||
}
|
||||
|
||||
public int bucketCapacity() {
|
||||
return bucketCapacity;
|
||||
}
|
||||
|
||||
static PricingPlan resolvePlanFromApiKey(String apiKey) {
|
||||
if (apiKey == null || apiKey.isEmpty()) {
|
||||
return FREE;
|
||||
|
||||
Reference in New Issue
Block a user