So I was given the following task:
Build a spring boot application with gradle as the build system.
The application should have 3 controllers, these controllers have the following url mapping: - /api/posts
/api/profile - /api/history
These endpoints are simple controllers, and should return the url of the request as a string, for example:
/api/posts should return “http://localhost:8080/api/posts” the host name should be dynamic depending on the “Host” header of the request.
We need to implement a mechanism to throttle requests if they exceed a certain threshold.
Each endpoint has its threshold configuration:
/api/posts: is open for everyone and doesn’t have a threshold.
/api/profile: has a threshold of 10 requests per minute, if exceeded we need to start
throttling the requests by slowing them down each by 3 seconds.
/api/history: has a threshold of 30 requests per minute, if exceeded we need to start
throttling the requests by slowing them down each by 3 seconds and if more than 30 requests are queued for throttling we need to return 503 ‘Service Unavailable’ for new requests until the queue goes under 30.
Keep in mind that throttling is done per IP address, so requests coming from different IPs should not affect each other.
The solution should be thread safe.
Best practices concerning thread safety, coding styles and clean code should be executed during the implementation of this task.
Test cases should be written to test the code and validate the requirements. Code should be added to github and shared with us.
Here's my code:
package com.atypon.task1.controllers;
@RequestMapping("/api/history")
@Controller
@RequiredArgsConstructor
public class HistoryController {
code
Assistant
Your code is well structured and follows best practices quite well. The use of dependency injection and separation of concerns in your services is very good. Your test cases cover most of the important scenarios, and you have implemented thread-safety as required.
Here are a few suggestions to further improve your code:
In ThrottlingService, instead of using a synchronized block on the ipSessions object, you can make use of ConcurrentHashMap's atomic methods like computeIfAbsent and compute. This will improve the performance of your code by reducing the time your threads spend waiting for access to the shared object.
In ResponseService, you can define constants for the threshold values and the delays like , , , and . This will make the code more maintainable and easy to understand.
@Autowired
private ThrottlingService throttlingService;
@Test
void throttlingRequestsTest() {
String ipAddress = "127.0.0.1";
int maxRequestsBeforeThrottling = 10;
int intervalTime = 60000;
for(int i = 0; i < 10; i++)
Assertions.assertFalse(throttlingService.throttledRequest(ipAddress,
maxRequestsBeforeThrottling, intervalTime));
Assertions.assertTrue(throttlingService.throttledRequest(ipAddress,
maxRequestsBeforeThrottling, intervalTime));
}
@Test
void ipThrottlingIsolationTest() {
String ipAddress = "127.0.0.2";
int maxRequestsBeforeThrottling = 10;
int intervalTime = 60000;
for(int i = 0; i < 10; i++)
Assertions.assertFalse(throttlingService.throttledRequest(ipAddress,
maxRequestsBeforeThrottling, intervalTime));
ipAddress = "127.0.0.3";
Assertions.assertFalse(throttlingService.throttledRequest(ipAddress,
maxRequestsBeforeThrottling, intervalTime));
}
@Test
void overQueuedServiceUnavailableTest() {
String ipAddress = "127.0.0.5";
int maxRequestsBeforeThrottling = 10;
int intervalTime = 60000;
for(int i = 0; i < 10; i++)
Assertions.assertFalse(throttlingService.throttledRequest(ipAddress,
maxRequestsBeforeThrottling, intervalTime));
for(int i = 0; i <= 10; i++)
Assertions.assertTrue(throttlingService.throttledRequest(ipAddress,
maxRequestsBeforeThrottling, intervalTime));
Assertions.assertTrue(throttlingService.exceedsThrottlingLimit(ipAddress, 10));
}
}
Is this enough ? please check the code really well... this is very important for me! is this enough test cases? is the code correct.. ? is there anything i could do to make it better (not general stuff though)
PROFILE_THRESHOLD
HISTORY_THRESHOLD
PROFILE_DELAY
HISTORY_DELAY
When writing test cases, it's always good to verify edge cases as well. You may add some more test cases like the following:
Test for the case where the request rate is just below the threshold.
Test for the case where the request rate is right at the threshold.
Test for cases where multiple IP addresses have requests in progress to ensure proper isolation.
Add more descriptive comments to describe the purpose of methods and what they are doing. This will help others better understand your code as well as act as documentation for future reference.
Overall, your code is quite good, and with these improvements, it will be even better. Good job!