Throttling pattern#629
Conversation
| --- | ||
|
|
||
| ## Intent | ||
| Ensure that a given tenant is not able to access resources more than the assigned limit. |
There was a problem hiding this comment.
Should we talk about client here instead of tenant? And service resources instead of just resources. Check for occurrences elsewhere too.
| * THE SOFTWARE. | ||
| */ | ||
|
|
||
| package com.iluwatar.tls; |
| Tenant nike = new Tenant("Nike", 70); | ||
|
|
||
| B2BService adidasService = new B2BService(adidas); | ||
| B2BService nikeService = new B2BService(nike); |
There was a problem hiding this comment.
The relationship between service and tenant here is one-to-one which seems unnatural. The service should have multiple tenants, I think.
There was a problem hiding this comment.
B2BService is one service which serves multiple tenants. As I am not dealing with the APIs, one instance of the service deals with one tenant.
There was a problem hiding this comment.
I get your point now.
| Runnable nikeTask = () -> makeServiceCalls(nikeService); | ||
|
|
||
| new Thread(adidasTask).start(); | ||
| new Thread(nikeTask).start(); |
There was a problem hiding this comment.
Threads should not be instantiated directly. Instead use ExecutorService.
| try { | ||
| Thread.sleep(10); | ||
| } catch (InterruptedException e) { | ||
| e.printStackTrace(); |
|
|
||
| /** | ||
| * A service which accepts a tenant and throttles the resource based on the time given to the tenant. | ||
| */ |
There was a problem hiding this comment.
The class seems to have multiple responsibilities. It offers the service and handles the throttling. Can we separate the throttling to another class? Maybe time based throttling would be one strategy but it could have others.
|
|
||
| /** | ||
| * A timer is initiated as soon as the Service is initiated. The timer runs every minute and resets the | ||
| * counter. |
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } |
There was a problem hiding this comment.
The method is unused and should be removed
|
|
||
| public void setAllowedCallsPerSecond(int allowedCallsPerSecond) { | ||
| this.allowedCallsPerSecond = allowedCallsPerSecond; | ||
| } |
There was a problem hiding this comment.
The method is unused and should be removed
| } | ||
| } | ||
| int counter = service.getCurrentCallsCount(); | ||
| Assert.assertTrue("", counter < 11); |
There was a problem hiding this comment.
Where this magic number 11 comes from?
There was a problem hiding this comment.
sleep for 100 ms means counter can go till 10. Hence the magic number :D
I can do <= 10 if 11 is looking odd.
|
Add |
| @@ -0,0 +1,44 @@ | |||
| <?xml version="1.0" encoding="UTF-8"?> | |||
There was a problem hiding this comment.
The license is incorrectly positioned
There was a problem hiding this comment.
Can you elaborate. I guess the first line has to be this for pom to work correctly.
|
@rastdeepanshu you have my review comments. Please notify when you've implemented the changes. |
|
@iluwatar Can you take a look now? |
|
@rastdeepanshu well done. Thanks for your effort to add this new pattern 👍 |
#456
Created throttling pattern which throttles request of a particular tenant.
The service only allows a specified number of requests for a tenant and rejects the remaining ones.