Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RateLimiter: change CanAccept() to TryAccept() #17694

Merged
merged 1 commit into from
Nov 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

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

Godoc please while you are here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is private implementation.
The docs should be covered in public interface (i.e. RateLimiter), right?

Copy link
Member

Choose a reason for hiding this comment

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

ah, sorry, I agree. thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great. Thanks!

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 {
Copy link
Member

Choose a reason for hiding this comment

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

Godoc as well.

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 @@ -302,14 +302,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