Counter for a proxy request

Not applicable

Hi all,

I need a counter to know the request number for a proxy in a day. If it's 10th request then I want to send the value 10 to backend in header.

Now I am using a dummy quota policy in which I have given counter a very big value.

But the quota policy is taking a very long time in this.

Is there any other inbuilt variable or counter which I can use ?

0 2 179
2 REPLIES 2

You could achieve this using a Quota with continueOnError="true" and a Condition to set the target header if the Quota policy failed

<Quota continueOnError="true" enabled="true" name="QU-ByProxyDayInterval" type="calendar">
    <DisplayName>QU-ByProxyDayInterval</DisplayName>
    <Allow count="10" countRef="request.header.quota_allowed"/>
    <Interval ref="request.header.quota_count">10</Interval>
    <TimeUnit ref="request.header.quota_timeout">day</TimeUnit>
    <Identifier ref="quota_interval_id"/>
    <Distributed>true</Distributed>
    <Synchronous>true</Synchronous>
    <StartTime>2021-01-01 00:00:01</StartTime>
</Quota>

The Conditional Flow

<Step>
    <Condition>ratelimit.QU-ByProxyDayInterval.failed == true</Condition>
    <Name>AM-ByProxyTargetHeader</Name>
</Step>

If you wanted to have this sent every "interval" (e.g. 10) times, you could add a Reset Quota policy.

<ResetQuota name="QR-QuotaByProxyDay">
    <DisplayName>QR-QuotaByProxyDay</DisplayName>
    <Properties/>
    <Quota name="QU-ByProxyDayInterval">
        <Identifier ref="quota_interval_id">
            <Allow ref="request.header.reset_quota">10</Allow>
        </Identifier>
    </Quota>
</ResetQuota>

Using the same condition

<Step>
    <Condition>ratelimit.QU-ByProxyDayInterval.failed == true</Condition>
    <Name>QR-QuotaByProxyDay</Name>
</Step>

The Reset Quota

<ResetQuota name="QR-QuotaByProxyDay">
    <DisplayName>QR-QuotaByProxyDay</DisplayName>
    <Properties/>
    <Quota name="QU-ByProxyDayInterval">
        <Identifier ref="quota_interval_id">
            <Allow ref="request.header.reset_quota">10</Allow>
        </Identifier>
    </Quota>
</ResetQuota>

Finally, if you also wanted to set the total used count for the day, you could use another all-day quota counter.

<Quota name="QU-ByProxyDayAll" type="calendar">
    <DisplayName>QU-ByProxyDayAll</DisplayName>
    <Allow count="100000000" countRef="request.header.quota_allowed"/>
    <Interval ref="request.header.quota_count">10</Interval>
    <TimeUnit ref="request.header.quota_timeout">day</TimeUnit>
    <Identifier ref="apiproxy.name"/>
    <Distributed>true</Distributed>
    <Synchronous>true</Synchronous>
    <StartTime>2021-01-01 00:00:01</StartTime>
</Quota>

I did not notice any latency when using a large allowed count.

If you don't like playing the Quota puzzle game, you could just use a KVM or Cache.

Not applicable

As I have mentioned, I am currently using the quota. I don't want to use kvm and cache is something which will get cleared after restart.

Is there any inbuilt counter ? I am jsut looking for something which is already there as a variable and I will be able to use inside javascript.