How to invoke Reset Quota Policy ?

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:

  1. 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.
  2. 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);
  3. 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>
  4. 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.

Comments
madhuwarriors
New Member

demoproxy-rev1-2019-03-16.zip

Hi Devegowda,

I followed the two methods but still i do not see reset quota working. Attached the proxy for first method using fault rules. Please view it and tell me if you find any mistakes. I could not figure out what is wrong with that.

jainpankaj-15m
New Member

Hi @GMS

I went through you proxy configuration. I noticed that Identifier used by you is default which might be causing issue. I modified your proxy slightly and its working. Have a look in attachement.

I could use method 2 as well and found it's working. Attaching proxy bundle for this as well.

Note: Make sure when you call proxy must pass Identifier in Header which is defined in your proxy. In my case it is clientId. Example:clientId=Test.
8334-demoproxy-rev1-2019-03-16-rev1-2019-03-31.zip
resetquotademo-rev6-2019-03-31.zip

Do let me know if it works for you.

madhuwarriors
New Member

Hi Pankaj,

Thanks for correcting the mistakes. I am able to reset using method 1 but using method 2, i do not see reset quota executing. I am unable to download your proxy due to permission issue.

I attached my proxy developed using method 2.resetquota-conditions-rev2-2019-04-01.zip

jainpankaj-15m
New Member

Hi GMS,

Did you try debugging your proxy in trace tool? I could see your Reset policy is always skipping. When I checked your js file I could see wrong policy name (QuotaPolicy) is used.

Here is your js code:

allowedCount = context.getVariable("ratelimit.QuotaPolicy.allowed.count");
usedCount = context.getVariable("ratelimit.QuotaPolicy.used.count");

Infact your Quota Policy name is Quota-1. Replace above two lines with below code in js file and all should be working.

allowedCount = context.getVariable("ratelimit.Quota-1.allowed.count");
usedCount = context.getVariable("ratelimit.Quota-1.used.count");
Version history
Last update:
‎12-30-2017 03:17 AM
Updated by: