-
Notifications
You must be signed in to change notification settings - Fork 454
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: cluster key rate limit enhancement #1036
Conversation
@@ -51,6 +52,12 @@ limit_keys: | |||
query_per_second: 10 | |||
- key: a6a6d7f2-ba8a-11ec-bec2-00163e1250b5 | |||
query_per_minute: 100 | |||
# 正则表达式,以b开头的所有字符串 |
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.
这里会有歧义,是匹配到的所有统一限制1000,还是匹配到的每一个分别限制1000。
所以我建议增加新的配置项: limit_by_per_header, limit_by_per_param
用来表示匹配到的每一个分别限制这一含义。
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.
这里会有歧义,是匹配到的所有统一限制1000,还是匹配到的每一个分别限制1000。
所以我建议增加新的配置项: limit_by_per_header, limit_by_per_param
用来表示匹配到的每一个分别限制这一含义。
好的,那应该就是limit_by_header和limit_by_param只支持精确匹配的,limit_by_per_header和limit_by_per_param只支持正则匹配和*的方式
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.
可以的
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.
可以的
已调整,目前支持以下几种方式:
limit_by_header、limit_by_param、limit_by_consumer、limit_by_cookie(匹配cookie中的key=value) 支持精准匹配
limit_by_per_ip 支持按照ip和ip段配置
limit_by_peer_header、limit_by_peer_param、limit_by_peer_consumer、limit_by_per_cookie 支持正则匹配和*
| redis | object | 是 | - | redis相关配置 | | ||
| 配置项 | 类型 | 必填 | 默认值 | 说明 | | ||
| ----------------------- | ------ | ---- | ------ |-----------------------------------------------------------------------------------------------------------------------------| | ||
| rule_name | string | 是 | - | 限流规则名称,根据限流类型+限流规则名称+限流key对应的实际值来拼装redis key | |
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.
支持下配置多个rule可以同时生效吧,现有的配置作为rules数组里的配置项
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.
支持下配置多个rule可以同时生效吧,现有的配置作为rules数组里的配置项
将limit_by_*(共9种)和limit_keys调整到了rule_items中,拼接redis的key的方式调整为限流规则名称+限流类型+限流key名称+限流key对应的实际值,这样只需要外部配置一个rule_name即可
e3aae23
to
7a9d6e3
Compare
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.
LGTM
Ⅰ. Describe what this PR did
cluster key rate limit enhancement
1)add limit_type: limit_by_consumer、limit_by_cookie、limit_by_per_header、limit_by_per_param、limit_by_per_consumer、limit_by_per_cookie
2)support batch configuration of rate limiting rules
Ⅱ. Does this pull request fix one issue?
Ⅲ. Why don't you add test cases (unit test/integration test)?
Ⅳ. Describe how to verify it
1)、limit_by_param和limit_by_per_param
wasmplugin.yam:
1)根据第一个apikey进行限流
一分钟内请求三次:
响应头中x-ratelimit-limit为10(限制的总请求数),x-ratelimit-remaining为7(剩余还可以发送的请求数)
2)根据第二个apikey限流
请求三次:
Redis中的数据:
![根据第二个apikey限流Redis中的数据](https://private-user-images.githubusercontent.com/45159482/339999845-de325792-5efe-4dce-88fe-d9966b4bafef.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1ODE3NzMsIm5iZiI6MTcyNDU4MTQ3MywicGF0aCI6Ii80NTE1OTQ4Mi8zMzk5OTk4NDUtZGUzMjU3OTItNWVmZS00ZGNlLTg4ZmUtZDk5NjZiNGJhZmVmLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDEwMjQzM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTNhOTdjMzdlMjIzMTM3YzBiYTlhNjIzODliNTNmODZmOGE0OGQxOTdkYzE0MjhhOGRiNjQ3NWQzNjM4ZTRiNWUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.OBZlUcS1OGSP68skc_frbdJPIyBf5ZpkYlA7afgmYHc)
3)正则表达式以b开头的字符串
一分钟内请求三次:
响应头中x-ratelimit-limit为100(限制的总请求数),x-ratelimit-remaining为97(剩余还可以发送的请求数)s
4)*
请求四次:
Redis中的数据:
![All请求三次Redis中的数据](https://private-user-images.githubusercontent.com/45159482/339999895-6010bde2-7b2d-4088-b812-3b90fd0f4a37.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1ODE3NzMsIm5iZiI6MTcyNDU4MTQ3MywicGF0aCI6Ii80NTE1OTQ4Mi8zMzk5OTk4OTUtNjAxMGJkZTItN2IyZC00MDg4LWI4MTItM2I5MGZkMGY0YTM3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDEwMjQzM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWYzZWQ2MGRlMjFlN2E0MTM4NDFjYTBhNjRiNGJjMzBjYWUzZTljOTM5Zjc2NzRhODE2MTNiNzk2YTZhM2EwOGEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.AUbPqTKkYaltobgVYdSJwNYmiGs0JfhUbpw8kjOstyw)
2)、limit_by_header和limit_by_per_header
wasmplugin.yam:
1)根据第一个请求头进行限流
一分钟内请求三次:
响应头中x-ratelimit-limit为10(限制的总请求数),x-ratelimit-remaining为7(剩余还可以发送的请求数)
2)根据第二个请求头进行限流
请求三次:
Redis中的数据:
3)正则表达式以b开头的字符串
一分钟内请求三次:
响应头中x-ratelimit-limit为100(限制的总请求数),x-ratelimit-remaining为97(剩余还可以发送的请求数)s
4)*
请求三次:
Redis中的数据:
3)、limit_by_cookie和limit_by_per_cookie
wasmplugin.yam:
1)根据第一个cookie进行限流
一分钟内请求三次:
响应头中x-ratelimit-limit为10(限制的总请求数),x-ratelimit-remaining为7(剩余还可以发送的请求数)
触发限流后:
2)根据第二个cookie限流
请求三次:
Redis中的数据:
3)正则表达式以b开头的字符串
一分钟内请求三次:
响应头中x-ratelimit-limit为100(限制的总请求数),x-ratelimit-remaining为97(剩余还可以发送的请求数)
4)*
请求三次:
Redis中的数据:
4)、limit_by_per_ip
wasmplugin.yam:
1)1.1.1.1
连续请求三次:
响应头中x-ratelimit-limit为10(限制的总请求数),x-ratelimit-remaining为7(剩余还可以发送的请求数)
2)1.1.1.0/24
请求三次:
Redis中的数据:
![ip段请求三次Redis中数据](https://private-user-images.githubusercontent.com/45159482/340000154-ef89e393-deca-4f6f-8e00-48cb88da4d57.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1ODE3NzMsIm5iZiI6MTcyNDU4MTQ3MywicGF0aCI6Ii80NTE1OTQ4Mi8zNDAwMDAxNTQtZWY4OWUzOTMtZGVjYS00ZjZmLThlMDAtNDhjYjg4ZGE0ZDU3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDEwMjQzM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTdiZmEyZGNhMWNlY2ZlN2I5ZTQ0ZTFlZGVmOTdmNTIyNWI5MjM4MTJjMzcxNWZhMjY4NGYwNjQ2ZTA4MTgxMmUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.2hw11aCFCBYdnmomxgLeJ25AWAZh1KkLTFE9w1iKwr8)
3)0.0.0.0/0
请求三次:
Redis中的数据:
![all请求三次Redis中数据](https://private-user-images.githubusercontent.com/45159482/340000201-8383bc1a-3a43-4d63-8800-022e9c78a72c.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1ODE3NzMsIm5iZiI6MTcyNDU4MTQ3MywicGF0aCI6Ii80NTE1OTQ4Mi8zNDAwMDAyMDEtODM4M2JjMWEtM2E0My00ZDYzLTg4MDAtMDIyZTljNzhhNzJjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDEwMjQzM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTg5ZWYzZGE0MTI4ZTllNGMyODA4MDYyNmNhOWY4YmVlN2QzMzdmYTNhYjk4MGQ5YzhlMzIwMGM0MGU3MTEyZDAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.RQx3GDwHfDYmpIhUgOZIGricGYml90X-7H2wb46hl4E)
4)、limit_by_consumer和limit_by_per_consumer
![image](https://private-user-images.githubusercontent.com/45159482/340000431-bbbd7a13-af91-4a5b-8443-50d2b74e1067.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ1ODE3NzMsIm5iZiI6MTcyNDU4MTQ3MywicGF0aCI6Ii80NTE1OTQ4Mi8zNDAwMDA0MzEtYmJiZDdhMTMtYWY5MS00YTViLTg0NDMtNTBkMmI3NGUxMDY3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MjUlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODI1VDEwMjQzM1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTc2YWZlOGNhMzZiN2VjMTU5YjhiZDMwMDU0ZjdiNGEzNDRlOWQwYmNiOTMwOGM4ZmU0YmMxOGUxNDIyNDg4Y2EmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.BwyJcy7ZUrFxFq5fmX4lD1KEgnVhs3rf9n5Jc2rgOgM)
Redis中key示例:
Ⅴ. Special notes for reviews