mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
Merge pull request #194 from bilibili/library/bbr-prev-drop-optimization
修复自适应限流在 cpu 抖动后,可能出现限流过激的问题
This commit is contained in:
commit
a6cc978bdf
@ -17,10 +17,10 @@ import (
|
||||
|
||||
var (
|
||||
cpu int64
|
||||
decay = 0.75
|
||||
decay = 0.95
|
||||
defaultConf = &Config{
|
||||
Window: time.Second * 5,
|
||||
WinBucket: 50,
|
||||
Window: time.Second * 10,
|
||||
WinBucket: 100,
|
||||
CPUThreshold: 800,
|
||||
}
|
||||
)
|
||||
@ -31,6 +31,7 @@ func init() {
|
||||
go cpuproc()
|
||||
}
|
||||
|
||||
// cpu = cpuᵗ⁻¹ * decay + cpuᵗ * (1 - decay)
|
||||
func cpuproc() {
|
||||
ticker := time.NewTicker(time.Millisecond * 250)
|
||||
defer func() {
|
||||
@ -136,7 +137,11 @@ func (l *BBR) shouldDrop() bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
return inFlight > 1 && inFlight > maxInflight
|
||||
drop := inFlight > 1 && inFlight > maxInflight
|
||||
if drop {
|
||||
atomic.StoreInt64(&l.prevDrop, time.Now().Unix())
|
||||
}
|
||||
return drop
|
||||
}
|
||||
|
||||
// Stat tasks a snapshot of the bbr limiter.
|
||||
@ -158,7 +163,6 @@ func (l *BBR) Allow(ctx context.Context, opts ...limit.AllowOption) (func(info l
|
||||
opt.Apply(&allowOpts)
|
||||
}
|
||||
if l.shouldDrop() {
|
||||
atomic.StoreInt64(&l.prevDrop, time.Now().Unix())
|
||||
return nil, ecode.LimitExceed
|
||||
}
|
||||
atomic.AddInt64(&l.inFlight, 1)
|
||||
|
@ -145,7 +145,13 @@ func TestBBRShouldDrop(t *testing.T) {
|
||||
bbr.inFlight = 80
|
||||
assert.Equal(t, true, bbr.shouldDrop())
|
||||
|
||||
// cpu < 800, inflight > maxQps, cold duration
|
||||
cpu = 700
|
||||
bbr.inFlight = 80
|
||||
assert.Equal(t, true, bbr.shouldDrop())
|
||||
|
||||
// cpu < 800, inflight > maxQps
|
||||
time.Sleep(2 * time.Second)
|
||||
cpu = 700
|
||||
bbr.inFlight = 80
|
||||
assert.Equal(t, false, bbr.shouldDrop())
|
||||
|
Loading…
x
Reference in New Issue
Block a user