Skip to content

A sample Spring Boot application with rate limiting functionality

Notifications You must be signed in to change notification settings

theBaffo/spring-boot-sample-rate-limited-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sample Rate Limited API (Spring Boot)

This is a sample Spring Boot application that implements a Rate Limit functionality.

Architecture

The main components of the application are:

  • Controllers
    • HotelController: The main entry point of the application, where the routes are defined. The methods here simply call the corresponding HotelService methods, after checking that the request is allowed: this functionality provided by the RateLimiter class.
  • Models
    • Hotel: A simple POJO describing an Hotel entity. This class is used to persist and retrieve Hotel information from the Database using JPA.
  • Repositories
    • HotelRepository: This interface defines the method that our Repository should implement. Rather than creating the class directly, we created an interface first so we can inject it where needed using Dependency Injection.
    • HotelRepositoryImpl: Our implementation of the HotelRepository interface. The methods here simply call the corresponding HotelJpaRepository methods, and also apply the sorting logic.
    • HotelJpaRepository: This interface provides access to the Hotel entities stored in the Database. By extending the Spring Repository class, we get "for free" the methods "findByCityIgnoreCase" and "findByRoomIgnoreCase" that return a List of Hotel filtered by the corresponding city / room.
  • Services
    • HotelService: This interface defines the method that our Service should implement. Rather than creating the class directly, we created an interface first so we can inject it where needed using Dependency Injection.
    • HotelServiceImpl: Our implementation of the HotelService interface. The methods here simply call the corresponding HotelRepository methods.
  • Utils
    • RateLimiter: A simple implementation of a Rate Limiter (Max N requests in M milliseconds). This class keeps reference of the timing of the last N requests using a Queue: when we reach the Nth request, if the time difference between the 0th and the Nth requests is less than M, then the request gets blocked. Also, that endpoint becomes unavailable for RateLimiter.API_BLOCKED_TIME_MS.

Also:

  • This Spring Boot application uses an H2 in-memory database that is accessed using JPA. The Database gets build automatically by the Spring Framework, and seeded using /src/main/resources/data.sql.

Testing

Every component of the application is covered by Unit Tests: all the components are tested in isolation by injecting mocked instances (created with Mockito) to the constructor.

We also have several Integration Tests (in the ApplicationTests class) that verify the correct behaviour of the application by simulating HTTP calls to the API.

Endpoints

The application expose two endpoints:

  • GET /city/{city}
    • optional: "?sortByPrice=ASC" (or DESC)
    • This endpoint returns all the Hotel in a specific city
  • GET /room/{room}
    • optional: "?sortByPrice=ASC" (or DESC)
    • This endpoint returns all the Hotel with a specific room

Here is some examples of HTTP calls to the API:

  • Retrieve all the hotels in Bangkok, order by price.
    • (GET) localhost:8080/city/Bangkok?sortByPrice=ASC
  • Retrieve all the hotels with "Deluxe" rooms, order by price (reversed).
    • (GET) localhost:8080/room/Deluxe?sortByPrice=DESC

Run the application

To run the application with Gradle:

$ gradlew bootRun

The application will be accessible at http://localhost:8080 from your browser.

The port can be easily modified by changing the server.port value in the application.properties file.

Test the application

To test the application with Gradle:

$ gradlew clean test

The result of the tests will be printed once all the test are executed.

About

A sample Spring Boot application with rate limiting functionality

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages