APILimiter.java Main Class
package com.ngdeveloper.api;
import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class APILimiter {
private final int defaultRequestPerMinute;
private final Map<String, TokenBucket> buckets;
public APILimiter(int defaultRequestPerMinute) {
this.defaultRequestPerMinute = defaultRequestPerMinute;
this.buckets = new ConcurrentHashMap<>();
}
public void configureRequestsPerMinute(String apiKey, int requestsPerMinute) {
TokenBucket bucket = buckets.computeIfAbsent(apiKey, key -> new TokenBucket(defaultRequestPerMinute));
bucket.setTokensPerMinute(requestsPerMinute);
}
public boolean allowRequest(String apiKey) {
TokenBucket bucket = buckets.computeIfAbsent(apiKey, key -> new TokenBucket(defaultRequestPerMinute));
return bucket.tryConsume();
}
private static class TokenBucket {
private int tokens;
private Instant lastRefillTime;
private int tokensPerMinute;
public TokenBucket(int tokensPerMinute) {
this.tokens = tokensPerMinute;
this.lastRefillTime = Instant.now();
this.tokensPerMinute = tokensPerMinute;
}
public synchronized void setTokensPerMinute(int tokensPerMinute) {
this.tokensPerMinute = tokensPerMinute;
}
public synchronized boolean tryConsume() {
refill();
if (tokens > 0) {
tokens--;
return true;
} else {
return false;
}
}
private void refill() {
Instant now = Instant.now();
long timeElapsed = now.toEpochMilli() - lastRefillTime.toEpochMilli();
int tokensToAdd = (int) ((timeElapsed / 1000) * tokensPerMinute / 60);
tokens = Math.min(tokens + tokensToAdd, tokensPerMinute);
lastRefillTime = now;
}
}
public static void main(String[] args) {
// Example usage and testing
// Create an API limiter with capacity of 60 requests per minute per API key
APILimiter limiter = new APILimiter(60);
// Allow some requests for an API key with a limit of 30 requests per minute
String apiKey1 = "API_KEY_1";
limiter.configureRequestsPerMinute(apiKey1, 30);
for (int i = 0; i < 40; i++) {
boolean allowed = limiter.allowRequest(apiKey1);
System.out.println("API key 1 request " + (i + 1) + " allowed: " + allowed);
}
// Allow some requests for an API key with the default limit of 60 requests per minute
String apiKey2 = "API_KEY_2";
for (int i = 0; i < 70; i++) {
boolean allowed = limiter.allowRequest(apiKey2);
System.out.println("API key 2 request " + (i + 1) + " allowed: " + allowed);
}
}
}
Output:
API key 1 request 1 allowed: true
API key 1 request 2 allowed: true
API key 1 request 3 allowed: true
API key 1 request 4 allowed: true
API key 1 request 5 allowed: true
API key 1 request 6 allowed: true
API key 1 request 7 allowed: true
API key 1 request 8 allowed: true
API key 1 request 9 allowed: true
API key 1 request 10 allowed: true
API key 1 request 11 allowed: true
API key 1 request 12 allowed: true
API key 1 request 13 allowed: true
API key 1 request 14 allowed: true
API key 1 request 15 allowed: true
API key 1 request 16 allowed: true
API key 1 request 17 allowed: true
API key 1 request 18 allowed: true
API key 1 request 19 allowed: true
API key 1 request 20 allowed: true
API key 1 request 21 allowed: true
API key 1 request 22 allowed: true
API key 1 request 23 allowed: true
API key 1 request 24 allowed: true
API key 1 request 25 allowed: true
API key 1 request 26 allowed: true
API key 1 request 27 allowed: true
API key 1 request 28 allowed: true
API key 1 request 29 allowed: true
API key 1 request 30 allowed: true
API key 1 request 31 allowed: false
API key 1 request 32 allowed: false
API key 1 request 33 allowed: false
API key 1 request 34 allowed: false
API key 1 request 35 allowed: false
API key 1 request 36 allowed: false
API key 1 request 37 allowed: false
API key 1 request 38 allowed: false
API key 1 request 39 allowed: false
API key 1 request 40 allowed: false
API key 2 request 1 allowed: true
API key 2 request 2 allowed: true
API key 2 request 3 allowed: true
API key 2 request 4 allowed: true
API key 2 request 5 allowed: true
API key 2 request 6 allowed: true
API key 2 request 7 allowed: true
API key 2 request 8 allowed: true
API key 2 request 9 allowed: true
API key 2 request 10 allowed: true
API key 2 request 11 allowed: true
API key 2 request 12 allowed: true
API key 2 request 13 allowed: true
API key 2 request 14 allowed: true
API key 2 request 15 allowed: true
API key 2 request 16 allowed: true
API key 2 request 17 allowed: true
API key 2 request 18 allowed: true
API key 2 request 19 allowed: true
API key 2 request 20 allowed: true
API key 2 request 21 allowed: true
API key 2 request 22 allowed: true
API key 2 request 23 allowed: true
API key 2 request 24 allowed: true
API key 2 request 25 allowed: true
API key 2 request 26 allowed: true
API key 2 request 27 allowed: true
API key 2 request 28 allowed: true
API key 2 request 29 allowed: true
API key 2 request 30 allowed: true
API key 2 request 31 allowed: true
API key 2 request 32 allowed: true
API key 2 request 33 allowed: true
API key 2 request 34 allowed: true
API key 2 request 35 allowed: true
API key 2 request 36 allowed: true
API key 2 request 37 allowed: true
API key 2 request 38 allowed: true
API key 2 request 39 allowed: true
API key 2 request 40 allowed: true
API key 2 request 41 allowed: true
API key 2 request 42 allowed: true
API key 2 request 43 allowed: true
API key 2 request 44 allowed: true
API key 2 request 45 allowed: true
API key 2 request 46 allowed: true
API key 2 request 47 allowed: true
API key 2 request 48 allowed: true
API key 2 request 49 allowed: true
API key 2 request 50 allowed: true
API key 2 request 51 allowed: true
API key 2 request 52 allowed: true
API key 2 request 53 allowed: true
API key 2 request 54 allowed: true
API key 2 request 55 allowed: true
API key 2 request 56 allowed: true
API key 2 request 57 allowed: true
API key 2 request 58 allowed: true
API key 2 request 59 allowed: true
API key 2 request 60 allowed: true
API key 2 request 61 allowed: false
API key 2 request 62 allowed: false
API key 2 request 63 allowed: false
API key 2 request 64 allowed: false
API key 2 request 65 allowed: false
API key 2 request 66 allowed: false
API key 2 request 67 allowed: false
API key 2 request 68 allowed: false
API key 2 request 69 allowed: false
API key 2 request 70 allowed: false
Like this:
Like Loading...
Related
Your content is consistently exceptional.
You’ve given me a lot to think about, thanks for such a stimulating article.
I always appreciate your balanced view on things. This post was no exception.
Its like you read my mind You appear to know so much about this like you wrote the book in it or something I think that you can do with a few pics to drive the message home a little bit but instead of that this is excellent blog A fantastic read Ill certainly be back
Program iz This is really interesting, You’re a very skilled blogger. I’ve joined your feed and look forward to seeking more of your magnificent post. Also, I’ve shared your site in my social networks!
Real Estate I like the efforts you have put in this, regards for all the great content.
Internet Chicks naturally like your web site however you need to take a look at the spelling on several of your posts. A number of them are rife with spelling problems and I find it very bothersome to tell the truth on the other hand I will surely come again again.
Tech to Trick I appreciate you sharing this blog post. Thanks Again. Cool.
Family Dollar I am truly thankful to the owner of this web site who has shared this fantastic piece of writing at at this place.
Your blog is a breath of fresh air in the often stagnant world of online content. Your thoughtful analysis and insightful commentary never fail to leave a lasting impression. Thank you for sharing your wisdom with us.
Your blog is a beacon of light in the often murky waters of online content. Your thoughtful analysis and insightful commentary never fail to leave a lasting impression. Keep up the amazing work!
Tech Learner I appreciate you sharing this blog post. Thanks Again. Cool.
Newtoki I really like reading through a post that can make men and women think. Also, thank you for allowing me to comment!
Your blog has quickly become one of my favorites. Your writing is both insightful and thought-provoking, and I always come away from your posts feeling inspired. Keep up the phenomenal work!
Mountsinai Awesome! Its genuinely remarkable post, I have got much clear idea regarding from this post . Mountsinai
BWER Company is Iraq’s leading supplier of advanced weighbridge systems, offering reliable, accurate, and durable solutions for industrial and commercial needs, designed to handle heavy-duty weighing applications across various sectors.
BWER is Iraq’s go-to provider for weighbridges, ensuring durability, accuracy, and cost-efficiency in all weighing solutions, backed by exceptional customer support and maintenance services.
Noodlemagazine I just like the helpful information you provide in your articles