Skip to content

adding example policies for tpm and circuit breaker #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ Overview
| <a href="Extracting multiple values from xml documents.policy.xml">Extract multiple values from xml documents</a> | Uses an intermediate json object to provide easy access to extracted values from an xml document. |
| <a href="Return a blob URL signed with a user delegation SAS token.xml">Return a blob URL signed with a user delegation SAS token</a> | Demonstrates how to check if a blob exists and then return the URL of the blob signed with a User Delegation SAS token leveraging Managed Identity. |
| <a href="oauth-proxy/readme.md">OAuth Reverse Proxy</a> | Demonstrates how to implement an OAuth Reverse Proxy to provide OIDC authentication in-front of a Web Application behind Azure API Management. |
| <a href="Rate limit by TPM.xml">Rate limit by TPM (Tokens per minute)</a> | Demonstrates how to implement a TPM based rate limiting policy in Azure API Management. |
30 changes: 30 additions & 0 deletions examples/Rate limit by TPM.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

<policies>
<inbound>
<base />
<!--Priority Management Based on Subscription Keys:
Applies quotas based on the subscription key. -->
<choose>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason why we don't use set-variable to define the allowed calls and then only define rate-limit-by-key once?

<when condition="@(context.Subscription.Id == "subscription1")">
<!-- Rate limit by TPM
Each subscription has its own TPM counter ('subscription1TPM' in this case) that tracks total tokens used per request.
If the response status is 200, the counter increases by the token count obtained in the response body.
If the counter hits the 500 TPM limit within 60 seconds, the request will not be forwarded to the backend.
Clients recieve 429 as the response status code.
-->
<rate-limit-by-key calls="500" renewal-period="60" counter-key="@(String.Concat(context.Subscription.Id,"TPM"))" increment-condition="@(context.Response.StatusCode >= 200 && context.Response.StatusCode < 400)" increment-count="@(context.Response.Body.As<JObject>(true).SelectToken("usage.total_tokens").ToObject<int>())" remaining-calls-header-name="remainingTPM" total-calls-header-name="totalTPM" />
</when>
<when condition="@(context.Subscription.Name == "subscription2")">
<!-- Rate limit by TPM -->
<rate-limit-by-key calls="200" renewal-period="60" counter-key="@(String.Concat(context.Subscription.Id,"TPM"))" increment-condition="@(context.Response.StatusCode >= 200 && context.Response.StatusCode < 400)" increment-count="@(context.Response.Body.As<JObject>(true).SelectToken("usage.total_tokens").ToObject<int>())" remaining-calls-header-name="remainingTPM" total-calls-header-name="totalTPM" />
</when>
<otherwise>
<rate-limit-by-key calls="5" renewal-period="60" counter-key="@(context.Subscription.Id)" />
</otherwise>
</choose>
</inbound>
<backend>
<!--The backend section inherits the base behavior without additional policies.-->
<base />
</backend>
</policies>