-
Notifications
You must be signed in to change notification settings - Fork 61
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: 限流-CPU使用率自适应限流 #186
Open
WTIFS
wants to merge
12
commits into
polarismesh:main
Choose a base branch
from
WTIFS:feat.cpu_limiter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: 限流-CPU使用率自适应限流 #186
Conversation
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
WTIFS
force-pushed
the
feat.cpu_limiter
branch
from
October 30, 2023 12:36
5f20492
to
6af0d02
Compare
chuntaojun
suggested changes
Nov 24, 2023
可以贴一个配置的图看下在北极星的配置吗 |
代码文件的 license header 需要加上 |
WTIFS
force-pushed
the
feat.cpu_limiter
branch
from
November 24, 2023 09:51
94beaf8
to
d7eb742
Compare
@chuntaojun polaris-console 的 PR:polarismesh/polaris-console#256 |
chuntaojun
reviewed
Dec 19, 2023
chuntaojun
reviewed
Dec 19, 2023
WTIFS
force-pushed
the
feat.cpu_limiter
branch
from
December 19, 2023 07:07
9711dee
to
d7eb742
Compare
daheige
approved these changes
Aug 12, 2024
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.
good idea!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Please provide issue(s) of this PR:
Fixes #165
To help us figure out who should review this PR, please put an X in all the areas that this PR affects.
Please check any characteristics that apply to this pull request.
BBR
BBR CPU 自适应限流组件参考了 TCP BBR 的思想,以及 阿里 Sentinel 的算法。
传统的限流思路为:超过一定负载就拦截流量进入,负载恢复就放开流量,这样做有延迟性,最终是按照果来调节因,无法取得良好效果。
BBR 的思路为:根据应用的请求处理时间、请求成功数、最大并发数这些指标,计算当前应用能承载的最大并发请求量,再对比当前系统并发量,判断是否应当拦截本次流量,即所谓"自适应"。
BBR 的源码实现可参考:
插件设计
本插件将 BBR 限流器适配成
QuotaBucket
接口(主要实现GetQuotaWithRelease
判断限流方法),以及ServiceRateLimiter
接口(实现InitQuota
初始化方法)。由于 BBR 限流需要记录请求通过数、当前并发数、请求耗时,因此没有复用原来
QuotaBucket
接口中的GetQuota
方法,而是新增了一个方法GetQuotaWithRelease
,该方法相比于GetQuota
方法,返回参数中多了一个func()
,供业务方在业务逻辑处理完成后调用。由于 CPU 使用率指标为实例单机指标,因此 CPU 限流只适用于单机限流,不适用于分布式限流,未实现分布式限流器需要实现的接口。
初始化 InitQuota
kratos - BBR 初始化需要三个入参:
这三个入参,从
apitraffic.Rule
结构体中解析,直接使用了结构体中的MaxAmount
、ValidDuration
、Precision
字段判断限流 GetQuotaWithRelease
调用了 BBR 的
Allow()
方法其内部执行
shouldDrop()
方法,其执行流程如下:流程中比较关键的一步是计算应用可承受的最大请求量,由下列方法计算:
maxPass * bucketPerSecond / 1000
为每毫秒处理的请求数l.minRT()
为 单个采样窗口中最小的响应时间inFlight
,通过在滑动窗口内的所有buckets中比较得出最多请求完成数maxPass
,以及最小的耗时minRT
,相乘就得出了预期的最佳请求数maxInFlight
。maxInFlight
表示系统能同时处理的最多请求数,这个水位是一个平衡点,保持该水位可以最大化系统的处理能力,超过该水位则会导致请求堆积。inFlight
与maxInFlight
对比,如果前者大于后者那么就已经过载,进而拒绝后续到来的请求防止服务过载。