Cannot get ResetQuota to reset the quota count

sgilson
Participant V

I have a simple Quota policy that I want to reset by using the ResetQuota policy, but I cannot get ResetQuota to work. My Quota policy:

<Quota async="false" continueOnError="false" enabled="true" name="Quota-PreFlow">
    <DisplayName>Quota-PreFlow</DisplayName>
    <Interval>1</Interval>
    <TimeUnit>hour</TimeUnit>
    <Allow count="100"/>
    <Synchronous>true</Synchronous>
    <Distributed>true</Distributed>
</Quota> 

If I call ResetQuota with no Identifier, current count is unchanged meaning ResetQuota did nothing. Shouldn't it be reset to 100?:

<ResetQuota async="false" continueOnError="false" enabled="true" name="Reset-Preflow-Quota">
    <DisplayName>Reset Preflow Quota</DisplayName>
    <Quota name="Quota-PreFlow">
    </Quota>
</ResetQuota> 

I then added an Identifier in the Quota policy:

<Quota async="false" continueOnError="false" enabled="true" name="Quota-PreFlow">
    <DisplayName>Quota-PreFlow</DisplayName>
    <Interval>1</Interval>
    <TimeUnit>hour</TimeUnit>
    <Allow count="100"/>
    <Synchronous>true</Synchronous>
    <Distributed>true</Distributed>
    <Identifier ref="request.queryparam.id"/>
</Quota> 

Before ResetQuota policy, the count for the id="smg" was 96. After the ResetQuota it was 196, meaning the value in ResetQuota was added to current count - it was not reset to 100.

<ResetQuota async="false" continueOnError="false" enabled="true" name="Reset-Preflow-Quota">
    <DisplayName>Reset Preflow Quota</DisplayName>
    <Quota name="Quota-PreFlow">
        <Identifier name="identifierName" ref="request.queryparam.id">
            <Allow>100</Allow>
        </Identifier>
    </Quota>
</ResetQuota> 

If I take <Allow> out of ResetQuota, then it does nothing. After the call, the current count for the Quota is unchanged:

<ResetQuota async="false" continueOnError="false" enabled="true" name="Reset-Preflow-Quota">
    <DisplayName>Reset Preflow Quota</DisplayName>
    <Quota name="Quota-PreFlow">
        <Identifier name="identifierName" ref="request.queryparam.id">
        </Identifier>
    </Quota>
</ResetQuota>
Solved Solved
2 5 950
1 ACCEPTED SOLUTION

sgilson
Participant V

I updated the ResetQuota doc with this info.

Stephen

View solution in original post

5 REPLIES 5

sgilson
Participant V

Some more info. For the first scenario with no identifier, here are the flow vars.

Before:

ratelimit.Quota-PreFlow.allowed.count 100
ratelimit.Quota-PreFlow.available.count 97
ratelimit.Quota-PreFlow.identifier _default
ratelimit.Quota-PreFlow.total.exceed.count 0
ratelimit.Quota-PreFlow.used.count 3 

After:

ratelimit.Quota-PreFlow.allowed.count 100
ratelimit.Quota-PreFlow.available.count 96
ratelimit.Quota-PreFlow.identifier _default
ratelimit.Quota-PreFlow.total.exceed.count 0
ratelimit.Quota-PreFlow.used.count 4 

With the identifier "smg".

Before:

ratelimit.Quota-PreFlow.allowed.count 100
ratelimit.Quota-PreFlow.available.count 95
ratelimit.Quota-PreFlow.identifier smg
ratelimit.Quota-PreFlow.total.exceed.count 0
ratelimit.Quota-PreFlow.used.count 5

After:

ratelimit.Quota-PreFlow.allowed.count 100
ratelimit.Quota-PreFlow.available.count 194
ratelimit.Quota-PreFlow.identifier smg
ratelimit.Quota-PreFlow.total.exceed.count 0
ratelimit.Quota-PreFlow.used.count -94

@sgilson , I can able to reproduce same, Policy name is misleading in this case. Ideally it should reset to original counter value instead of adding the count to existing available count. Yes, It doesn't work on global identifier.

Thanks @Anil Sagar, based on how it seems to work, the ResetQuota policy should really be called "IncreaseQuota" or "TempIncreaseQuota" because the way it works is:

- Add a value to available.count

- Subtract that same value from used.count

- Discard the changes when the Quota policy next resets

sgilson
Participant V

I updated the ResetQuota doc with this info.

Stephen

I am not able to post a question because the form keeps telling me that the description needs to be a minimum of 60 or something

Sorry if I hijacked this post with my question

I noticed that after I deploy my proxy for a long time, accepting numbers of traffic, the quota counter seems to be stuck at some point

My intention was to limit 60 active request simultaneously, and thus any request over that will hit the quota limit and receive a 429 message. I added a ResetQuota on the way out decrementing by the counter by one. However, I noticed that even though there are no active traffic going on, my quota counter shows usedCount at 6, availableCount at 54. I expected the availableCount to be back to 0 since I am decrementing one on every exit (both success and error)

Here are my policies and Proxy setup.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Quota async="false" continueOnError="false" enabled="true" name="My-Quota" type="flexi">
    <DisplayName>My-Quota</DisplayName>
    <Identifier ref="request.header.apikey"/>
    <Allow count="60"/>
    <Interval>1000</Interval>
    <TimeUnit>month</TimeUnit>
    <Distributed>true</Distributed>
    <Synchronous>true</Synchronous>
</Quota>




<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ResetQuota async="false" continueOnError="false" enabled="true" name="My-ResetQuota">
    <DisplayName>My-ResetQuota</DisplayName>
    <Properties/>
    <Quota name="My-Quota">
        <Identifier name="apikey" ref="request.header.apikey">
            <Allow>1</Allow>
        </Identifier>
    </Quota>
</ResetQuota>






<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
    <Description>My API</Description>
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Condition>(request.verb != "OPTIONS")</Condition>
                <FaultRules/>
                <Name>My-Quota</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
    <Flows/>
    <PostFlow name="PostFlow">
        <Request/>
        <Response>
            <Step>
                <Name>My-ResetQuota</Name>
            </Step>
        </Response>
    </PostFlow>


     ...................


    <FaultRules/>
	
    <DefaultFaultRule name="default_fault_rule">
        <AlwaysEnforce>true</AlwaysEnforce>
        <Step>
            <Name>My-ResetQuota</Name>
            <Condition>not (fault.name = "QuotaViolation")</Condition>
        </Step>
    </DefaultFaultRule>
    <PostClientFlow>
        <Request/>
        <Response>
        
          ..................		
        
        </Response>
    </PostClientFlow>
</ProxyEndpoint>