Skip to content

Commit d7eb742

Browse files
committed
fix: fix syntax bug
1 parent 9f1b7de commit d7eb742

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

pkg/flow/quota/assist.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,16 @@ func (f *FlowQuotaAssistant) GetQuota(commonRequest *data.CommonRateLimitRequest
263263
if quotaResult == nil {
264264
continue
265265
}
266-
for i := range quotaResult.ReleaseFuncs {
267-
releaseFuncs = append(releaseFuncs, quotaResult.ReleaseFuncs[i])
266+
for _, releaseFunc := range quotaResult.ReleaseFuncs {
267+
if releaseFunc != nil {
268+
releaseFuncs = append(releaseFuncs, releaseFunc)
269+
}
268270
}
269271
// 触发限流,提前返回
270272
if quotaResult.Code == model.QuotaResultLimited {
271273
// 先释放资源
272274
for i := range releaseFuncs {
273-
releaseFuncs[i](0)
275+
releaseFuncs[i]()
274276
}
275277
return model.QuotaFutureWithResponse(quotaResult), nil
276278
}

plugin/ratelimiter/bbr/core/bbr.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"github.com/polarismesh/polaris-go/pkg/log"
45
"github.com/polarismesh/polaris-go/plugin/ratelimiter/bbr/cpu"
56
"github.com/polarismesh/polaris-go/plugin/ratelimiter/bbr/window"
67
"math"
@@ -21,18 +22,16 @@ type (
2122
Option func(*options)
2223
)
2324

24-
func init() {
25-
go collectCPUStat()
26-
}
27-
28-
// collectCPUStat 定期采集并更新 CPU 使用率等指标
25+
// CollectCPUStat 定期采集并更新 CPU 使用率等指标
2926
// cpu = cpuᵗ⁻¹ * decay + cpuᵗ * (1 - decay)
30-
func collectCPUStat() {
27+
func CollectCPUStat() {
3128
ticker := time.NewTicker(time.Millisecond * 500) // same to cpu sample rate
3229
defer func() {
3330
ticker.Stop()
34-
if err := recover(); err != nil {
35-
go collectCPUStat()
31+
if r := recover(); r != nil {
32+
buf := make([]byte, 1<<18)
33+
n := runtime.Stack(buf, false)
34+
log.GetBaseLogger().Errorf("bbr limiter panic recovered: %v.\nruntime stack: %s", r, buf[0:n])
3635
}
3736
}()
3837

plugin/ratelimiter/bbr/plugin.go

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/polarismesh/polaris-go/pkg/plugin"
77
"github.com/polarismesh/polaris-go/pkg/plugin/common"
88
"github.com/polarismesh/polaris-go/pkg/plugin/ratelimiter"
9+
"github.com/polarismesh/polaris-go/plugin/ratelimiter/bbr/core"
910
)
1011

1112
// BBRPlugin 基于 CPU BBR 策略的限流控制器
@@ -26,6 +27,7 @@ func (g *BBRPlugin) Name() string {
2627
// Init 初始化插件
2728
func (g *BBRPlugin) Init(ctx *plugin.InitContext) error {
2829
g.PluginBase = plugin.NewPluginBase(ctx)
30+
go core.CollectCPUStat()
2931
return nil
3032
}
3133

0 commit comments

Comments
 (0)