Merge branch 'master' of https://github.com/iluwatar/java-design-patterns
This commit is contained in:
commit
04dd93f8bc
@ -18,10 +18,10 @@ incomplete or inappropriate state
|
||||
## Applicability
|
||||
Use the Balking pattern when
|
||||
|
||||
*you want to invoke an action on an object only when it is in a particular state
|
||||
*objects are generally only in a state that is prone to balking temporarily
|
||||
* you want to invoke an action on an object only when it is in a particular state
|
||||
* objects are generally only in a state that is prone to balking temporarily
|
||||
but for an unknown amount of time
|
||||
|
||||
## Related patterns
|
||||
* Guarded Suspension Pattern
|
||||
* Double Checked Locking Pattern
|
||||
* Double Checked Locking Pattern
|
||||
|
@ -95,7 +95,7 @@ public class FactoryMethodTest {
|
||||
*/
|
||||
private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class<?> clazz) {
|
||||
assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon));
|
||||
assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType,
|
||||
assertEquals("Weapon must be of weaponType: " + expectedWeaponType, expectedWeaponType,
|
||||
weapon.getWeaponType());
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ public class PrototypeTest<P extends Prototype> {
|
||||
/**
|
||||
* The tested prototype instance
|
||||
*/
|
||||
private final Prototype testedPrototype;
|
||||
private final P testedPrototype;
|
||||
|
||||
/**
|
||||
* The expected {@link Prototype#toString()} value
|
||||
@ -69,7 +69,7 @@ public class PrototypeTest<P extends Prototype> {
|
||||
* @param testedPrototype The tested prototype instance
|
||||
* @param expectedToString The expected {@link Prototype#toString()} value
|
||||
*/
|
||||
public PrototypeTest(final Prototype testedPrototype, final String expectedToString) {
|
||||
public PrototypeTest(final P testedPrototype, final String expectedToString) {
|
||||
this.expectedToString = expectedToString;
|
||||
this.testedPrototype = testedPrototype;
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ class B2BService {
|
||||
*/
|
||||
public int dummyCustomerApi(Tenant tenant) {
|
||||
String tenantName = tenant.getName();
|
||||
int count = CallsCount.getCount(tenantName);
|
||||
long count = CallsCount.getCount(tenantName);
|
||||
LOGGER.debug("Counter for {} : {} ", tenant.getName(), count);
|
||||
if (count >= tenant.getAllowedCallsPerSecond()) {
|
||||
LOGGER.error("API access per second limit reached for: {}", tenantName);
|
||||
|
@ -22,9 +22,13 @@
|
||||
*/
|
||||
package com.iluwatar.throttling;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
/**
|
||||
* A class to keep track of the counter of different Tenants
|
||||
@ -32,16 +36,16 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
*
|
||||
*/
|
||||
public final class CallsCount {
|
||||
private static Map<String, Integer> tenantCallsCount = new ConcurrentHashMap<>();
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CallsCount.class);
|
||||
private static Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Add a new tenant to the map.
|
||||
* @param tenantName name of the tenant.
|
||||
*/
|
||||
public static void addTenant(String tenantName) {
|
||||
if (!tenantCallsCount.containsKey(tenantName)) {
|
||||
tenantCallsCount.put(tenantName, 0);
|
||||
}
|
||||
tenantCallsCount.putIfAbsent(tenantName, new AtomicLong(0));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -49,7 +53,7 @@ public final class CallsCount {
|
||||
* @param tenantName name of the tenant.
|
||||
*/
|
||||
public static void incrementCount(String tenantName) {
|
||||
tenantCallsCount.put(tenantName, tenantCallsCount.get(tenantName) + 1);
|
||||
tenantCallsCount.get(tenantName).incrementAndGet();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -57,16 +61,17 @@ public final class CallsCount {
|
||||
* @param tenantName name of the tenant.
|
||||
* @return the count of the tenant.
|
||||
*/
|
||||
public static int getCount(String tenantName) {
|
||||
return tenantCallsCount.get(tenantName);
|
||||
public static long getCount(String tenantName) {
|
||||
return tenantCallsCount.get(tenantName).get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the count of all the tenants in the map.
|
||||
*/
|
||||
public static void reset() {
|
||||
for (Entry<String, Integer> e : tenantCallsCount.entrySet()) {
|
||||
tenantCallsCount.put(e.getKey(), 0);
|
||||
LOGGER.debug("Resetting the map.");
|
||||
for (Entry<String, AtomicLong> e : tenantCallsCount.entrySet()) {
|
||||
tenantCallsCount.put(e.getKey(), new AtomicLong(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,14 +36,13 @@ public class B2BServiceTest {
|
||||
@Test
|
||||
public void dummyCustomerApiTest() {
|
||||
Tenant tenant = new Tenant("testTenant", 2);
|
||||
Throttler timer = new ThrottleTimerImpl(10);
|
||||
Throttler timer = new ThrottleTimerImpl(100);
|
||||
B2BService service = new B2BService(timer);
|
||||
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
service.dummyCustomerApi(tenant);
|
||||
}
|
||||
|
||||
int counter = CallsCount.getCount(tenant.getName());
|
||||
long counter = CallsCount.getCount(tenant.getName());
|
||||
Assert.assertTrue("Counter limit must be reached", counter == 2);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user