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
57bc8719
Commit
57bc8719
authored
Feb 07, 2016
by
k8s-merge-robot
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #19917 from ingvagabund/jitter-until-in-util-package
Auto commit by PR queue bot
parents
fd38f4f4
8e270922
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
75 additions
and
2 deletions
+75
-2
wait.go
pkg/util/wait/wait.go
+16
-2
wait_test.go
pkg/util/wait/wait_test.go
+59
-0
No files found.
pkg/util/wait/wait.go
View file @
57bc8719
...
@@ -42,10 +42,18 @@ func Forever(f func(), period time.Duration) {
...
@@ -42,10 +42,18 @@ func Forever(f func(), period time.Duration) {
}
}
// Until loops until stop channel is closed, running f every period.
// Until loops until stop channel is closed, running f every period.
// Until is syntactic sugar on top of JitterUntil with zero jitter factor
func
Until
(
f
func
(),
period
time
.
Duration
,
stopCh
<-
chan
struct
{})
{
JitterUntil
(
f
,
period
,
0.0
,
stopCh
)
}
// JitterUntil loops until stop channel is closed, running f every period.
// If jitterFactor is positive, the period is jittered before every run of f.
// If jitterFactor is not positive, the period is unchanged.
// Catches any panics, and keeps going. f may not be invoked if
// Catches any panics, and keeps going. f may not be invoked if
// stop channel is already closed. Pass NeverStop to Until if you
// stop channel is already closed. Pass NeverStop to Until if you
// don't want it stop.
// don't want it stop.
func
Until
(
f
func
(),
period
time
.
Duration
,
stopCh
<-
chan
struct
{})
{
func
JitterUntil
(
f
func
(),
period
time
.
Duration
,
jitterFactor
float64
,
stopCh
<-
chan
struct
{})
{
select
{
select
{
case
<-
stopCh
:
case
<-
stopCh
:
return
return
...
@@ -57,10 +65,16 @@ func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
...
@@ -57,10 +65,16 @@ func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
defer
runtime
.
HandleCrash
()
defer
runtime
.
HandleCrash
()
f
()
f
()
}()
}()
jitteredPeriod
:=
period
if
jitterFactor
>
0.0
{
jitteredPeriod
=
Jitter
(
period
,
jitterFactor
)
}
select
{
select
{
case
<-
stopCh
:
case
<-
stopCh
:
return
return
case
<-
time
.
After
(
p
eriod
)
:
case
<-
time
.
After
(
jitteredP
eriod
)
:
}
}
}
}
}
}
...
...
pkg/util/wait/wait_test.go
View file @
57bc8719
...
@@ -56,6 +56,65 @@ func TestUntilReturnsImmediately(t *testing.T) {
...
@@ -56,6 +56,65 @@ func TestUntilReturnsImmediately(t *testing.T) {
}
}
}
}
func
TestJitterUntil
(
t
*
testing
.
T
)
{
ch
:=
make
(
chan
struct
{})
// if a channel is closed JitterUntil never calls function f
// and returns imidiatelly
close
(
ch
)
JitterUntil
(
func
()
{
t
.
Fatal
(
"should not have been invoked"
)
},
0
,
1.0
,
ch
)
ch
=
make
(
chan
struct
{})
called
:=
make
(
chan
struct
{})
go
func
()
{
JitterUntil
(
func
()
{
called
<-
struct
{}{}
},
0
,
1.0
,
ch
)
close
(
called
)
}()
<-
called
close
(
ch
)
<-
called
}
func
TestJitterUntilReturnsImmediately
(
t
*
testing
.
T
)
{
now
:=
time
.
Now
()
ch
:=
make
(
chan
struct
{})
JitterUntil
(
func
()
{
close
(
ch
)
},
30
*
time
.
Second
,
1.0
,
ch
)
if
now
.
Add
(
25
*
time
.
Second
)
.
Before
(
time
.
Now
())
{
t
.
Errorf
(
"JitterUntil did not return immediately when the stop chan was closed inside the func"
)
}
}
func
TestJitterUntilNegativeFactor
(
t
*
testing
.
T
)
{
now
:=
time
.
Now
()
ch
:=
make
(
chan
struct
{})
called
:=
make
(
chan
struct
{})
received
:=
make
(
chan
struct
{})
go
func
()
{
JitterUntil
(
func
()
{
called
<-
struct
{}{}
<-
received
},
time
.
Second
,
-
30.0
,
ch
)
}()
// first loop
<-
called
received
<-
struct
{}{}
// second loop
<-
called
close
(
ch
)
received
<-
struct
{}{}
// it should take at most 2 seconds + some overhead, not 3
if
now
.
Add
(
3
*
time
.
Second
)
.
Before
(
time
.
Now
())
{
t
.
Errorf
(
"JitterUntil did not returned after predefined period with negative jitter factor when the stop chan was closed inside the func"
)
}
}
func
TestExponentialBackoff
(
t
*
testing
.
T
)
{
func
TestExponentialBackoff
(
t
*
testing
.
T
)
{
opts
:=
Backoff
{
Factor
:
1.0
,
Steps
:
3
}
opts
:=
Backoff
{
Factor
:
1.0
,
Steps
:
3
}
...
...
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