mirror of
https://github.com/kubernetes/client-go.git
synced 2026-05-16 20:37:09 +00:00
leasecandidate: Improve goroutine management
Make sure all goroutines are terminated when Run returns. Kubernetes-commit: 498896ec4270b790e971a6fb01a292aa4c8cdfe0
This commit is contained in:
committed by
Kubernetes Publisher
parent
d9d16a9e17
commit
0728b482e1
@@ -19,6 +19,7 @@ package leaderelection
|
||||
import (
|
||||
"context"
|
||||
"reflect"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
v1 "k8s.io/api/coordination/v1"
|
||||
@@ -118,19 +119,25 @@ func NewCandidate(clientset kubernetes.Interface,
|
||||
}
|
||||
|
||||
func (c *LeaseCandidate) Run(ctx context.Context) {
|
||||
defer c.queue.ShutDown()
|
||||
|
||||
logger := klog.FromContext(ctx)
|
||||
logger = klog.LoggerWithName(logger, "leasecandidate")
|
||||
ctx = klog.NewContext(ctx, logger)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
defer func() {
|
||||
c.queue.ShutDown()
|
||||
wg.Wait()
|
||||
}()
|
||||
|
||||
c.informerFactory.Start(ctx.Done())
|
||||
if !cache.WaitForNamedCacheSyncWithContext(ctx, c.hasSynced) {
|
||||
return
|
||||
}
|
||||
|
||||
c.enqueueLease()
|
||||
go c.runWorker(ctx)
|
||||
wg.Go(func() {
|
||||
c.runWorker(ctx)
|
||||
})
|
||||
<-ctx.Done()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user