-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from un000/rate-limiter-and-leaky-bucket
Rate limiter and leaky bucket
- Loading branch information
Showing
7 changed files
with
110 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2019 Yegor Myskin. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package tailor | ||
|
||
// RateLimiter provides methods to create a custom rate limiter. | ||
type RateLimiter interface { | ||
// Allow says that a line should be sent to a receiver of a lines. | ||
Allow() bool | ||
|
||
// Close finalizes rate limiter. | ||
Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package tailor | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type ChannelBasedRateLimiter struct { | ||
t *time.Ticker | ||
} | ||
|
||
// NewChannelBasedRateLimiter creates an instance of rate limiter, which ticker ticks every period to limit the lps. | ||
func NewChannelBasedRateLimiter(lps int) *ChannelBasedRateLimiter { | ||
return &ChannelBasedRateLimiter{ | ||
t: time.NewTicker(time.Second / time.Duration(lps)), | ||
} | ||
} | ||
|
||
// Allow will block until the ticker ticks. | ||
func (rl *ChannelBasedRateLimiter) Allow() bool { | ||
_, ok := <-rl.t.C | ||
return ok | ||
} | ||
|
||
func (rl *ChannelBasedRateLimiter) Close() { | ||
rl.t.Stop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2019 Yegor Myskin. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package tailor | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestChannelBasedRateLimiterDisallow(t *testing.T) { | ||
l := NewChannelBasedRateLimiter(30) | ||
defer l.Close() | ||
|
||
start := time.Now() | ||
for i := 0; i < 100; i++ { | ||
if !l.Allow() { | ||
t.FailNow() | ||
} | ||
} | ||
dur := time.Since(start) | ||
|
||
if dur < 3333*time.Millisecond || dur > 3500*time.Millisecond { | ||
t.Errorf("expected duration: ~3.33s, actual: %s", dur) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters