Skip to content

Commit

Permalink
style: 将 cpu.init 移动到插件的初始化函数中
Browse files Browse the repository at this point in the history
  • Loading branch information
WTIFS committed Dec 19, 2023
1 parent d7eb742 commit dd46cc0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/flow/quota/assist.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,15 @@ 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()
quotaResult := window.AllocateQuota(commonRequest)
if quotaResult == nil {
continue
}
// 收集所有 window 的 release 方法
for _, releaseFunc := range quotaResult.ReleaseFuncs {
if releaseFunc != nil {
releaseFuncs = append(releaseFuncs, releaseFunc)
Expand Down
6 changes: 3 additions & 3 deletions plugin/ratelimiter/bbr/cpu/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -44,6 +43,7 @@ func init() {
}
}
}()
return nil
}

// Stat cpu stat.
Expand Down
4 changes: 4 additions & 0 deletions plugin/ratelimiter/bbr/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 策略的限流控制器
Expand All @@ -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
}
Expand Down

0 comments on commit dd46cc0

Please sign in to comment.