@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "fmt"
2222 "github.com/caarlos0/env"
23+ "github.com/devtron-labs/common-lib/async"
2324 "github.com/devtron-labs/common-lib/utils/k8s"
2425 k8sCommonBean "github.com/devtron-labs/common-lib/utils/k8s/commonBean"
2526 "github.com/devtron-labs/devtron/api/bean/AppView"
@@ -75,6 +76,7 @@ type K8sCommonServiceImpl struct {
7576 K8sApplicationServiceConfig * K8sApplicationServiceConfig
7677 argoApplicationConfigService config.ArgoApplicationConfigService
7778 ClusterReadService read.ClusterReadService
79+ asyncRunnable * async.Runnable
7880}
7981type K8sApplicationServiceConfig struct {
8082 BatchSize int `env:"BATCH_SIZE" envDefault:"5" description:"there is feature to get URL's of services/ingresses. so to extract those, we need to parse all the servcie and ingress objects of the application. this BATCH_SIZE flag controls the no of these objects get parsed in one go."`
@@ -83,7 +85,7 @@ type K8sApplicationServiceConfig struct {
8385
8486func NewK8sCommonServiceImpl (Logger * zap.SugaredLogger , k8sUtils * k8s.K8sServiceImpl ,
8587 argoApplicationConfigService config.ArgoApplicationConfigService ,
86- ClusterReadService read.ClusterReadService ) * K8sCommonServiceImpl {
88+ ClusterReadService read.ClusterReadService , asyncRunnable * async. Runnable ) * K8sCommonServiceImpl {
8789 cfg := & K8sApplicationServiceConfig {}
8890 err := env .Parse (cfg )
8991 if err != nil {
@@ -95,6 +97,7 @@ func NewK8sCommonServiceImpl(Logger *zap.SugaredLogger, k8sUtils *k8s.K8sService
9597 K8sApplicationServiceConfig : cfg ,
9698 argoApplicationConfigService : argoApplicationConfigService ,
9799 ClusterReadService : ClusterReadService ,
100+ asyncRunnable : asyncRunnable ,
98101 }
99102}
100103
@@ -295,15 +298,18 @@ func (impl *K8sCommonServiceImpl) GetManifestsByBatch(ctx context.Context, reque
295298 var res []bean5.BatchResourceResponse
296299 ctx , cancel := context .WithTimeout (ctx , time .Duration (impl .K8sApplicationServiceConfig .TimeOutInSeconds )* time .Second )
297300 defer cancel ()
298- go func () {
301+
302+ runnableFunc := func () {
299303 ans := impl .getManifestsByBatch (ctx , requests )
300304 select {
301305 case <- ctx .Done ():
302306 return
303307 default :
304308 ch <- ans
305309 }
306- }()
310+ }
311+ impl .asyncRunnable .Execute (runnableFunc )
312+
307313 select {
308314 case ans := <- ch :
309315 res = ans
@@ -398,7 +404,7 @@ func (impl *K8sCommonServiceImpl) getManifestsByBatch(ctx context.Context, reque
398404 var wg sync.WaitGroup
399405 for j := 0 ; j < batchSize ; j ++ {
400406 wg .Add (1 )
401- go func (j int ) {
407+ runnableFunc := func (j int ) {
402408 resp := bean5.BatchResourceResponse {}
403409 response , err := impl .GetResource (ctx , & requests [i + j ])
404410 if response != nil {
@@ -407,7 +413,8 @@ func (impl *K8sCommonServiceImpl) getManifestsByBatch(ctx context.Context, reque
407413 resp .Err = err
408414 res [i + j ] = resp
409415 wg .Done ()
410- }(j )
416+ }
417+ impl .asyncRunnable .Execute (func () { runnableFunc (j ) })
411418 }
412419 wg .Wait ()
413420 i += batchSize
0 commit comments