limit-conn

Description

The limit-con Plugin limits the number of concurrent requests to your services.

Attributes

NameTypeRequiredDefaultValid valuesDescription
connintegerTrueconn > 0Maximum number of concurrent requests allowed. Requests exceeding this ratio (and below conn + burst) will be delayed (configured by default_conn_delay).
burstintegerTrueburst >= 0Number of additional concurrent requests allowed to be delayed per second. If the number exceeds this hard limit, they will get rejected immediately.
default_conn_delaynumberTruedefault_conn_delay > 0Delay in seconds to process the concurrent requests exceeding conn (and conn + burst).
only_use_default_delaybooleanFalsefalse[true,false]When set to true, the Plugin will always set a delay of default_conn_delay and would not use any other calculations.
key_typestringFalse“var”[“var”, “var_combination”]Type of user specified key to use.
keystringTrueUser specified key to base the request limiting on. If the key_type attribute is set to var, the key will be treated as a name of variable, like remote_addr or consumer_name. If the key_type is set to var_combination, the key will be a combination of variables, like $remote_addr $consumer_name. If the value of the key is empty, remote_addr will be set as the default key.
rejected_codestringFalse503[200,…,599]HTTP status code returned when the requests exceeding the threshold are rejected.
rejected_msgstringFalsenon-emptyBody of the response returned when the requests exceeding the threshold are rejected.
allow_degradationbooleanFalsefalseWhen set to true enables Plugin degradation when the Plugin is temporarily unavailable and allows requests to continue.

Enabling the Plugin

You can enable the Plugin on a Route as shown below:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/index.html",
  5. "plugins": {
  6. "limit-conn": {
  7. "conn": 1,
  8. "burst": 0,
  9. "default_conn_delay": 0.1,
  10. "rejected_code": 503,
  11. "key_type": "var",
  12. "key": "http_a"
  13. }
  14. },
  15. "upstream": {
  16. "type": "roundrobin",
  17. "nodes": {
  18. "127.0.0.1:1980": 1
  19. }
  20. }
  21. }'

You can also configure the key_type to var_combination as shown:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/index.html",
  5. "plugins": {
  6. "limit-conn": {
  7. "conn": 1,
  8. "burst": 0,
  9. "default_conn_delay": 0.1,
  10. "rejected_code": 503,
  11. "key_type": "var_combination",
  12. "key": "$consumer_name $remote_addr"
  13. }
  14. },
  15. "upstream": {
  16. "type": "roundrobin",
  17. "nodes": {
  18. "127.0.0.1:1980": 1
  19. }
  20. }
  21. }'

Example usage

The example above configures the Plugin to only allow one concurrent request. When more than one request is received, the Plugin will respond with a 503 status code:

  1. curl -i http://127.0.0.1:9080/index.html?sleep=20 &
  2. curl -i http://127.0.0.1:9080/index.html?sleep=20
  1. <html>
  2. <head><title>503 Service Temporarily Unavailable</title></head>
  3. <body>
  4. <center><h1>503 Service Temporarily Unavailable</h1></center>
  5. <hr><center>openresty</center>
  6. </body>
  7. </html>

Disable Plugin

To disable the limit-conn Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "methods": ["GET"],
  4. "uri": "/index.html",
  5. "id": 1,
  6. "plugins": {
  7. },
  8. "upstream": {
  9. "type": "roundrobin",
  10. "nodes": {
  11. "127.0.0.1:1980": 1
  12. }
  13. }
  14. }'