none
Consulting rate-limit-by-key policy of Azure API Management

    Question

  • Our requirement is to extract customer id from API request, then use it as key to control call rate.

    • If no customer id in the request, don’t control it. (Couldn’t find how to configure it in Azure.)
    • If request contains customer id, 
    1. Set different rate limit to some customer ids. (Couldn’t find how to configure in Azure. Particularly if two rate-limit-by-key policy, which one will take effect?)
    2. If not set rate limit in previous step, use a constant rate limit. (It supported by Azure as below policy)

    For example, there are A, B, C, D customer ids, we want to set 20 calls/sec to A, set 30 calls/sec to B, and to others set 10 calls/sec as limit.

     

    Any ideas or workaround on this concern?

    Thanks!

    <rate-limit-by-key calls="100"

              renewal-period="60"

              counter-key="@( string inBody = context.Request.Body.As<string>(preserveContent: true);  

        string pattern = @"<h:CustomerId (.*?)>(.*?)</h:CustomerId>";

        Regex rgx = new Regex(pattern);

        var match = rgx.Match(inBody);

        if (match.Success)

            { return match.Groups[2].Value.Trim();  }

        else

            {return "";   })

    "/>

     

     



    Friday, October 13, 2017 10:03 AM

Answers

  • You can use “control flow” (choose) policy to implement this:

    <choose>  
    
        <when condition="Boolean
    expression | Boolean constant">  
    
            <!— one or more policy statements to be applied if the above condition is true  -->  
    
        </when> 
    
        <when condition="Boolean
    expression | Boolean constant">  
    
            <!— one or more policy statements to be applied if the above condition is true  -->  
    
        </when> 
    
        <otherwise> 
    
            <!— one or more policy statements to be applied if none of the above conditions are true  -->  
    
    </otherwise>  
    
    </choose>

    So you can define several policies but only one of them will be executed for the request.

    -----------------------------------------------------------------------------------------------------

    Do click on "Mark as Answer" on the post that helps you, this can be beneficial to other community members.

    Friday, October 13, 2017 4:54 PM
    Moderator