Time-based custom throttling policies - WSO2 API Manager

Have you come across a scenario, where you wanted to assign a set of custom throttling policies based on the time.

Ex: 1. limit the request count as below
9:00 AM to 6.00 PM -> 10000/min
6.01 PM to 8:59 AM -> 20000/min

Ex: 2. Throttleout all the requests within the specific time period(10.00–10.30)

We are using the Custom Throttling Policy which is an admin functionality of WSO2 API Manager to achieve this.

Custom throttling allows system administrators to define dynamic rules for specific use cases, which are applied globally across all tenants. When a custom throttling policy is created, it is possible to define any policy you like. The Traffic Manager acts as the global throttling engine and is based on the same technology as WSO2 Complex Event Processor (CEP), which uses the Siddhi query language. Users are therefore able to create their own custom throttling policies by writing custom Siddhi queries. [1]

Let's get started

➡️ Login to the https:://<APIM — HOST>:<PORT>/admin portal

➡️ Go to the “Throttling Policies” > “Custom Policies” > “Define Policy”

Ex: 1. limit the request count as below
9:00 AM to 6.00 PM -> 10000/min
6.01 PM to 8:59 AM -> 20000/min

➡️ Add the following siddhi query (Key Template: $userId)

FROM RequestStream
SELECT userId, ( 1==1) AS isEligible, str:concat(userId,'') AS throttleKey
INSERT INTO EligibilityStream;

FROM EligibilityStream[ isEligible==true and ( ( 6 >= convert(time:dateFormat(time:currentTimestamp(), 'HH', 'yyyy-MM-dd HH:mm:ss'),'int')) and ( convert(time:dateFormat(time:currentTimestamp(), 'HH', 'yyyy-MM-dd HH:mm:ss'),'int') <= 18)) ]#throttler:timeBatch(1 min)
select throttleKey, (count(userId) >= 10000) as isThrottled, expiryTimeStamp group by throttleKey
INSERT ALL EVENTS into ResultStream;

Above will throttle out the requests for users between 9:00 AM to 6.00 PM. Then we need to create another policy to cater “6.01 PM to 8:59 AM” scenario.

Ex: 2. Throttleout all the requests within the specific time period(10.00–10.30)

➡️ Add the following siddhi query (Key Template: $userId)

FROM   RequestStream 
SELECT userId, (1 == 1) AS isEligible, str :concat(userId, '') AS throttleKey
INSERT INTO EligibilityStream;
FROM EligibilityStream [ isEligible==true and (( 10 == convert(time:dateFormat(time:currentTimestamp(), 'HH', 'yyyy-MM-dd HH:mm:ss'),'int')) and ( convert(time:dateFormat(time:currentTimestamp(), 'mm', 'yyyy-MM-dd HH:mm:ss'),'int') < 30)) ]#throttler:timeBatch(1 min) select throttleKey, (1 == 1) as isThrottled, expiryTimeStamp group by throttleKey INSERT ALL EVENTS into ResultStream;

Above will throttle out the requests for users between 10.00–10.30 AM by setting the isThrottled as true.

Hope this will help you…
Happy Throttling!
CHEERS! ✌️

[1] https://docs.wso2.com/display/AM260/Adding+New+Throttling+Policies#AddingNewThrottlingPolicies-Addingacustomthrottlingpolicy

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store