diff --git a/pkg/flow/quota/assist.go b/pkg/flow/quota/assist.go index 042f80a8..cd1211c7 100644 --- a/pkg/flow/quota/assist.go +++ b/pkg/flow/quota/assist.go @@ -256,6 +256,7 @@ func (f *FlowQuotaAssistant) GetQuota(commonRequest *data.CommonRateLimitRequest return model.QuotaFutureWithResponse(resp), nil } var maxWaitMs int64 = 0 + // releaseFuncs release 方法列表。如果有多个 window,用这个列表收集所有 window 的 release 方法 var releaseFuncs = make([]model.ReleaseFunc, 0, len(windows)) for _, window := range windows { window.Init() @@ -263,6 +264,7 @@ func (f *FlowQuotaAssistant) GetQuota(commonRequest *data.CommonRateLimitRequest if quotaResult == nil { continue } + // 收集所有 window 的 release 方法 for _, releaseFunc := range quotaResult.ReleaseFuncs { if releaseFunc != nil { releaseFuncs = append(releaseFuncs, releaseFunc) diff --git a/plugin/ratelimiter/bbr/cpu/stat.go b/plugin/ratelimiter/bbr/cpu/stat.go index 92d8a5c7..7399db0f 100644 --- a/plugin/ratelimiter/bbr/cpu/stat.go +++ b/plugin/ratelimiter/bbr/cpu/stat.go @@ -21,16 +21,15 @@ type CPU interface { Info() Info } -func init() { +func Init() error { var ( err error ) stats, err = newCgroupCPU() if err != nil { - // fmt.Printf("cgroup cpu init failed(%v),switch to psutil cpu\n", err) stats, err = newPsutilCPU(interval) if err != nil { - panic(fmt.Sprintf("cgroup cpu init failed!err:=%v", err)) + return fmt.Errorf("cgroup cpu init failed! err: %s", err.Error()) } } go func() { @@ -44,6 +43,7 @@ func init() { } } }() + return nil } // Stat cpu stat. diff --git a/plugin/ratelimiter/bbr/plugin.go b/plugin/ratelimiter/bbr/plugin.go index 9ea58895..7e61ec03 100644 --- a/plugin/ratelimiter/bbr/plugin.go +++ b/plugin/ratelimiter/bbr/plugin.go @@ -7,6 +7,7 @@ import ( "github.com/polarismesh/polaris-go/pkg/plugin/common" "github.com/polarismesh/polaris-go/pkg/plugin/ratelimiter" "github.com/polarismesh/polaris-go/plugin/ratelimiter/bbr/core" + "github.com/polarismesh/polaris-go/plugin/ratelimiter/bbr/cpu" ) // BBRPlugin 基于 CPU BBR 策略的限流控制器 @@ -27,6 +28,9 @@ func (g *BBRPlugin) Name() string { // Init 初始化插件 func (g *BBRPlugin) Init(ctx *plugin.InitContext) error { g.PluginBase = plugin.NewPluginBase(ctx) + if err := cpu.Init(); err != nil { + return err + } go core.CollectCPUStat() return nil }