Skip to content

Instantly share code, notes, and snippets.

@tomkerkhove
Last active August 9, 2023 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomkerkhove/cb2b5f42725bbd338f56b3c5c26424f9 to your computer and use it in GitHub Desktop.
Save tomkerkhove/cb2b5f42725bbd338f56b3c5c26424f9 to your computer and use it in GitHub Desktop.
Throttling by product, but applied on operation-level
<!--
IMPORTANT:
- Policy fragment are included as-is whenever they are referenced.
- If using variables. Ensure they are setup before use.
- Copy and paste your code here or simply start coding
-->
<fragment>
<rate-limit-by-key calls="@(int.Parse((string)context.Variables["amountOfCalls"]))" renewal-period="@(int.Parse((string)context.Variables["renewalPeriod"]))" counter-key="@(context.Subscription.Id)" remaining-calls-variable-name="remainingCallsPerSubscription" />
</fragment>
<policies>
<inbound>
<base />
<include-fragment fragment-id="operation-level-rate-limiting" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<set-header name="X-RateLimit-Reset" exists-action="override">
<value>@(((int)context.Variables["remainingCallsPerSubscription"]).ToString())</value>
</set-header>
</outbound>
<on-error>
<base />
</on-error>
</policies>
<policies>
<inbound>
<set-variable name="amountOfCalls" value="10" />
<set-variable name="renewalPeriod" value="60" />
<base />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<set-header name="X-RateLimit-Renewal" exists-action="override">
<value>@((string)context.Variables["renewalPeriod"])</value>
</set-header>
<set-header name="X-RateLimit-Amount" exists-action="override">
<value>@((string)context.Variables["amountOfCalls"])</value>
</set-header>
</outbound>
<on-error>
<base />
</on-error>
</policies>
<policies>
<inbound>
<base />
<rate-limit-by-key calls="@(int.Parse((string)context.Variables["amountOfCalls"]))" renewal-period="@(int.Parse((string)context.Variables["renewalPeriod"]))" counter-key="@(context.Subscription.Id)" remaining-calls-variable-name="remainingCallsPerSubscription" />
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
<set-header name="X-RateLimit-Reset" exists-action="override">
<value>@(((int)context.Variables["remainingCallsPerSubscription"]).ToString())</value>
</set-header>
</outbound>
<on-error>
<base />
</on-error>
</policies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment