From d7eb742a495abc753905c240f0e7c66e02247d3b Mon Sep 17 00:00:00 2001 From: wtifs Date: Fri, 24 Nov 2023 17:51:31 +0800 Subject: [PATCH] fix: fix syntax bug --- pkg/flow/quota/assist.go | 8 +++++--- plugin/ratelimiter/bbr/core/bbr.go | 15 +++++++-------- plugin/ratelimiter/bbr/plugin.go | 2 ++ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pkg/flow/quota/assist.go b/pkg/flow/quota/assist.go index 5fb446c9..042f80a8 100644 --- a/pkg/flow/quota/assist.go +++ b/pkg/flow/quota/assist.go @@ -263,14 +263,16 @@ func (f *FlowQuotaAssistant) GetQuota(commonRequest *data.CommonRateLimitRequest if quotaResult == nil { continue } - for i := range quotaResult.ReleaseFuncs { - releaseFuncs = append(releaseFuncs, quotaResult.ReleaseFuncs[i]) + for _, releaseFunc := range quotaResult.ReleaseFuncs { + if releaseFunc != nil { + releaseFuncs = append(releaseFuncs, releaseFunc) + } } // 触发限流,提前返回 if quotaResult.Code == model.QuotaResultLimited { // 先释放资源 for i := range releaseFuncs { - releaseFuncs[i](0) + releaseFuncs[i]() } return model.QuotaFutureWithResponse(quotaResult), nil } diff --git a/plugin/ratelimiter/bbr/core/bbr.go b/plugin/ratelimiter/bbr/core/bbr.go index 861ac43b..cb9d51f0 100644 --- a/plugin/ratelimiter/bbr/core/bbr.go +++ b/plugin/ratelimiter/bbr/core/bbr.go @@ -1,6 +1,7 @@ package core import ( + "github.com/polarismesh/polaris-go/pkg/log" "github.com/polarismesh/polaris-go/plugin/ratelimiter/bbr/cpu" "github.com/polarismesh/polaris-go/plugin/ratelimiter/bbr/window" "math" @@ -21,18 +22,16 @@ type ( Option func(*options) ) -func init() { - go collectCPUStat() -} - -// collectCPUStat 定期采集并更新 CPU 使用率等指标 +// CollectCPUStat 定期采集并更新 CPU 使用率等指标 // cpu = cpuᵗ⁻¹ * decay + cpuᵗ * (1 - decay) -func collectCPUStat() { +func CollectCPUStat() { ticker := time.NewTicker(time.Millisecond * 500) // same to cpu sample rate defer func() { ticker.Stop() - if err := recover(); err != nil { - go collectCPUStat() + if r := recover(); r != nil { + buf := make([]byte, 1<<18) + n := runtime.Stack(buf, false) + log.GetBaseLogger().Errorf("bbr limiter panic recovered: %v.\nruntime stack: %s", r, buf[0:n]) } }() diff --git a/plugin/ratelimiter/bbr/plugin.go b/plugin/ratelimiter/bbr/plugin.go index a22775fd..9ea58895 100644 --- a/plugin/ratelimiter/bbr/plugin.go +++ b/plugin/ratelimiter/bbr/plugin.go @@ -6,6 +6,7 @@ import ( "github.com/polarismesh/polaris-go/pkg/plugin" "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" ) // BBRPlugin 基于 CPU BBR 策略的限流控制器 @@ -26,6 +27,7 @@ func (g *BBRPlugin) Name() string { // Init 初始化插件 func (g *BBRPlugin) Init(ctx *plugin.InitContext) error { g.PluginBase = plugin.NewPluginBase(ctx) + go core.CollectCPUStat() return nil }