File tree 1 file changed +29
-0
lines changed
1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,7 @@ import (
23
23
"net"
24
24
"net/http"
25
25
"reflect"
26
+ "sync"
26
27
"time"
27
28
28
29
"github.com/go-logr/logr"
@@ -341,6 +342,34 @@ type LeaderElectionRunnable interface {
341
342
NeedLeaderElection () bool
342
343
}
343
344
345
+ // NewCancelRunnable returns a new Runnable and a function to cancel that runnable.
346
+ func NewCancelRunnable (r Runnable ) (Runnable , context.CancelFunc ) {
347
+ mu := sync.Mutex {}
348
+ var cancels []func ()
349
+ canceled := false
350
+
351
+ return RunnableFunc (func (ctx context.Context ) error {
352
+ mu .Lock ()
353
+ var cancel context.CancelFunc
354
+ ctx , cancel = context .WithCancel (ctx )
355
+ if canceled {
356
+ cancel ()
357
+ } else {
358
+ cancels = append (cancels , cancel )
359
+ }
360
+ mu .Unlock ()
361
+
362
+ return r .Start (ctx )
363
+ }), func () {
364
+ mu .Lock ()
365
+ defer mu .Unlock ()
366
+ canceled = true
367
+ for _ , cancel := range cancels {
368
+ cancel ()
369
+ }
370
+ }
371
+ }
372
+
344
373
// New returns a new Manager for creating Controllers.
345
374
func New (config * rest.Config , options Options ) (Manager , error ) {
346
375
// Set default values for options fields
You can’t perform that action at this time.
0 commit comments