Hey!
I wonder if there is a possibility to setup quota policy for developer's app based on my backend service response. I would like to check if my backend returned message with status 200 (ok), and only then increase call limit counter. I do not want to charge my client when my backend is shut down or returned any kind of error.
I also want to express my thankfulness for such fast and professional answers. Thanks!
Cheers, Tomas
Solved! Go to Solution.
Dear @Tomasz Korecki ,
Yes, it's possible.
Cheers,
Anil Sagar
Refer your successful transaction uniquely with a flag & apply condition to Quota policy
Ex - For every 200 OK, set a variable - backend.response="ok" or "true". Use this flag as condition while applying Quota policy.
@Abhishek Subramanya , It's more about either executing the policy or not. Identifier is used only to uniquely identify the quota policy. Please check my answer below for more details.
@Anil Sagar,
Thanks for pointing it out. I have updated my answer
Dear @Tomasz Korecki ,
Yes, it's possible.
Cheers,
Anil Sagar
@Anil Sagar I've added new custom quota policy. And it is called in proxy endpoint request flow. Everything works fine.
However, in my case I need something like a quota identifier increasing. I want to prevent calling my backend using quota policy, so it needs to be placed in the proxy endpoint flow. But if quota is not exceeded, request goes to backend and after response from my backend, I would like to check response code value and conditionally increase quota allow by 1.
I probably could use Reset quota policy.I figured out that I need to retrieve actual available count for policy and increment it by 1. I made such steps:
1) I've added javascript file for quota allow count retrieve:
var quotaAllowCount = context.getVariable("ratelimit.myQuotaPolicyName.available.count"); context.setVariable("newMyQuotaPolicyAllowCount", quotaAllowCount + 1)
I've placed it in the target endpoint response postflow with proper conditionals.
2) I've created new Reset quota policy, but I do not know how to insert new quota allow count to Allow xml element value. Code below does not compile:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ResetQuota async="false" continueOnError="false" enabled="true" name="ResetMyQuotaPolicyName"> <DisplayName>ResetMyQuotaPolicyName</DisplayName> <Properties/> <Quota name="MyQuotaPolicyName"> <Identifier name="client_id" ref="client_id"> <Allow>"newMyQuotaPolicyAllowCount"</Allow> </Identifier> </Quota> </ResetQuota>
Am I doing this thing in the right way? How to setup Allow element value from variable?
Cheers, Tomas
Oh... I can type:
<Allow ref="newMyQuotaPolicyAllowCount"></Allow>
I put my policy for calling javascript file in the target endpoint postflow response:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <TargetEndpoint name="default"> <Description/> <PreFlow name="PreFlow"> <Request/> <Response/> </PreFlow> <Flows></Flows> <PostFlow name="PostFlow"> <Response> <Step> <Name>JS1CallPer1HourQuotaAdder</Name> </Step> </Response> </PostFlow> <HTTPTargetConnection> <URL>http://demo3662224.mockable.io</URL> </HTTPTargetConnection> </TargetEndpoint>However, I saw that when response from backend is 404, javascript file is not called. Whole flow from backend to client app is passed. I want to put there a condition for checking a response status code:
<Condition>(response.status.code != 200)</Condition>
My plan is to put there a policy for resetting quota call rate when this condition will result in true. However, first thing after response is an error:
Received non success response code
After that, whole flow is passed. How could I put there any kind of conditions in the case of 404 response status code from my backend?
@Tomasz Korecki , Its as per design. You can override same using propery success codes in http connection settings.
<HTTPTargetConnection> <Properties> <Property name="success.codes">1xx,2xx,3xx,400,500</Property> </Properties> <URL>http://weather.yahooapis.com</URL> </HTTPTargetConnection>
Find more about same here.
Right now it looks like that:
1) I have got Javascript file which is called when my backend response with error:
var quotaAllowCount = context.getVariable("ratelimit.2CallPer1MinuteQuota.allowed.count"); var newAllowCount = quotaAllowCount + 1; context.setVariable("new2CallPer1MinuteAllowCount", newAllowCount.toString());
2) 2CallPer1MinuteQuota looks like that:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Quota async="false" continueOnError="false" enabled="true" name="2CallPer1MinuteQuota" type="rollingwindow"> <DisplayName>2CallPer1MinuteQuota</DisplayName> <Properties/> <Allow count="2"/> <Interval>1</Interval> <Identifier ref="client_id"/> <Distributed>true</Distributed> <Synchronous>true</Synchronous> <TimeUnit>minute</TimeUnit> <PreciseAtSecondsLevel>true</PreciseAtSecondsLevel> </Quota>3) In the case of backend error I finally run Reset policy:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ResetQuota async="false" continueOnError="false" enabled="true" name="Reset2CallPer1MinuteQuota"> <DisplayName>Reset2CallPer1MinuteQuota</DisplayName> <Properties/> <Quota name="2CallPer1MinuteQuota"> <Identifier name="client_id" ref="client_id"> <Allow ref="new2CallPer1MinuteAllowCount"/> </Identifier> </Quota> </ResetQuota>
I am turning off my backend and sending next requests. I've figured out that in such solution I am incrementing my ratelimit.2CallPer1MinuteQuota.available.count on each backend response. So e.x. after 4th request, client has got 7 available calls, instead of 2.
It looks like resetting policy adds to available count variable, an allowed.count value. Am I right?
What I want to achieve is not really the resetting policy. It's more about incrementing available counter by 1, but to not exceed allowed counter.
Have you got any ideas?
Ok, I've made it too complicated... I removed this javascript file and setup value Reset policy allow element to 1. And it works!
User | Count |
---|---|
2 | |
1 | |
1 | |
1 | |
1 |