Reputation: 2441
I would like to optimize and reduce REST-API usage abuse for my service.
So, is there any "out of the box" way to limit the amount of concurrent calls to a webserivce on java spring?
for instance, if I have this method below
@RequestMapping(value="demo",method=RequestMethod.GET)
public synchronized @ResponseBody ObjectNode getDemo()
{
logger.info("started.. demo ");
ObjectNode node = om.createObjectNode().put("test", true);
logger.info("end.. demo");
return node;
}
i will be limiting this service to only 1 user at time. but, how about limiting it to 4 or 10 concurrent users?
Thanks :)
Upvotes: 3
Views: 1797
Reputation: 4024
There is no out-of-box rate limiting support for Spring as of now.
However if you have / can use Google Guava then there is a support in form of RateLimiter
class which has detailed explanation here.
In case Google Gauva is not an option consider implementing rate limiter via Token Bucket algorithm.
Sample implementation should have below components -
web.xml
and avoid /*
pattern)Bucket - In its simplest form a bucket can be created as a cache entry with key name as the URL and value as the available token(s) as shown in example below
key = '/test/demo' value = 5
key = '/test/otherDemo' value = 10
Preferably use AtomicInteger
to hold the token count to avoid any concurrent access issue
Filter - Here simply try acquiring a token from bucket (i.e. read the cache and look for positive value of token; decrement the token value by one for every request until zero). Proceed with request only when token is acquired else block request with HTTP status code 429
Scheduler - A cron based scheduled service (see Spring @Scheduled
) to reset the cache with maximum allowed request per unit of time.
Let me know in comments if you need more information.
Upvotes: 1