Traffic Shaper
Overview
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.
Quick Start - Rate Throttler
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);
API
1. Setup invocation throttler
void setupInvocationThrottler(final Invocation invocation)
2. Tick invocation and signal throttling outcome
boolean throttle(final Invocation invocation)
3. Purge previous recordings for invocation
void purgeInvocationThrottler(final Invocation invocation)
4. Drop throttler
void dropInvocationThrottler(final Invocation invocation)
5. Count active throttlers
int reportActiveThrottlerCount()
6. Check existence of throttler for invocation
boolean existsInvocationThrottler(final Invocation invocation)
7. Purge all runtime throttling state
void purgeAllState()
8. Snaphsot all runtime throttling state to bson
String takeSnapshot()
9. Reconstruct throttling state from a previous snapshot
void reconstructFromSnapshot(String bsonSnapshot)
License
MIT License - Copyright (c) 2012 Gaurav Sharma