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
7ffaef63
Commit
7ffaef63
authored
Nov 05, 2015
by
jay vyas
Committed by
jay vyas
Nov 25, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Backoff and Randomness additions for use in client utilities
Updated String to use int, and int not to panic, and panic to test panic Format fixes.
parent
3a5e7d15
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
11 deletions
+50
-11
backoff.go
pkg/util/backoff.go
+7
-0
backoff_test.go
pkg/util/backoff_test.go
+8
-0
rand.go
pkg/util/rand/rand.go
+14
-11
rand_test.go
pkg/util/rand/rand_test.go
+21
-0
No files found.
pkg/util/backoff.go
View file @
7ffaef63
...
@@ -70,6 +70,13 @@ func (p *Backoff) Next(id string, eventTime time.Time) {
...
@@ -70,6 +70,13 @@ func (p *Backoff) Next(id string, eventTime time.Time) {
entry
.
lastUpdate
=
p
.
Clock
.
Now
()
entry
.
lastUpdate
=
p
.
Clock
.
Now
()
}
}
// Reset forces clearing of all backoff data for a given key.
func
(
p
*
Backoff
)
Reset
(
id
string
)
{
p
.
Lock
()
defer
p
.
Unlock
()
delete
(
p
.
perItemBackoff
,
id
)
}
// Returns True if the elapsed time since eventTime is smaller than the current backoff window
// Returns True if the elapsed time since eventTime is smaller than the current backoff window
func
(
p
*
Backoff
)
IsInBackOffSince
(
id
string
,
eventTime
time
.
Time
)
bool
{
func
(
p
*
Backoff
)
IsInBackOffSince
(
id
string
,
eventTime
time
.
Time
)
bool
{
p
.
Lock
()
p
.
Lock
()
...
...
pkg/util/backoff_test.go
View file @
7ffaef63
...
@@ -46,6 +46,14 @@ func TestSlowBackoff(t *testing.T) {
...
@@ -46,6 +46,14 @@ func TestSlowBackoff(t *testing.T) {
}
}
b
.
Next
(
id
,
tc
.
Now
())
b
.
Next
(
id
,
tc
.
Now
())
}
}
//Now confirm that the Reset cancels backoff.
b
.
Next
(
id
,
tc
.
Now
())
b
.
Reset
(
id
)
if
b
.
Get
(
id
)
!=
0
{
t
.
Errorf
(
"Reset didn't clear the backoff."
)
}
}
}
func
TestBackoffReset
(
t
*
testing
.
T
)
{
func
TestBackoffReset
(
t
*
testing
.
T
)
{
...
...
pkg/util/rand/rand.go
View file @
7ffaef63
...
@@ -32,19 +32,12 @@ var rng = struct {
...
@@ -32,19 +32,12 @@ var rng = struct {
rand
:
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UTC
()
.
UnixNano
())),
rand
:
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UTC
()
.
UnixNano
())),
}
}
// String generates a random alphanumeric string n characters long. This will
// Intn generates an integer in range 0->max.
// panic if n is less than zero.
// By design this should panic if input is invalid, <= 0.
func
String
(
n
int
)
string
{
func
Intn
(
max
int
)
int
{
if
n
<
0
{
panic
(
"out-of-bounds value"
)
}
b
:=
make
([]
rune
,
n
)
rng
.
Lock
()
rng
.
Lock
()
defer
rng
.
Unlock
()
defer
rng
.
Unlock
()
for
i
:=
range
b
{
return
rng
.
rand
.
Intn
(
max
)
b
[
i
]
=
letters
[
rng
.
rand
.
Intn
(
numLetters
)]
}
return
string
(
b
)
}
}
// Seed seeds the rng with the provided seed.
// Seed seeds the rng with the provided seed.
...
@@ -62,3 +55,13 @@ func Perm(n int) []int {
...
@@ -62,3 +55,13 @@ func Perm(n int) []int {
defer
rng
.
Unlock
()
defer
rng
.
Unlock
()
return
rng
.
rand
.
Perm
(
n
)
return
rng
.
rand
.
Perm
(
n
)
}
}
// String generates a random alphanumeric string n characters long. This will
// panic if n is less than zero.
func
String
(
length
int
)
string
{
b
:=
make
([]
rune
,
length
)
for
i
:=
range
b
{
b
[
i
]
=
letters
[
Intn
(
numLetters
)]
}
return
string
(
b
)
}
pkg/util/rand/rand_test.go
View file @
7ffaef63
...
@@ -37,6 +37,27 @@ func TestString(t *testing.T) {
...
@@ -37,6 +37,27 @@ func TestString(t *testing.T) {
}
}
}
}
// Confirm that panic occurs on invalid input.
func
TestRangePanic
(
t
*
testing
.
T
)
{
defer
func
()
{
if
err
:=
recover
();
err
==
nil
{
t
.
Errorf
(
"Panic didn't occur!"
)
}
}()
// Should result in an error...
Intn
(
0
)
}
func
TestIntn
(
t
*
testing
.
T
)
{
// 0 is invalid.
for
_
,
max
:=
range
[]
int
{
1
,
2
,
10
,
123
}
{
inrange
:=
Intn
(
max
)
if
inrange
<
0
||
inrange
>
max
{
t
.
Errorf
(
"%v out of range (0,%v)"
,
inrange
,
max
)
}
}
}
func
TestPerm
(
t
*
testing
.
T
)
{
func
TestPerm
(
t
*
testing
.
T
)
{
Seed
(
5
)
Seed
(
5
)
rand
.
Seed
(
5
)
rand
.
Seed
(
5
)
...
...
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