In this scenario, the application of API Based Quota policy to the Mock API created on Apinizer will be tested.

A quota policy limits the total number of requests that can be made in a given time period. This policy is often used to provide longer term usage control (for example, allowing 1000 requests to a specific user in a day or a month).

In the scenario, we can limit incoming requests to 100 requests in a 1 day period.


Create a mock api instance and add the policy.


We add API Based Quota as a policy.


In the scenario, let's limit the incoming requests to 100 requests for 1 day using the Fixed Window method.

  • If the period is set to 1 and the day is selected:
  • For example, if the policy is saved at 12:00:00:00, it will be valid from 00:00:00 to 23:59:59 on the current day.
  • The next day at 00:00:00 a new window will start and the counter will be reset.



FieldDescription
Message CountThe number of requests allowed. In this scenario we set it to 100.
Interval Time AmountIt is the period determined according to the selected time unit. In this scenario, we set it as 1.
Interval Time UnitUnit of time.  In this scenario we set it as a day.
Specific quota limits are defined for each target.
Interval Window TypeMethod used to implement the time interval. Time periods of 1 day of type Fixed.
Cache Connection Timeout (Second)Specify the connection timeout value for the connection between the policy and the cache.

If there is a connection problem with the cache, return an error (select Fail ) or continue without applying the request constraint (select Continue ).


When we make a sample request from the test console, the message is blocked because the constraint limit is reached after the 100th request in the time range 00:00:00 and 23:59:59 on the current day.

Target-Specific Quota Rules

With this feature, you can define custom quota limits for specific users or targets.

We add API Based Quota as a policy.

  • By using Target-Specific Quota Rules, special quota limits can be set based on user (header, body or any other field). In the general policy, incoming requests can be limited to 100 requests for 1 day with the Fixed Window method, while at the same time a maximum limit of 5 requests per hour can be defined for user1.
  • In the Fixed Window method, if the period is set to 1 and the day is selected:
  • For example, if the policy is saved at 12:00:00 am, this policy will be valid from 00:00:00 am to 23:59:59 pm on the current day and 100 requests can be made during this period as a general rule.
  • The next day at 00:00:00:00, a new window will start and the counter will be reset.
  • For user1, which we defined with Target-Specific Quota Rules, an hourly limit is set. If a time zone starts at 00:00:00, this covers the period between 00:00:00 and 00:59:59 and user1 can make a maximum of 5 requests in this time interval. At the next time zone (01:00:00:00) the counter is reset.

When we send a sample request from the test console, when we send a request with user1 from the username header, the message was blocked because the restriction limit was reached after the 5th request in the time range 00:00:00 to 00:59:59 at the current time.