Skip to content

Commit a6458c4

Browse files
committed
docs: update throttling
1 parent 8372d92 commit a6458c4

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

throttling/README.md

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ In this example a young human and an old dwarf walk into a bar. They start order
3939
`BarCustomer` class presents the clients of the `Bartender` API. `CallsCount` tracks the number of calls per `BarCustomer`.
4040

4141
```java
42-
4342
@Getter
4443
public class BarCustomer {
4544

@@ -55,7 +54,9 @@ public class BarCustomer {
5554
callsCount.addTenant(name);
5655
}
5756
}
57+
```
5858

59+
```java
5960
@Slf4j
6061
public final class CallsCount {
6162
private final Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();
@@ -85,7 +86,9 @@ Next, the service that the tenants are calling is introduced. To track the call
8586
public interface Throttler {
8687
void start();
8788
}
89+
```
8890

91+
```java
8992
public class ThrottleTimerImpl implements Throttler {
9093

9194
private final int throttlePeriod;
@@ -142,36 +145,42 @@ class Bartender {
142145
Now it is possible to see the full example in action. `BarCustomer` young human is rate-limited to 2 calls per second and the old dwarf to 4.
143146

144147
```java
145-
public static void main(String[] args) {
146-
var callsCount = new CallsCount();
147-
var human = new BarCustomer("young human", 2, callsCount);
148-
var dwarf = new BarCustomer("dwarf soldier", 4, callsCount);
148+
@Slf4j
149+
public class App {
149150

150-
var executorService = Executors.newFixedThreadPool(2);
151+
public static void main(String[] args) {
152+
var callsCount = new CallsCount();
153+
var human = new BarCustomer("young human", 2, callsCount);
154+
var dwarf = new BarCustomer("dwarf soldier", 4, callsCount);
151155

152-
executorService.execute(() -> makeServiceCalls(human, callsCount));
153-
executorService.execute(() -> makeServiceCalls(dwarf, callsCount));
156+
var executorService = Executors.newFixedThreadPool(2);
154157

155-
executorService.shutdown();
156-
try {
157-
executorService.awaitTermination(10, TimeUnit.SECONDS);
158-
} catch (InterruptedException e) {
159-
LOGGER.error("Executor service terminated: {}", e.getMessage());
160-
}
161-
}
158+
executorService.execute(() -> makeServiceCalls(human, callsCount));
159+
executorService.execute(() -> makeServiceCalls(dwarf, callsCount));
162160

163-
private static void makeServiceCalls(BarCustomer barCustomer, CallsCount callsCount) {
164-
var timer = new ThrottleTimerImpl(1000, callsCount);
165-
var service = new Bartender(timer, callsCount);
166-
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
167-
IntStream.range(0, 50).forEach(i -> {
168-
service.orderDrink(barCustomer);
161+
executorService.shutdown();
169162
try {
170-
Thread.sleep(100);
163+
if (!executorService.awaitTermination(10, TimeUnit.SECONDS)) {
164+
executorService.shutdownNow();
165+
}
171166
} catch (InterruptedException e) {
172-
LOGGER.error("Thread interrupted: {}", e.getMessage());
167+
executorService.shutdownNow();
173168
}
174-
});
169+
}
170+
171+
private static void makeServiceCalls(BarCustomer barCustomer, CallsCount callsCount) {
172+
var timer = new ThrottleTimerImpl(1000, callsCount);
173+
var service = new Bartender(timer, callsCount);
174+
// Sleep is introduced to keep the output in check and easy to view and analyze the results.
175+
IntStream.range(0, 50).forEach(i -> {
176+
service.orderDrink(barCustomer);
177+
try {
178+
Thread.sleep(100);
179+
} catch (InterruptedException e) {
180+
LOGGER.error("Thread interrupted: {}", e.getMessage());
181+
}
182+
});
183+
}
175184
}
176185
```
177186

@@ -202,10 +211,6 @@ An excerpt from the example's console output:
202211
18:46:37.148 [pool-1-thread-2] ERROR com.iluwatar.throttling.Bartender - I'm sorry dwarf soldier, you've had enough for today!
203212
```
204213

205-
## Class diagram
206-
207-
![Throttling](./etc/throttling_urm.png "Throttling pattern class diagram")
208-
209214
## Applicability
210215

211216
* You need to protect resources from being overwhelmed by too many requests.
@@ -239,5 +244,5 @@ Trade-offs:
239244

240245
## Credits
241246

242-
* [Throttling pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/throttling)
243-
* [Cloud Design Patterns: Prescriptive Architecture Guidance for Cloud Applications (Microsoft patterns & practices)](https://www.amazon.com/gp/product/B00ITGHBBS/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=B00ITGHBBS&linkId=12aacdd0cec04f372e7152689525631a)
247+
* [Throttling pattern (Microsoft)](https://docs.microsoft.com/en-us/azure/architecture/patterns/throttling)
248+
* [Cloud Design Patterns: Prescriptive Architecture Guidance for Cloud Applications](https://amzn.to/4dLvowg)

0 commit comments

Comments
 (0)