Skip to content

Commit

Permalink
feat: 移动cpu初始化逻辑至插件初始化里
Browse files Browse the repository at this point in the history
  • Loading branch information
WTIFS committed Dec 19, 2023
1 parent d7eb742 commit 23ced7f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 4 additions & 5 deletions plugin/ratelimiter/bbr/cpu/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,14 @@ type CPU interface {
Info() Info
}

func init() {
var (
err error
)
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: %w", err)
}
}
go func() {
Expand All @@ -44,6 +42,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 23ced7f

Please sign in to comment.