Open
Conversation
daheige
approved these changes
Sep 4, 2023
Member
|
Nice! |
This file contains hidden or 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
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.
Kratos - BBR
Kratos 是 bilibili 开源的一套 Go 微服务框架。BBR 是其中的一个限流组件。参考了 TCP BBR 的思想,以及 阿里 Sentinel 的算法。
传统的限流思路为:超过一定负载就拦截流量进入,负载恢复就放开流量,这样做有延迟性,最终是按照果来调节因,无法取得良好效果。
BBR 的思路为:根据应用的请求处理时间、请求成功数、最大并发数这些指标,计算当前应用能承载的最大并发请求量,再对比当前系统并发量,判断是否应当拦截本次流量,即所谓"自适应"。
BBR 的源码实现可参考:
spec 改动
Rule 结构体的 Resource 字段,新增一个枚举类型:CPU
SDK 侧改动
SDK PR:polarismesh/polaris-go#166
SDK 侧新增 bbr 插件。插件使用了 kratos 的 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,相乘就得出了预期的最佳请求数maxFlight。maxFlight表示系统能同时处理的最多请求数,这个水位是一个平衡点,保持该水位可以最大化系统的处理能力,超过该水位则会导致请求堆积。