Throttling pattern

throttling-pattern.png

Interface Throttler

Throttler defined a simple method used to call throttler implementaton.

public void start()

Implementaton

ThrottleTimerImpl implement Throttler used to cyclical reset service visit counter.

1
2
3
4
5
6
new Timer(true).schedule(new TimerTask() {
@Override
public void run() {
callsCount.reset();
}
}, 0, throttlePeriod);

Counter

CallsCount use ConcurrentHashMap to implement multi thread save counter.

private Map<String, AtomicLong> tenantCallsCount = new ConcurrentHashMap<>();

POJO

Tenant represent a kind of business.
have properties name and allowedCallsPerSecond

Service

XXXService call Throttler to start reset thread.
XXXService call counter to check whether ths visit under threshold.

1
2
3
4
5
6
7
String tenantName = tenant.getName();
long count = callsCount.getCount(tenantName);
if (count >= tenant.getAllowedCallsPerSecond()) {
// TODO throw excepton or return error code
return ;
}
callsCount.incrementCount(tenantName);

Summary

Throttling is used to limit client visit frequency, so we need:
a overall situation counter
a daemonize thread resetor
a business represent pojo
a limited flow service