The key-rate-limit plug-in implements throttling based on specific key values. Key values can be obtained from URL parameters or HTTP request headers. This topic describes how to configure the key-rate-limit plug-in.
Plug-in type
Throttling plug-in.
Fields
Field | Data type | Required | Default value | Description |
limit_by_header | string | No You must configure limit_by_header or limit_by_param. | - | The HTTP request header from which the key value for throttling is obtained. |
limit_by_param | string | No You must configure limit_by_header or limit_by_param. | - | The URL parameter from which the key value for throttling is obtained. |
limit_keys | array of object | Yes | - | The number of times rate limiting is triggered after a specific key value is matched. |
The following table describes the configuration items in the limit_keys field.
Configuration item | Data type | Required | Default value | Description |
key | string | Yes | - | The key value that is used to match requests. |
query_per_second | number | No You must configure query_per_second, query_per_minute, query_per_hour, or query_per_day. | - | The number of requests that are allowed per second. |
query_per_minute | number | No You must configure query_per_second, query_per_minute, query_per_hour, or query_per_day. | - | The number of requests that are allowed per minute. |
query_per_hour | number | No You must configure query_per_second, query_per_minute, query_per_hour, or query_per_day. | - | The number of requests that are allowed per hour. |
query_per_day | number | No You must configure query_per_second, query_per_minute, query_per_hour, or query_per_day. | - | The number of requests that are allowed per day. |
Examples
Perform throttling based on the key value from the request header x-ca-key.
limit_by_header: x-ca-key limit_keys: - key: 102234 query_per_second: 10 - key: 308239 query_per_hour: 10Perform throttling based on the key value from the request parameter apikey.
limit_by_param: apikey limit_keys: - key: 9a342114-ba8a-11ec-b1bf-00163e1250b5 query_per_second: 10 - key: a6a6d7f2-ba8a-11ec-bec2-00163e1250b5 query_per_minute: 100Enable throttling for routes or domain names.
Apply the following plug-in configurations to the
route-aandroute-broutes:limit_by_header: x-ca-key limit_keys: - key: 102234 query_per_second: 10Apply the following plug-in configuration to the
*.example.comandtest.comdomain names:limit_by_header: x-ca-key limit_keys: - key: 102234 query_per_second: 100NoteIn this example, the
route-aandroute-broutes are those specified when the gateway routes are created. If a client request matches one of the routes, the rules that are configured for the matched route take effect.In this example, the
*.example.comandtest.comdomain names are used to match domain names in requests. If a client request matches one of the domain names, the rules that are configured for the matched domain name take effect.Rules that you configure take effect in sequence. If the first rule is matched, subsequent rules are ignored.