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:
- Open the API7 Cloud console.
- From the left navigation bar, choose API Management, then select Services from the secondary menu.
- Click on the HTTPBIN Service name, enter the Service details page.
- Click on Add Plugin, select the Limit Count plugin.
- 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"}
.
- Set Count to
In this case, we configure:
- A gateway instance only accepts five requests in a minute (for the JSON route);
- 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.
Apache APISIX wraps the error message in a JSON string.
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.