Skip to content

Commit

Permalink
Merge pull request #17694 from hongchaodeng/ratelimit
Browse files Browse the repository at this point in the history
Auto commit by PR queue bot
  • Loading branch information
k8s-merge-robot committed Nov 26, 2015
2 parents 56897b3 + 13d152a commit 4eb010b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pkg/apiserver/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
type fakeRL bool

func (fakeRL) Stop() {}
func (f fakeRL) CanAccept() bool { return bool(f) }
func (f fakeRL) TryAccept() bool { return bool(f) }
func (f fakeRL) Accept() {}

func expectHTTP(url string, code int, t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/node/rate_limited_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (q *RateLimitedTimedQueue) Try(fn ActionFunc) {
val, ok := q.queue.Head()
for ok {
// rate limit the queue checking
if !q.limiter.CanAccept() {
if !q.limiter.TryAccept() {
glog.V(10).Info("Try rate limitted...")
// Try again later
break
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubelet/dockertools/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (p dockerPuller) Pull(image string, secrets []api.Secret) error {
}

func (p throttledDockerPuller) Pull(image string, secrets []api.Secret) error {
if p.limiter.CanAccept() {
if p.limiter.TryAccept() {
return p.puller.Pull(image, secrets)
}
return fmt.Errorf("pull QPS exceeded.")
Expand Down
9 changes: 5 additions & 4 deletions pkg/util/throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package util
import "github.com/juju/ratelimit"

type RateLimiter interface {
// CanAccept returns true if the rate is below the limit, false otherwise
CanAccept() bool
// TryAccept returns true if a token is taken immediately. Otherwise,
// it returns false.
TryAccept() bool
// Accept returns once a token becomes available.
Accept()
// Stop stops the rate limiter, subsequent calls to CanAccept will return false
Expand All @@ -47,7 +48,7 @@ func NewFakeRateLimiter() RateLimiter {
return &fakeRateLimiter{}
}

func (t *tickRateLimiter) CanAccept() bool {
func (t *tickRateLimiter) TryAccept() bool {
return t.limiter.TakeAvailable(1) == 1
}

Expand All @@ -59,7 +60,7 @@ func (t *tickRateLimiter) Accept() {
func (t *tickRateLimiter) Stop() {
}

func (t *fakeRateLimiter) CanAccept() bool {
func (t *fakeRateLimiter) TryAccept() bool {
return true
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/util/throttle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ import (
func TestBasicThrottle(t *testing.T) {
r := NewTokenBucketRateLimiter(1, 3)
for i := 0; i < 3; i++ {
if !r.CanAccept() {
if !r.TryAccept() {
t.Error("unexpected false accept")
}
}
if r.CanAccept() {
if r.TryAccept() {
t.Error("unexpected true accept")
}
}

func TestIncrementThrottle(t *testing.T) {
r := NewTokenBucketRateLimiter(1, 1)
if !r.CanAccept() {
if !r.TryAccept() {
t.Error("unexpected false accept")
}
if r.CanAccept() {
if r.TryAccept() {
t.Error("unexpected true accept")
}

// Allow to refill
time.Sleep(2 * time.Second)

if !r.CanAccept() {
if !r.TryAccept() {
t.Error("unexpected false accept")
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/pkg/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,14 @@ type FakeRateLimiter struct {
acceptValues []bool
}

func (fr *FakeRateLimiter) CanAccept() bool {
func (fr *FakeRateLimiter) TryAccept() bool {
return true
}

func (fr *FakeRateLimiter) Stop() {}

func (fr *FakeRateLimiter) Accept() {
fr.acceptValues = append(fr.acceptValues, fr.r.CanAccept())
fr.acceptValues = append(fr.acceptValues, fr.r.TryAccept())
}

func TestSchedulerRateLimitsBinding(t *testing.T) {
Expand Down

1 comment on commit 4eb010b

@k8s-teamcity-mesosphere

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TeamCity OSS :: Kubernetes Mesos :: 4 - Smoke Tests Build 6984 outcome was SUCCESS
Summary: Tests passed: 1, ignored: 203 Build time: 00:09:12

Please sign in to comment.