Handle review comments for Fix goroutine leak in federation service controller

This commit is contained in:
shashidharatd 2016-09-23 14:27:51 +05:30
parent d8ff4870cb
commit 690a06b9b8
3 changed files with 40 additions and 39 deletions

View File

@ -32,29 +32,28 @@ import (
// It enforces that the syncHandler is never invoked concurrently with the same key. // It enforces that the syncHandler is never invoked concurrently with the same key.
func (sc *ServiceController) clusterEndpointWorker() { func (sc *ServiceController) clusterEndpointWorker() {
// process all pending events in endpointWorkerDoneChan // process all pending events in endpointWorkerDoneChan
eventPending := true ForLoop:
for eventPending { for {
select { select {
case clusterName := <-sc.endpointWorkerDoneChan: case clusterName := <-sc.endpointWorkerDoneChan:
sc.endpointWorkerMap[clusterName] = false sc.endpointWorkerMap[clusterName] = false
default: default:
// non-blocking, comes here if all existing events are processed // non-blocking, comes here if all existing events are processed
eventPending = false break ForLoop
break
} }
} }
for clusterName, cache := range sc.clusterCache.clientMap { for clusterName, cache := range sc.clusterCache.clientMap {
workerExist, keyFound := sc.endpointWorkerMap[clusterName] workerExist, found := sc.endpointWorkerMap[clusterName]
if keyFound && workerExist { if found && workerExist {
continue continue
} }
sc.endpointWorkerMap[clusterName] = true
// create a worker only if the previous worker has finished and gone out of scope // create a worker only if the previous worker has finished and gone out of scope
go func(cache *clusterCache, clusterName string) { go func(cache *clusterCache, clusterName string) {
fedClient := sc.federationClient fedClient := sc.federationClient
for { for {
func() {
key, quit := cache.endpointQueue.Get() key, quit := cache.endpointQueue.Get()
// update endpoint cache // update endpoint cache
if quit { if quit {
@ -67,8 +66,10 @@ func (sc *ServiceController) clusterEndpointWorker() {
if err != nil { if err != nil {
glog.V(2).Infof("Failed to sync endpoint: %+v", err) glog.V(2).Infof("Failed to sync endpoint: %+v", err)
} }
}()
} }
}(cache, clusterName) }(cache, clusterName)
sc.endpointWorkerMap[clusterName] = true
} }
} }

View File

@ -36,29 +36,28 @@ import (
// It enforces that the syncHandler is never invoked concurrently with the same key. // It enforces that the syncHandler is never invoked concurrently with the same key.
func (sc *ServiceController) clusterServiceWorker() { func (sc *ServiceController) clusterServiceWorker() {
// process all pending events in serviceWorkerDoneChan // process all pending events in serviceWorkerDoneChan
eventPending := true ForLoop:
for eventPending { for {
select { select {
case clusterName := <-sc.serviceWorkerDoneChan: case clusterName := <-sc.serviceWorkerDoneChan:
sc.serviceWorkerMap[clusterName] = false sc.serviceWorkerMap[clusterName] = false
default: default:
// non-blocking, comes here if all existing events are processed // non-blocking, comes here if all existing events are processed
eventPending = false break ForLoop
break
} }
} }
for clusterName, cache := range sc.clusterCache.clientMap { for clusterName, cache := range sc.clusterCache.clientMap {
workerExist, keyFound := sc.serviceWorkerMap[clusterName] workerExist, found := sc.serviceWorkerMap[clusterName]
if keyFound && workerExist { if found && workerExist {
continue continue
} }
sc.serviceWorkerMap[clusterName] = true
// create a worker only if the previous worker has finished and gone out of scope // create a worker only if the previous worker has finished and gone out of scope
go func(cache *clusterCache, clusterName string) { go func(cache *clusterCache, clusterName string) {
fedClient := sc.federationClient fedClient := sc.federationClient
for { for {
func() {
key, quit := cache.serviceQueue.Get() key, quit := cache.serviceQueue.Get()
if quit { if quit {
// send signal that current worker has finished tasks and is going out of scope // send signal that current worker has finished tasks and is going out of scope
@ -70,9 +69,10 @@ func (sc *ServiceController) clusterServiceWorker() {
if err != nil { if err != nil {
glog.Errorf("Failed to sync service: %+v", err) glog.Errorf("Failed to sync service: %+v", err)
} }
}()
} }
}(cache, clusterName) }(cache, clusterName)
sc.serviceWorkerMap[clusterName] = true
} }
} }

View File

@ -68,7 +68,7 @@ const (
KubeAPIQPS = 20.0 KubeAPIQPS = 20.0
KubeAPIBurst = 30 KubeAPIBurst = 30
maxNoOfClusters = 256 maxNoOfClusters = 100
) )
type cachedService struct { type cachedService struct {