Skip to content

Commit

Permalink
fix: fix syntax bug
Browse files Browse the repository at this point in the history
  • Loading branch information
WTIFS committed Nov 24, 2023
1 parent 9f1b7de commit d7eb742
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
8 changes: 5 additions & 3 deletions pkg/flow/quota/assist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 7 additions & 8 deletions plugin/ratelimiter/bbr/core/bbr.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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])
}
}()

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

Expand Down

0 comments on commit d7eb742

Please sign in to comment.