This is a simple Java rate limit to HTTP request.
Rate limiting refers to preventing the frequency of an operation from exceeding some constraint. In large-scale systems, rate limiting is commonly used to protect underlying services and resources.
Definition by: Google Cloud Architecture
This project has been tested and confirmed to work with Java 6
, Java 7
and Java 8
, but may also work with other versions of Java. However, these other versions have not been tested.
- Build the project with the command
mvn package
. - Deploy the generated
.war
file located in thetarget
directory to thewebapps
directory of your Tomcat installation. - Start the Tomcat server by running the
startup
script located in thebin
directory of your Tomcat installation.
You may need to modify the pom.xml
file to add any necessary dependencies for your project. You can also modify the web.xml
file located in the src/main/webapp/WEB-INF
directory to configure your servlets and mappings.
If you need to configure any additional settings for your Tomcat installation, you can do so in the conf
directory of your Tomcat installation.
This is a demonstration of rate limit working in an attack started by Owasp Zap tool with 5 threads requests.
- Add RateLimit class in your project.
- Create an instance of the RateLimit class and run the doFilter method around your HTTP endpoint.
...
rateLimit = RateLimit.getInstance();
...
if (rateLimit.doFilter(request)) {
json = "{\"message\": \"ok\"}";
response.setStatus(200);
} else {
json = "{\"message\": \"too many requests\"}";
response.setStatus(429);
}
For more details, see the example of the MyServlet class.
If you want to contribute to this project, please follow the guidelines below:
- Fork the repository.
- Create a new branch:
git checkout -b <my-new-branch>
- Make changes and test them.
- Add the changes:
git add .
- Commit your changes:
git commit -m "Add some feature"
- Push to the branch:
git push origin <my-new-branch>
- Submit a pull request to the original repository.
This project is licensed under the MIT License.