Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
3fe4224b
Commit
3fe4224b
authored
Apr 02, 2015
by
Prashanth B
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #6314 from bparees/tokenbucket
add a blocking accept method to RateLimiter
parents
4a2000c4
70be667c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
22 additions
and
0 deletions
+22
-0
handlers_test.go
pkg/apiserver/handlers_test.go
+1
-0
throttle.go
pkg/util/throttle.go
+7
-0
throttle_test.go
pkg/util/throttle_test.go
+14
-0
No files found.
pkg/apiserver/handlers_test.go
View file @
3fe4224b
...
@@ -36,6 +36,7 @@ type fakeRL bool
...
@@ -36,6 +36,7 @@ type fakeRL bool
func
(
fakeRL
)
Stop
()
{}
func
(
fakeRL
)
Stop
()
{}
func
(
f
fakeRL
)
CanAccept
()
bool
{
return
bool
(
f
)
}
func
(
f
fakeRL
)
CanAccept
()
bool
{
return
bool
(
f
)
}
func
(
f
fakeRL
)
Accept
()
{}
func
expectHTTP
(
url
string
,
code
int
,
t
*
testing
.
T
)
{
func
expectHTTP
(
url
string
,
code
int
,
t
*
testing
.
T
)
{
r
,
err
:=
http
.
Get
(
url
)
r
,
err
:=
http
.
Get
(
url
)
...
...
pkg/util/throttle.go
View file @
3fe4224b
...
@@ -24,6 +24,8 @@ import (
...
@@ -24,6 +24,8 @@ import (
type
RateLimiter
interface
{
type
RateLimiter
interface
{
// CanAccept returns true if the rate is below the limit, false otherwise
// CanAccept returns true if the rate is below the limit, false otherwise
CanAccept
()
bool
CanAccept
()
bool
// Accept returns once a token becomes available.
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
()
}
}
...
@@ -73,6 +75,11 @@ func (t *tickRateLimiter) CanAccept() bool {
...
@@ -73,6 +75,11 @@ func (t *tickRateLimiter) CanAccept() bool {
}
}
}
}
// Accept will block until a token becomes available
func
(
t
*
tickRateLimiter
)
Accept
()
{
<-
t
.
tokens
}
func
(
t
*
tickRateLimiter
)
Stop
()
{
func
(
t
*
tickRateLimiter
)
Stop
()
{
close
(
t
.
stop
)
close
(
t
.
stop
)
}
}
...
...
pkg/util/throttle_test.go
View file @
3fe4224b
...
@@ -60,3 +60,17 @@ func TestOverBurst(t *testing.T) {
...
@@ -60,3 +60,17 @@ func TestOverBurst(t *testing.T) {
r
.
step
()
r
.
step
()
}
}
}
}
func
TestThrottle
(
t
*
testing
.
T
)
{
r
:=
NewTokenBucketRateLimiter
(
10
,
5
)
// Should consume 5 tokens immediately, then
// the remaining 11 should take at least 1 second (0.1s each)
expectedFinish
:=
time
.
Now
()
.
Add
(
time
.
Second
*
1
)
for
i
:=
0
;
i
<
16
;
i
++
{
r
.
Accept
()
}
if
time
.
Now
()
.
Before
(
expectedFinish
)
{
t
.
Error
(
"rate limit was not respected, finished too early"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment