简体   繁体   中英

How to disable rate limit policy based on the azure subscription in Azure APIM

I have a use case to use single policy.xml for different environments however the rate-limit is applicable only for certain environment.

For eg:

Dev: rate-limit is applicable (hosted in dev azure subscription)

QA: rate-limit is not applicable (hosted in test azure subscription)

Prod: rate-limit is applicable (hosted in prod azure subscription)

Update: Tried this from one of the posts here post :

    <choose>
        <when condition="@(context.Subscription.Name=="abcd")">
            <rate-limit-by-key calls="1" renewal-period="15" counter-key="@(context.Subscription.Id)" />
        </when>
        <when condition="@(context.Subscription.Name=="efgh")">
            <rate-limit-by-key calls="2" renewal-period="15" counter-key="@(context.Subscription.Id)" />
        </when>
        <otherwise />
    </choose>

Below snippet is from the inbound request and what i don't understand is the value of the first when element condition attribute is false even though it is executed from the "abcd" subscription.

choose (7.565 ms)
    {
    "message": "Expression was successfully evaluated.",
    "expression": "context.Subscription.Name==\"abcd\"",
    "value": false
}
choose (0.251 ms)
    {
    "message": "Expression was successfully evaluated.",
    "expression": "context.Subscription.Name==\"efgh\"",
    "value": false
} 

Solution that worked for me from below approaches:

<choose>
    <when condition="@(context.Request.Url.Host.Contains("dev"))">
        <rate-limit-by-key calls="1" renewal-period="5" counter-key="@(context.Subscription.Id)" />
    </when>
    <when condition="@(context.Request.OriginalUrl.Host.Contains("prod")">
        <rate-limit-by-key calls="2" renewal-period="10" counter-key="@(context.Subscription.Id)" />
    </when>
    <otherwise />
</choose>

1.The subscription key based approach is below.

You can define a subscription key on each of the environmen. In the below example i am creating a subscrption named dev on dev environment and prod on prod environment. You can check this link to understand how to create a subscription key. Once you creaete subscription keys on all three environment. You can add the following policy to your inbound policies.

<choose>
        <when condition="@(context.Subscription.Name=="dev")">
            <rate-limit-by-key calls="10000" renewal-period="15" counter-key="@(context.Subscription.Id)" />
        </when>
        <when condition="@(context.Subscription.Name=="prod")">
            <rate-limit-by-key calls="10000" renewal-period="15" counter-key="@(context.Subscription.Id)" />
        </when>
        <otherwise />
    </choose>

Once you done this next step is you need to make sure this subscription keys are being sent by the client. You can enable this on the settings.

If you don't want to create subscriptions and maintaining it. There is another way. Here the condition can be based on the name of the resource.

2. Second approach is pretty easy it is by referring any variables from context

Here you can refer a variable to distingush between your environment. like the below.

<choose>
        <when condition="@(context.Request.OriginalUrl.Host=="www.devenvironment.com")">
            <rate-limit-by-key calls="10000" renewal-period="15" counter-key="@(context.Subscription.Id)" />
        </when>
        <when condition="@(context.Request.OriginalUrl.Host=="www.prodenvironment.com")">
            <rate-limit-by-key calls="10000" renewal-period="15" counter-key="@(context.Subscription.Id)" />
        </when>
        <otherwise />
    </choose>

I think the second one is easy if you don't have any other constraints than the environments. You can actually refer any properties which is available on the context not only context.Request.OriginalUrl.Host

EDIT: About why example from this post is not excecuting as you expect is: context.Subscription.Name is not the subscription of your azure. It is the subscription you create under API manager, with which a client can access the APIs. I hope my answer describes with appropriate links

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM