summaryrefslogtreecommitdiff
path: root/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/k8s.io/apimachinery/pkg/util/wait/wait.go')
-rw-r--r--vendor/k8s.io/apimachinery/pkg/util/wait/wait.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go b/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go
index badaa2159..0997de806 100644
--- a/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go
+++ b/vendor/k8s.io/apimachinery/pkg/util/wait/wait.go
@@ -17,8 +17,10 @@ limitations under the License.
package wait
import (
+ "context"
"errors"
"math/rand"
+ "sync"
"time"
"k8s.io/apimachinery/pkg/util/runtime"
@@ -36,6 +38,40 @@ var ForeverTestTimeout = time.Second * 30
// NeverStop may be passed to Until to make it never stop.
var NeverStop <-chan struct{} = make(chan struct{})
+// Group allows to start a group of goroutines and wait for their completion.
+type Group struct {
+ wg sync.WaitGroup
+}
+
+func (g *Group) Wait() {
+ g.wg.Wait()
+}
+
+// StartWithChannel starts f in a new goroutine in the group.
+// stopCh is passed to f as an argument. f should stop when stopCh is available.
+func (g *Group) StartWithChannel(stopCh <-chan struct{}, f func(stopCh <-chan struct{})) {
+ g.Start(func() {
+ f(stopCh)
+ })
+}
+
+// StartWithContext starts f in a new goroutine in the group.
+// ctx is passed to f as an argument. f should stop when ctx.Done() is available.
+func (g *Group) StartWithContext(ctx context.Context, f func(context.Context)) {
+ g.Start(func() {
+ f(ctx)
+ })
+}
+
+// Start starts f in a new goroutine in the group.
+func (g *Group) Start(f func()) {
+ g.wg.Add(1)
+ go func() {
+ defer g.wg.Done()
+ f()
+ }()
+}
+
// Forever calls f every period for ever.
//
// Forever is syntactic sugar on top of Until.