We use Reset Quota Policy to add more number of API requests (quota) that can be made in a given period of time. This is on top of the quota that is already allowed by the Quota Policy for a specific API Proxy. You can read more about it in Reset Quota Policy.
Just thought of sharing the different ways we can invoke Reset Quota Policy based on some experiments that I did recently.
Let’s take an example, where in your Quota Policy allows every developer to make 1000 API requests per week.
Here’s the Quota Policy:
<Quota name="QuotaPolicy">
<DisplayName>QuotaPolicy</DisplayName>
<Properties/>
<Distributed>true</Distributed>
<Synchronous>true</Synchronous>
<Identifier ref="request.header.clientId"/>
<Allow count="1000"/>
<Interval>1</Interval>
<TimeUnit>week</TimeUnit>
</Quota>
Let’s say now you want to increase the quota by 500 more requests per week, incase the original quota gets exceeded before the completion of a week. Here are different ways you can achieve this based on my experiments.
First let’s define the Reset Quota policy:
ResetQuota Policy
<ResetQuota name="IncreaseQuota">
<Quota name="QuotaPolicy">
<Identifier ref="request.header.clientId">
<Allow>500</Allow>
</Identifier>
</Quota>
</ResetQuota>
This policy increases the quota (number of API requests) for the specified clientID by 500.
Method 1: Increase the quota when the original quota is exceeded
When the original quota exceeds, we get a QuotaViolation error.We can trigger the Reset Quota Policy in a Fault Flow when we get a QuotaViolation error and increase the quota as shown below.
<FaultRules>
<FaultRule name="quota-violation”>
<Step>
<Name>IncreaseQuota</Name>
</Step>
<Condition>(fault.name == "QuotaViolation")</Condition>
</FaultRule>
</FaultRules>
Note: This method will ensure that Reset Quota policy is invoked only when the original quota exceeds.
Method 2: Increase the quota before the original quota is exceeded
If you want to increase the quota before the original quota is exceeded. Let’s say you want to increase the quota when the developer exceeds more than 50% of the original quota. You can achieve this as follows:
-
You can used the Quota Policy variables ratelimit.{policy_name}.used.countand ratelimit.{policy_name}.allowed.count to determine the number of requests made and total number of requests allowed respectively.
-
Have a JavaScript(ComputeThreshold.js) after the that computes the 50% of the allowed count (quota)
const threshold = 0.5; // we can also read this from an input parameter or a variable allowedCount = context.getVariable("ratelimit.QuotaPolicy.allowed.count"); usedCount = context.getVariable("ratelimit.QuotaPolicy.used.count"); thresholdCount = threshold * allowedCount; context.setVariable("allowedCount", allowedCount); context.setVariable("usedCount", usedCount); context.setVariable("thresholdCount", thresholdCount); -
You can trigger the Reset Quota policy when the developer has exceeded 50% of the original quota using the condition flow as follows:
<Step> <Condition>(usedCount > thresholdCount) </Condition> <Name>IncreaseQuota</Name> </Step> -
Your complete flow will look something like this:
<PreFlow name="PreFlow"> <Request> <Step> <Name>check_quota_static</Name> </Step> <Step> <Name>JSComputeThreshold</Name> </Step> <Step> <Condition>(usedCount > thresholdCount) </Condition> <Name>IncreaseQuota</Name> </Step> </Request> <Response/> </PreFlow>
Please do feel free to add if there’s any other ways or conditions under which we can use Reset Quota policy.