Skip to content

Commit dd46cc0

Browse files
committed
style: 将 cpu.init 移动到插件的初始化函数中
1 parent d7eb742 commit dd46cc0

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

pkg/flow/quota/assist.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,15 @@ func (f *FlowQuotaAssistant) GetQuota(commonRequest *data.CommonRateLimitRequest
256256
return model.QuotaFutureWithResponse(resp), nil
257257
}
258258
var maxWaitMs int64 = 0
259+
// releaseFuncs release 方法列表。如果有多个 window,用这个列表收集所有 window 的 release 方法
259260
var releaseFuncs = make([]model.ReleaseFunc, 0, len(windows))
260261
for _, window := range windows {
261262
window.Init()
262263
quotaResult := window.AllocateQuota(commonRequest)
263264
if quotaResult == nil {
264265
continue
265266
}
267+
// 收集所有 window 的 release 方法
266268
for _, releaseFunc := range quotaResult.ReleaseFuncs {
267269
if releaseFunc != nil {
268270
releaseFuncs = append(releaseFuncs, releaseFunc)

plugin/ratelimiter/bbr/cpu/stat.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,15 @@ type CPU interface {
2121
Info() Info
2222
}
2323

24-
func init() {
24+
func Init() error {
2525
var (
2626
err error
2727
)
2828
stats, err = newCgroupCPU()
2929
if err != nil {
30-
// fmt.Printf("cgroup cpu init failed(%v),switch to psutil cpu\n", err)
3130
stats, err = newPsutilCPU(interval)
3231
if err != nil {
33-
panic(fmt.Sprintf("cgroup cpu init failed!err:=%v", err))
32+
return fmt.Errorf("cgroup cpu init failed! err: %s", err.Error())
3433
}
3534
}
3635
go func() {
@@ -44,6 +43,7 @@ func init() {
4443
}
4544
}
4645
}()
46+
return nil
4747
}
4848

4949
// Stat cpu stat.

plugin/ratelimiter/bbr/plugin.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/polarismesh/polaris-go/pkg/plugin/common"
88
"github.com/polarismesh/polaris-go/pkg/plugin/ratelimiter"
99
"github.com/polarismesh/polaris-go/plugin/ratelimiter/bbr/core"
10+
"github.com/polarismesh/polaris-go/plugin/ratelimiter/bbr/cpu"
1011
)
1112

1213
// BBRPlugin 基于 CPU BBR 策略的限流控制器
@@ -27,6 +28,9 @@ func (g *BBRPlugin) Name() string {
2728
// Init 初始化插件
2829
func (g *BBRPlugin) Init(ctx *plugin.InitContext) error {
2930
g.PluginBase = plugin.NewPluginBase(ctx)
31+
if err := cpu.Init(); err != nil {
32+
return err
33+
}
3034
go core.CollectCPUStat()
3135
return nil
3236
}

0 commit comments

Comments
 (0)