docs: update throttling

This commit is contained in:
Ilkka Seppälä committed 2024-05-27 14:09:08 +03:00
1 parent 8372d925f5
commit a6458c469d
1 file changed
+14 -9
+14 -9
View File
@@ -39,7 +39,6 @@ In this example a young human and an old dwarf walk into a bar. They start order
`BarCustomer` class presents the clients of the `Bartender` API. `CallsCount` tracks the number of calls per `BarCustomer`. `BarCustomer` class presents the clients of the `Bartender` API. `CallsCount` tracks the number of calls per `BarCustomer`.
```java ```java
@Getter @Getter
public class BarCustomer { public class BarCustomer {
@@ -55,7 +54,9 @@ public class BarCustomer {
callsCount.addTenant(name); callsCount.addTenant(name);
} }
} }
```
```java
@Slf4j @Slf4j
public final class CallsCount { public final class CallsCount {
private final Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>(); 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
public interface Throttler { public interface Throttler {
void start(); void start();
} }
```
```java
public class ThrottleTimerImpl implements Throttler { public class ThrottleTimerImpl implements Throttler {
private final int throttlePeriod; private final int throttlePeriod;
@@ -142,6 +145,9 @@ class Bartender {
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. 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.
```java ```java
@Slf4j
public class App {
public static void main(String[] args) { public static void main(String[] args) {
var callsCount = new CallsCount(); var callsCount = new CallsCount();
var human = new BarCustomer("young human", 2, callsCount); var human = new BarCustomer("young human", 2, callsCount);
@@ -154,9 +160,11 @@ public static void main(String[] args) {
executorService.shutdown(); executorService.shutdown();
try { try {
executorService.awaitTermination(10, TimeUnit.SECONDS); if (!executorService.awaitTermination(10, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOGGER.error("Executor service terminated: {}", e.getMessage()); executorService.shutdownNow();
} }
} }
@@ -173,6 +181,7 @@ private static void makeServiceCalls(BarCustomer barCustomer, CallsCount callsCo
} }
}); });
} }
}
``` ```
An excerpt from the example's console output: An excerpt from the example's console output:
@@ -202,10 +211,6 @@ An excerpt from the example's console output:
18:46:37.148 [pool-1-thread-2] ERROR com.iluwatar.throttling.Bartender - I'm sorry dwarf soldier, you've had enough for today! 18:46:37.148 [pool-1-thread-2] ERROR com.iluwatar.throttling.Bartender - I'm sorry dwarf soldier, you've had enough for today!
``` ```
## Class diagram
![Throttling](./etc/throttling_urm.png "Throttling pattern class diagram")
## Applicability ## Applicability
* You need to protect resources from being overwhelmed by too many requests. * You need to protect resources from being overwhelmed by too many requests.
@@ -239,5 +244,5 @@ Trade-offs:
## Credits ## Credits
* [Throttling pattern](https://docs.microsoft.com/en-us/azure/architecture/patterns/throttling) * [Throttling pattern (Microsoft)](https://docs.microsoft.com/en-us/azure/architecture/patterns/throttling)
* [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) * [Cloud Design Patterns: Prescriptive Architecture Guidance for Cloud Applications](https://amzn.to/4dLvowg)