-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(limit-count): add constant key type #5984
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
94f515c
feat(limit-count): add constant key type
spacewander c8b8703
Update docs/zh/latest/plugins/limit-count.md
spacewander e2aa7c3
add desc for constant key_type
spacewander ce1736a
Update docs/en/latest/plugins/limit-count.md
spacewander 8ea0cc7
up
spacewander File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ Limit request rate by a fixed number of requests in a given time window. | |
| ------------------- | ------- | --------------------------------------- | ------------- | ------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | ||
| count | integer | required | | count > 0 | the specified number of requests threshold. | | ||
| time_window | integer | required | | time_window > 0 | the time window in seconds before the request count is reset. | | ||
| key_type | string | optional | "var" | ["var", "var_combination"] | the type of key. | | ||
| key_type | string | optional | "var" | ["var", "var_combination", "constant"] | the type of key. | | ||
| key | string | optional | "remote_addr" | | the user specified key to limit the rate. If the `key_type` is "var", the key will be treated as a name of variable. If the `key_type` is "var_combination", the key will be a combination of variables. For example, if we use "$remote_addr $consumer_name" as keys, plugin will be restricted by two keys which are "remote_addr" and "consumer_name". If the value of the key is empty, `remote_addr` will be set as the default key.| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible, can we add a line here[L43] to document how the key will be interpreted when the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in the new commit |
||
| rejected_code | integer | optional | 503 | [200,...,599] | The HTTP status code returned when the request exceeds the threshold is rejected, default 503. | | ||
| rejected_msg | string | optional | | non-empty | The response body returned when the request exceeds the threshold is rejected. | | ||
|
@@ -110,7 +110,7 @@ You also can complete the above operation through the web interface, first add a | |
|
||
It is possible to share the same limit counter across different routes. For example, | ||
|
||
``` | ||
```shell | ||
curl -i http://127.0.0.1:9080/apisix/admin/services/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' | ||
{ | ||
"plugins": { | ||
|
@@ -133,7 +133,7 @@ curl -i http://127.0.0.1:9080/apisix/admin/services/1 -H 'X-API-KEY: edd1c9f0343 | |
|
||
Every route which group name is "services_1#1640140620" will share the same count limitation `1` in one minute per remote_addr. | ||
|
||
``` | ||
```shell | ||
$ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' | ||
{ | ||
"service_id": "1", | ||
|
@@ -156,6 +156,36 @@ HTTP/1.1 503 ... | |
Note that every limit-count configuration of the same group must be the same. | ||
Therefore, once update the configuration, we also need to update the group name. | ||
|
||
It is also possible to share the same limit counter in all requests. For example, | ||
|
||
```shell | ||
curl -i http://127.0.0.1:9080/apisix/admin/services/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d ' | ||
{ | ||
"plugins": { | ||
"limit-count": { | ||
"count": 1, | ||
"time_window": 60, | ||
"rejected_code": 503, | ||
"key": "remote_addr", | ||
"key_type": "constant", | ||
"group": "services_1#1640140621" | ||
} | ||
}, | ||
"upstream": { | ||
"type": "roundrobin", | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
} | ||
} | ||
}' | ||
``` | ||
|
||
Compared with the previous configuration, we set the `key_type` to `constant`. | ||
By setting `key_type` to `constant`, we don't evaluate the value of `key` but treat it as a constant. | ||
|
||
Now every route which group name is "services_1#1640140621" will share the same count limitation `1` in one minute among all the requests, | ||
even these requests are from different remote_addr. | ||
|
||
If you need a cluster-level precision traffic limit, then we can do it with the redis server. The rate limit of the traffic will be shared between different APISIX nodes to limit the rate of cluster traffic. | ||
|
||
Here is the example if we use single `redis` policy: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we should add a note here to alert those who use the key to monitoring or something else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you give an example for the note?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Err...
I don't want to comment on all the changes in the code, unless it is something that affects public API. AFAIK, we don't describe the key format in the doc. So I think such change is just implementation details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make Sense, leave the comment here should do the trick as well.