Skip to main content

Attach the Limit Count Plugin

So far, you know how to configure service and route to let your APISIX instance run as your expectation. This section will extend the route with the Limit Count Plugin. Limit Count protects your route from being overwhelmed by a large number of requests.

To add the Limit Count plugin for HTTPBIN service. Do the following:

  1. Open the API7 Cloud console.
  2. From the left navigation bar, choose API Management, then select Services from the secondary menu.
  3. Click on the HTTPBIN Service name, enter the Service details page.
  4. Click on Add Plugin, select the Limit Count plugin.
  5. Fill the form:
    • Set Count to 5
    • Set Period to 60.
    • Set Rejected Status Code to 429.
    • Set Error Message to {"error_msg":"Too many requests"}.

In this case, we configure:

  1. A gateway instance only accepts five requests in a minute (for the JSON route);
  2. If the number of requests exceeds the limit, the gateway instance rejects the requests with the 429 status code, and the response body will be "Too many requests".

Now let's try to verify the Limit Count Plugin.

Again, we'll use curl for the verification. This time we'll send requests continuously.

for ((i=0; i<6; i++)); do
curl http://127.0.0.1:9080/v1/json -H 'Host: cloud.httpbin.org' -s -o/dev/null -w 'status code: %{http_code}\n'
done

The output will be:

status code: 200
status code: 200
status code: 200
status code: 200
status code: 200
status code: 429

As you can see, we sent 6 requests in a minute. The first 5 requests responded with a 200 status code, and the last one responded with the expected429, but what about the response body? Let's send a request separately through the below command.

curl http://127.0.0.1:9080/v1/json -H 'Host: cloud.httpbin.org' -s

You'll see {"error_msg":"Too many requests"} printed on the screen.

note

Apache APISIX wraps the error message in a JSON string.

info

The throttling quota might already reset when you run the above command. Try a few times if you don't see this output.

Congratulations, you've mastered using Limit Count plugin in your route.

Next

Summary.


API7.ai Logo

API Management for Modern Architectures with Edge, API Gateway, Kubernetes, and Service Mesh.

Product

API7 Cloud

SOC2 Type IIISO 27001HIPAAGDPRRed Herring

Copyright © APISEVEN Ltd. 2019 – 2024. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the

Apache Software Foundation