The Traffic Shaper allows for customizable time-windowed (both sliding and tumbling windows) traffic shaping (eg. rate throttling of any invocations in a selectable time-unit). An example use would be say we want to generate an event if our subscription service receives 20000 new user subscription messages in 3 seconds.
Since throttling is in-memory but at the same time, we care for data reliability, there's full state-snapshotting available on-demand or pre-shutdown; similarly state-reconstruction happens at boot-time but is also available on-demand.
Two modes of traffic shaping are supported via Leaky and Token buckets.
Assuming you dropped the ratethrottler jar in your classpath:
- Create an instance
ServiceRateThrottler throttler = new ServiceRateThrottler();
- Create an invocation
Invocation invocation = new Invocation("dispatchPayment", 50000L, 5L, WindowType.SECONDS);
- Register invocation's interest with the throttler
throttler.setupInvocationThrottler(invocation);
- Tick the event at every invocation and watch outcome
boolean limitReached = throttler.throttle(invocation);
void setupInvocationThrottler(final Invocation invocation)
boolean throttle(final Invocation invocation)
void purgeInvocationThrottler(final Invocation invocation)
void dropInvocationThrottler(final Invocation invocation)
int reportActiveThrottlerCount()
boolean existsInvocationThrottler(final Invocation invocation)
void purgeAllState()
String takeSnapshot()
void reconstructFromSnapshot(String bsonSnapshot)
MIT License - Copyright (c) 2012 Gaurav Sharma