[Design] Limit the Request per Second - Shuatiblog.com
maintain a variable for the number of request processed/rejected.
have a method to process request
This is the most important: clear the count every 1 seconds!
public class SetRps {
AtomicInteger count = new AtomicInteger(0);
int limit = -1;
int printIndex = 1;
long startTimestamp = -1;
void setRPS(int num) {
limit = num;
}
boolean process(long timestamp) {
// suppose timestamp is ms
synchronized (this) {
if (timestamp - startTimestamp >= 1000) {
// every 1 seconds, reset
count.set(0);
startTimestamp = timestamp;
System.out.println("clear!");
return true;
}
if (count.get() < limit) {
// can process
count.incrementAndGet();
System.out.println(printIndex++ + ". processing request "
+ timestamp % 100000 / 1000 + "," + timestamp % 1000);
return true;
}
}
return false;
}
}
Google的Guava里提供了一个RateLimiter的class,可以做参考
Google Guava RateLimiter
Read full article from [Design] Limit the Request per Second - Shuatiblog.com
- This variable must be atomic, thus a AtomicInteger.
- the variable is called 'count'
- if count < limit, do it
- otherwise, reject
- eg. LIMIT = 5r/s, so:
- the first 5 number of requests in every second are getting fulfilled
- from 6th request onward, the request all rejected, until the next second.
public class SetRps {
AtomicInteger count = new AtomicInteger(0);
int limit = -1;
int printIndex = 1;
long startTimestamp = -1;
void setRPS(int num) {
limit = num;
}
boolean process(long timestamp) {
// suppose timestamp is ms
synchronized (this) {
if (timestamp - startTimestamp >= 1000) {
// every 1 seconds, reset
count.set(0);
startTimestamp = timestamp;
System.out.println("clear!");
return true;
}
if (count.get() < limit) {
// can process
count.incrementAndGet();
System.out.println(printIndex++ + ". processing request "
+ timestamp % 100000 / 1000 + "," + timestamp % 1000);
return true;
}
}
return false;
}
}
Google的Guava里提供了一个RateLimiter的class,可以做参考
Google Guava RateLimiter
Read full article from [Design] Limit the Request per Second - Shuatiblog.com