|
|
@ -17,10 +17,10 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
cpu int64 |
|
|
|
cpu int64 |
|
|
|
decay = 0.75 |
|
|
|
decay = 0.95 |
|
|
|
defaultConf = &Config{ |
|
|
|
defaultConf = &Config{ |
|
|
|
Window: time.Second * 5, |
|
|
|
Window: time.Second * 10, |
|
|
|
WinBucket: 50, |
|
|
|
WinBucket: 100, |
|
|
|
CPUThreshold: 800, |
|
|
|
CPUThreshold: 800, |
|
|
|
} |
|
|
|
} |
|
|
|
) |
|
|
|
) |
|
|
@ -31,6 +31,7 @@ func init() { |
|
|
|
go cpuproc() |
|
|
|
go cpuproc() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// cpu = cpuᵗ⁻¹ * decay + cpuᵗ * (1 - decay)
|
|
|
|
func cpuproc() { |
|
|
|
func cpuproc() { |
|
|
|
ticker := time.NewTicker(time.Millisecond * 250) |
|
|
|
ticker := time.NewTicker(time.Millisecond * 250) |
|
|
|
defer func() { |
|
|
|
defer func() { |
|
|
@ -136,7 +137,11 @@ func (l *BBR) shouldDrop() bool { |
|
|
|
} |
|
|
|
} |
|
|
|
return false |
|
|
|
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.
|
|
|
|
// 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) |
|
|
|
opt.Apply(&allowOpts) |
|
|
|
} |
|
|
|
} |
|
|
|
if l.shouldDrop() { |
|
|
|
if l.shouldDrop() { |
|
|
|
atomic.StoreInt64(&l.prevDrop, time.Now().Unix()) |
|
|
|
|
|
|
|
return nil, ecode.LimitExceed |
|
|
|
return nil, ecode.LimitExceed |
|
|
|
} |
|
|
|
} |
|
|
|
atomic.AddInt64(&l.inFlight, 1) |
|
|
|
atomic.AddInt64(&l.inFlight, 1) |
|
|
|