Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throttle HTTP request in Java Servlet

In a java servlet, how can I throttle http requests coming from users based on the client's IP address? I do not want to serve more than X requests per second coming from a particular source IP address where X is configurable and having practical values in [0.1; 10] range (from 1 request in 10 sec to 10 requests per sec).

like image 445
Prakash Sharma Avatar asked Jun 02 '11 01:06

Prakash Sharma


2 Answers

The owasp-esapi-java project, hosted at code.google.com, has an implementation of a throttle filter that you can use "as is" or use as inspiration for your own.

You can check the code at the following link:

http://code.google.com/p/owasp-esapi-java/source/browse/trunk/src/main/java/org/owasp/esapi/filters/RequestRateThrottleFilter.java

like image 198
Philippe Sevestre Avatar answered Oct 09 '22 22:10

Philippe Sevestre


Use a servlet filter: if you're on Jetty 7.0 or higher there is this

like image 43
Femi Avatar answered Oct 09 '22 20:10

Femi