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
72770157
Unverified
Commit
72770157
authored
Jan 28, 2019
by
Kubernetes Prow Robot
Committed by
GitHub
Jan 28, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #72928 from lucab/ups/wait-context
apimachinery/wait: add context-aware helpers
parents
cc1be289
c4f1568d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
0 deletions
+93
-0
wait.go
staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go
+32
-0
wait_test.go
staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go
+61
-0
No files found.
staging/src/k8s.io/apimachinery/pkg/util/wait/wait.go
View file @
72770157
...
@@ -88,6 +88,15 @@ func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
...
@@ -88,6 +88,15 @@ func Until(f func(), period time.Duration, stopCh <-chan struct{}) {
JitterUntil
(
f
,
period
,
0.0
,
true
,
stopCh
)
JitterUntil
(
f
,
period
,
0.0
,
true
,
stopCh
)
}
}
// UntilWithContext loops until context is done, running f every period.
//
// UntilWithContext is syntactic sugar on top of JitterUntilWithContext
// with zero jitter factor and with sliding = true (which means the timer
// for period starts after the f completes).
func
UntilWithContext
(
ctx
context
.
Context
,
f
func
(
context
.
Context
),
period
time
.
Duration
)
{
JitterUntilWithContext
(
ctx
,
f
,
period
,
0.0
,
true
)
}
// NonSlidingUntil loops until stop channel is closed, running f every
// NonSlidingUntil loops until stop channel is closed, running f every
// period.
// period.
//
//
...
@@ -98,6 +107,16 @@ func NonSlidingUntil(f func(), period time.Duration, stopCh <-chan struct{}) {
...
@@ -98,6 +107,16 @@ func NonSlidingUntil(f func(), period time.Duration, stopCh <-chan struct{}) {
JitterUntil
(
f
,
period
,
0.0
,
false
,
stopCh
)
JitterUntil
(
f
,
period
,
0.0
,
false
,
stopCh
)
}
}
// NonSlidingUntilWithContext loops until context is done, running f every
// period.
//
// NonSlidingUntilWithContext is syntactic sugar on top of JitterUntilWithContext
// with zero jitter factor, with sliding = false (meaning the timer for period
// starts at the same time as the function starts).
func
NonSlidingUntilWithContext
(
ctx
context
.
Context
,
f
func
(
context
.
Context
),
period
time
.
Duration
)
{
JitterUntilWithContext
(
ctx
,
f
,
period
,
0.0
,
false
)
}
// JitterUntil loops until stop channel is closed, running f every period.
// 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 positive, the period is jittered before every run of f.
...
@@ -151,6 +170,19 @@ func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding b
...
@@ -151,6 +170,19 @@ func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding b
}
}
}
}
// JitterUntilWithContext loops until context is done, 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 and not jittered.
//
// If sliding is true, the period is computed after f runs. If it is false then
// period includes the runtime for f.
//
// Cancel context to stop. f may not be invoked if context is already expired.
func
JitterUntilWithContext
(
ctx
context
.
Context
,
f
func
(
context
.
Context
),
period
time
.
Duration
,
jitterFactor
float64
,
sliding
bool
)
{
JitterUntil
(
func
()
{
f
(
ctx
)
},
period
,
jitterFactor
,
sliding
,
ctx
.
Done
())
}
// Jitter returns a time.Duration between duration and duration + maxFactor *
// Jitter returns a time.Duration between duration and duration + maxFactor *
// duration.
// duration.
//
//
...
...
staging/src/k8s.io/apimachinery/pkg/util/wait/wait_test.go
View file @
72770157
...
@@ -17,6 +17,7 @@ limitations under the License.
...
@@ -17,6 +17,7 @@ limitations under the License.
package
wait
package
wait
import
(
import
(
"context"
"errors"
"errors"
"fmt"
"fmt"
"math/rand"
"math/rand"
...
@@ -48,6 +49,26 @@ func TestUntil(t *testing.T) {
...
@@ -48,6 +49,26 @@ func TestUntil(t *testing.T) {
<-
called
<-
called
}
}
func
TestUntilWithContext
(
t
*
testing
.
T
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
TODO
())
cancel
()
UntilWithContext
(
ctx
,
func
(
context
.
Context
)
{
t
.
Fatal
(
"should not have been invoked"
)
},
0
)
ctx
,
cancel
=
context
.
WithCancel
(
context
.
TODO
())
called
:=
make
(
chan
struct
{})
go
func
()
{
UntilWithContext
(
ctx
,
func
(
context
.
Context
)
{
called
<-
struct
{}{}
},
0
)
close
(
called
)
}()
<-
called
cancel
()
<-
called
}
func
TestNonSlidingUntil
(
t
*
testing
.
T
)
{
func
TestNonSlidingUntil
(
t
*
testing
.
T
)
{
ch
:=
make
(
chan
struct
{})
ch
:=
make
(
chan
struct
{})
close
(
ch
)
close
(
ch
)
...
@@ -68,6 +89,26 @@ func TestNonSlidingUntil(t *testing.T) {
...
@@ -68,6 +89,26 @@ func TestNonSlidingUntil(t *testing.T) {
<-
called
<-
called
}
}
func
TestNonSlidingUntilWithContext
(
t
*
testing
.
T
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
TODO
())
cancel
()
NonSlidingUntilWithContext
(
ctx
,
func
(
context
.
Context
)
{
t
.
Fatal
(
"should not have been invoked"
)
},
0
)
ctx
,
cancel
=
context
.
WithCancel
(
context
.
TODO
())
called
:=
make
(
chan
struct
{})
go
func
()
{
NonSlidingUntilWithContext
(
ctx
,
func
(
context
.
Context
)
{
called
<-
struct
{}{}
},
0
)
close
(
called
)
}()
<-
called
cancel
()
<-
called
}
func
TestUntilReturnsImmediately
(
t
*
testing
.
T
)
{
func
TestUntilReturnsImmediately
(
t
*
testing
.
T
)
{
now
:=
time
.
Now
()
now
:=
time
.
Now
()
ch
:=
make
(
chan
struct
{})
ch
:=
make
(
chan
struct
{})
...
@@ -101,6 +142,26 @@ func TestJitterUntil(t *testing.T) {
...
@@ -101,6 +142,26 @@ func TestJitterUntil(t *testing.T) {
<-
called
<-
called
}
}
func
TestJitterUntilWithContext
(
t
*
testing
.
T
)
{
ctx
,
cancel
:=
context
.
WithCancel
(
context
.
TODO
())
cancel
()
JitterUntilWithContext
(
ctx
,
func
(
context
.
Context
)
{
t
.
Fatal
(
"should not have been invoked"
)
},
0
,
1.0
,
true
)
ctx
,
cancel
=
context
.
WithCancel
(
context
.
TODO
())
called
:=
make
(
chan
struct
{})
go
func
()
{
JitterUntilWithContext
(
ctx
,
func
(
context
.
Context
)
{
called
<-
struct
{}{}
},
0
,
1.0
,
true
)
close
(
called
)
}()
<-
called
cancel
()
<-
called
}
func
TestJitterUntilReturnsImmediately
(
t
*
testing
.
T
)
{
func
TestJitterUntilReturnsImmediately
(
t
*
testing
.
T
)
{
now
:=
time
.
Now
()
now
:=
time
.
Now
()
ch
:=
make
(
chan
struct
{})
ch
:=
make
(
chan
struct
{})
...
...
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