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

Question for rate limiter #698

Closed
gigimushroom opened this issue Apr 5, 2018 · 2 comments
Closed

Question for rate limiter #698

gigimushroom opened this issue Apr 5, 2018 · 2 comments

Comments

@gigimushroom
Copy link

I am trying to set up rate limiter for my grpc:
limit := rate.NewLimiter(rate.Every(time.Minute), 10)
e = ratelimit.NewErroringLimiter(limit)(e)
pb.RegisterFlowServer(s, server.NewBinding(e))
...

My client test code is to send 10 RPC, wait a minute, then send another 10 RPC.

However, my 2nd batch failed 9 out of 10.

Any ideas?

@gigimushroom
Copy link
Author

gigimushroom commented Apr 5, 2018

It seems the token need a long time to refill itself.

func TestXRateErroring(t *testing.T) {
	limit := rate.NewLimiter(rate.Every(time.Second), 2)
	testSuccessThenFailure(
		t,
		ratelimit.NewErroringLimiter(limit)(nopEndpoint),
		ratelimit.ErrLimited.Error())
}

func testSuccessThenFailure(t *testing.T, e endpoint.Endpoint, failContains string) {
	ctx, cxl := context.WithTimeout(context.Background(), 5*time.Second)
	defer cxl()

	// First request should succeed.
	if _, err := e(ctx, struct{}{}); err != nil {
		t.Errorf("unexpected: %v\n", err)
	}
	if _, err := e(ctx, struct{}{}); err != nil {
		t.Errorf("unexpected2: %v\n", err)
	}

	time.Sleep(time.Second * 2)

	if _, err := e(ctx, struct{}{}); err != nil {
		t.Errorf("unexpected3: %v\n", err)
	}

	if _, err := e(ctx, struct{}{}); err != nil {
		t.Errorf("unexpected5: %v\n", err)
	}
}

Result:

=== RUN   TestXRateErroring
2018-04-04 23:57:04.005158994 -0700 PDT m=+0.000303523 0001-01-01 00:00:00 +0000 UTC 2
num of burst left 2 num of tokens 1
2018-04-04 23:57:04.005276645 -0700 PDT m=+0.000421178 2018-04-04 23:57:04.005158994 -0700 PDT m=+0.000303523 1.000117655
num of burst left 2 num of tokens 0.00011765499999993878
2018-04-04 23:57:06.005817534 -0700 PDT m=+2.000962157 2018-04-04 23:57:04.005276645 -0700 PDT m=+0.000421178 2
num of burst left 2 num of tokens 1
2018-04-04 23:57:06.005983106 -0700 PDT m=+2.001127703 2018-04-04 23:57:06.005817534 -0700 PDT m=+2.000962157 1.000165546
num of burst left 2 num of tokens 0.0001655460000000719
--- PASS: TestXRateErroring (2.00s)
PASS
ok      intent/flow2.0/tools/rate       2.003s

I seems I need to make my program sleep 2 sec, so it will refill.
But based on the API:
rate.NewLimiter(rate.Every(time.Second), 2)
I thought it refill every second with max 2 burst.

Any ideas?

@gigimushroom
Copy link
Author

Not a problem actually.

// A Limiter controls how frequently events are allowed to happen.
// It implements a "token bucket" of size b, initially full and refilled
// at rate r tokens per second.

// Every converts a minimum time interval between events to a Limit.
func Every(interval time.Duration) Limit {
if interval <= 0 {
return Inf
}
return 1 / Limit(interval.Seconds())
}
this produce r.

If I have rate.NewLimiter(rate.Every(time.Second * 1/3), 2),
then 1/3 sec is r = 3, so it generates 3 tokens per second!

marselester added a commit to marselester/kit that referenced this issue Dec 28, 2019
Rate limiting examples might be a bit confusing when Every
method is used go-kit#698.
Naturally one would expect to see a throttling demo as
a number of requests per unit of time instead of defining
an interval between requests.
marselester added a commit to marselester/kit that referenced this issue Dec 29, 2019
Rate limiting examples might be a bit confusing when Every
method is used go-kit#698.
One could expect to see throttling expressed as
a number of requests per unit of time whereas another would see it
as an interval between requests.
peterbourgon pushed a commit that referenced this issue Jan 1, 2020
Rate limiting examples might be a bit confusing when Every
method is used #698.
One could expect to see throttling expressed as
a number of requests per unit of time whereas another would see it
as an interval between requests.
sagikazarmark pushed a commit to go-kit/examples that referenced this issue Jun 4, 2021
Rate limiting examples might be a bit confusing when Every
method is used go-kit/kit#698.
One could expect to see throttling expressed as
a number of requests per unit of time whereas another would see it
as an interval between requests.
muscliary pushed a commit to muscliary/kit that referenced this issue Sep 12, 2023
Rate limiting examples might be a bit confusing when Every
method is used go-kit/kit#698.
One could expect to see throttling expressed as
a number of requests per unit of time whereas another would see it
as an interval between requests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant