Skip to content

Commit 4e29041

Browse files
authored
Merge pull request iluwatar#629 from rastdeepanshu/throttling-pattern
Throttling pattern
2 parents c136618 + 50d7dbe commit 4e29041

File tree

14 files changed

+597
-2
lines changed

14 files changed

+597
-2
lines changed

pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@
145145
<module>cqrs</module>
146146
<module>event-sourcing</module>
147147
<module>data-transfer-object</module>
148-
</modules>
148+
<module>throttling</module>
149+
</modules>
149150

150151
<dependencyManagement>
151152
<dependencies>
@@ -488,4 +489,4 @@
488489
</plugins>
489490
</reporting>
490491

491-
</project>
492+
</project>

throttling/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
layout: pattern
3+
title: Throttling pattern
4+
folder: throttling
5+
permalink: /patterns/throttling/
6+
tags:
7+
- Java
8+
- Difficulty-Beginner
9+
- Throttling
10+
---
11+
12+
## Intent
13+
Ensure that a given client is not able to access service resources more than the assigned limit.
14+
![alt text](./etc/throttling-patern.png "Throttling pattern")
15+
16+
## Applicability
17+
The Throttling pattern should be used:
18+
19+
* when a service access needs to be restricted to not have high impacts on the performance of the service.
20+
* when multiple clients are consuming the same service resources and restriction has to be made according to the usage per client.

throttling/etc/throttling-pattern.png

59 KB
Loading

throttling/etc/throttling.urm.puml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
@startuml
2+
package com.iluwatar.tls {
3+
class App {
4+
- LOGGER : Logger {static}
5+
+ App()
6+
+ main(args : String[]) {static}
7+
- makeServiceCalls(service : B2BService) {static}
8+
}
9+
~class B2BService {
10+
- LOGGER : Logger {static}
11+
- callsCounter : int
12+
- tenant : Tenant
13+
+ B2BService(tenant : Tenant)
14+
+ dummyCustomerApi() : int
15+
+ getCurrentCallsCount() : int
16+
- getRandomCustomerId() : int
17+
}
18+
class Tenant {
19+
- allowedCallsPerSecond : int
20+
- name : String
21+
+ Tenant(name : String, allowedCallsPerSecond : int)
22+
+ getAllowedCallsPerSecond() : int
23+
+ getName() : String
24+
+ setAllowedCallsPerSecond(allowedCallsPerSecond : int)
25+
+ setName(name : String)
26+
}
27+
}
28+
B2BService --> "-tenant" Tenant
29+
@enduml

throttling/pom.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2014 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<parent>
30+
<artifactId>java-design-patterns</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.17.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>throttling</artifactId>
37+
<dependencies>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
</dependency>
42+
</dependencies>
43+
44+
</project>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014-2016 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.throttling;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import com.iluwatar.throttling.timer.Throttler;
30+
import com.iluwatar.throttling.timer.ThrottleTimerImpl;
31+
32+
import java.util.concurrent.ExecutorService;
33+
import java.util.concurrent.Executors;
34+
import java.util.concurrent.TimeUnit;
35+
36+
/**
37+
* Throttling pattern is a design pattern to throttle or limit the use of resources or even a complete service by
38+
* users or a particular tenant. This can allow systems to continue to function and meet service level agreements,
39+
* even when an increase in demand places load on resources.
40+
* <p>
41+
* In this example we have ({@link App}) as the initiating point of the service.
42+
* This is a time based throttling, i.e. only a certain number of calls are allowed per second.
43+
* </p>
44+
* ({@link Tenant}) is the Tenant POJO class with which many tenants can be created
45+
* ({@link B2BService}) is the service which is consumed by the tenants and is throttled.
46+
*/
47+
public class App {
48+
49+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
50+
51+
/**
52+
* Application entry point
53+
* @param args main arguments
54+
*/
55+
public static void main(String[] args) {
56+
57+
Tenant adidas = new Tenant("Adidas", 5);
58+
Tenant nike = new Tenant("Nike", 6);
59+
60+
ExecutorService executorService = Executors.newFixedThreadPool(2);
61+
62+
executorService.execute(() -> makeServiceCalls(adidas));
63+
executorService.execute(() -> makeServiceCalls(nike));
64+
65+
executorService.shutdown();
66+
try {
67+
executorService.awaitTermination(10, TimeUnit.SECONDS);
68+
} catch (InterruptedException e) {
69+
LOGGER.error("Executor Service terminated: {}", e.getMessage());
70+
}
71+
}
72+
73+
/**
74+
* Make calls to the B2BService dummy API
75+
* @param service an instance of B2BService
76+
*/
77+
private static void makeServiceCalls(Tenant tenant) {
78+
Throttler timer = new ThrottleTimerImpl(10);
79+
B2BService service = new B2BService(timer);
80+
for (int i = 0; i < 20; i++) {
81+
service.dummyCustomerApi(tenant);
82+
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
83+
try {
84+
Thread.sleep(1);
85+
} catch (InterruptedException e) {
86+
LOGGER.error("Thread interrupted: {}", e.getMessage());
87+
}
88+
}
89+
}
90+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.throttling;
24+
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import com.iluwatar.throttling.timer.Throttler;
29+
30+
import java.util.concurrent.ThreadLocalRandom;
31+
32+
/**
33+
* A service which accepts a tenant and throttles the resource based on the time given to the tenant.
34+
*/
35+
class B2BService {
36+
37+
private static final Logger LOGGER = LoggerFactory.getLogger(B2BService.class);
38+
39+
public B2BService(Throttler timer) {
40+
timer.start();
41+
}
42+
43+
/**
44+
*
45+
* @return customer id which is randomly generated
46+
*/
47+
public int dummyCustomerApi(Tenant tenant) {
48+
String tenantName = tenant.getName();
49+
int count = CallsCount.getCount(tenantName);
50+
LOGGER.debug("Counter for {} : {} ", tenant.getName(), count);
51+
if (count >= tenant.getAllowedCallsPerSecond()) {
52+
LOGGER.error("API access per second limit reached for: {}", tenantName);
53+
return -1;
54+
}
55+
CallsCount.incrementCount(tenantName);
56+
return getRandomCustomerId();
57+
}
58+
59+
private int getRandomCustomerId() {
60+
return ThreadLocalRandom.current().nextInt(1, 10000);
61+
}
62+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.throttling;
24+
25+
import java.util.Map;
26+
import java.util.Map.Entry;
27+
import java.util.concurrent.ConcurrentHashMap;
28+
29+
/**
30+
* A class to keep track of the counter of different Tenants
31+
* @author drastogi
32+
*
33+
*/
34+
public final class CallsCount {
35+
private static Map<String, Integer> tenantCallsCount = new ConcurrentHashMap<>();
36+
37+
/**
38+
* Add a new tenant to the map.
39+
* @param tenantName name of the tenant.
40+
*/
41+
public static void addTenant(String tenantName) {
42+
if (!tenantCallsCount.containsKey(tenantName)) {
43+
tenantCallsCount.put(tenantName, 0);
44+
}
45+
}
46+
47+
/**
48+
* Increment the count of the specified tenant.
49+
* @param tenantName name of the tenant.
50+
*/
51+
public static void incrementCount(String tenantName) {
52+
tenantCallsCount.put(tenantName, tenantCallsCount.get(tenantName) + 1);
53+
}
54+
55+
/**
56+
*
57+
* @param tenantName name of the tenant.
58+
* @return the count of the tenant.
59+
*/
60+
public static int getCount(String tenantName) {
61+
return tenantCallsCount.get(tenantName);
62+
}
63+
64+
/**
65+
* Resets the count of all the tenants in the map.
66+
*/
67+
public static void reset() {
68+
for (Entry<String, Integer> e : tenantCallsCount.entrySet()) {
69+
tenantCallsCount.put(e.getKey(), 0);
70+
}
71+
}
72+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* The MIT License
3+
* Copyright (c) 2014 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
package com.iluwatar.throttling;
24+
25+
import java.security.InvalidParameterException;
26+
27+
/**
28+
* A Pojo class to create a basic Tenant with the allowed calls per second.
29+
*/
30+
public class Tenant {
31+
32+
private String name;
33+
private int allowedCallsPerSecond;
34+
35+
/**
36+
*
37+
* @param name Name of the tenant
38+
* @param allowedCallsPerSecond The number of calls allowed for a particular tenant.
39+
* @throws InvalidParameterException If number of calls is less than 0, throws exception.
40+
*/
41+
public Tenant(String name, int allowedCallsPerSecond) {
42+
if (allowedCallsPerSecond < 0) {
43+
throw new InvalidParameterException("Number of calls less than 0 not allowed");
44+
}
45+
this.name = name;
46+
this.allowedCallsPerSecond = allowedCallsPerSecond;
47+
CallsCount.addTenant(name);
48+
}
49+
50+
public String getName() {
51+
return name;
52+
}
53+
54+
public int getAllowedCallsPerSecond() {
55+
return allowedCallsPerSecond;
56+
}
57+
}

0 commit comments

Comments
 (0)