mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Rate limiter: Add Saturation() method
This commit is contained in:
parent
9781a838da
commit
ba80892cec
@ -26,6 +26,11 @@ type RateLimiter interface {
|
|||||||
Accept()
|
Accept()
|
||||||
// Stop stops the rate limiter, subsequent calls to CanAccept will return false
|
// Stop stops the rate limiter, subsequent calls to CanAccept will return false
|
||||||
Stop()
|
Stop()
|
||||||
|
// Saturation returns a percentage number which describes how saturated
|
||||||
|
// this rate limiter is.
|
||||||
|
// Usually we use token bucket rate limiter. In that case,
|
||||||
|
// 1.0 means no tokens are available; 0.0 means we have a full bucket of tokens to use.
|
||||||
|
Saturation() float64
|
||||||
}
|
}
|
||||||
|
|
||||||
type tokenBucketRateLimiter struct {
|
type tokenBucketRateLimiter struct {
|
||||||
@ -52,6 +57,12 @@ func (t *tokenBucketRateLimiter) TryAccept() bool {
|
|||||||
return t.limiter.TakeAvailable(1) == 1
|
return t.limiter.TakeAvailable(1) == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *tokenBucketRateLimiter) Saturation() float64 {
|
||||||
|
capacity := t.limiter.Capacity()
|
||||||
|
avail := t.limiter.Available()
|
||||||
|
return float64(capacity-avail) / float64(capacity)
|
||||||
|
}
|
||||||
|
|
||||||
// Accept will block until a token becomes available
|
// Accept will block until a token becomes available
|
||||||
func (t *tokenBucketRateLimiter) Accept() {
|
func (t *tokenBucketRateLimiter) Accept() {
|
||||||
t.limiter.Wait(1)
|
t.limiter.Wait(1)
|
||||||
@ -64,6 +75,10 @@ func (t *fakeRateLimiter) TryAccept() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *fakeRateLimiter) Saturation() float64 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func (t *fakeRateLimiter) Stop() {}
|
func (t *fakeRateLimiter) Stop() {}
|
||||||
|
|
||||||
func (t *fakeRateLimiter) Accept() {}
|
func (t *fakeRateLimiter) Accept() {}
|
||||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -63,3 +64,26 @@ func TestThrottle(t *testing.T) {
|
|||||||
t.Error("rate limit was not respected, finished too early")
|
t.Error("rate limit was not respected, finished too early")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRateLimiterSaturation(t *testing.T) {
|
||||||
|
const e = 0.000001
|
||||||
|
tests := []struct {
|
||||||
|
capacity int
|
||||||
|
take int
|
||||||
|
|
||||||
|
expectedSaturation float64
|
||||||
|
}{
|
||||||
|
{1, 1, 1},
|
||||||
|
{10, 3, 0.3},
|
||||||
|
}
|
||||||
|
for i, tt := range tests {
|
||||||
|
rl := NewTokenBucketRateLimiter(1, tt.capacity)
|
||||||
|
for i := 0; i < tt.take; i++ {
|
||||||
|
rl.Accept()
|
||||||
|
}
|
||||||
|
if math.Abs(rl.Saturation()-tt.expectedSaturation) > e {
|
||||||
|
t.Fatalf("#%d: Saturation rate difference isn't within tolerable range\n want=%f, get=%f",
|
||||||
|
i, tt.expectedSaturation, rl.Saturation())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -307,6 +307,10 @@ func (fr *FakeRateLimiter) TryAccept() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fr *FakeRateLimiter) Saturation() float64 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func (fr *FakeRateLimiter) Stop() {}
|
func (fr *FakeRateLimiter) Stop() {}
|
||||||
|
|
||||||
func (fr *FakeRateLimiter) Accept() {
|
func (fr *FakeRateLimiter) Accept() {
|
||||||
|
Loading…
Reference in New Issue
Block a user