From e3a58fd6552622d602e080fb3d3b9ce15ea43b29 Mon Sep 17 00:00:00 2001 From: Salim Afiune Maya Date: Thu, 12 Dec 2024 10:21:40 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20=20reduce=20the=20workerpo?= =?UTF-8?q?ol=20Task=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We really don't need to do everything inside the workerpool, do we? Signed-off-by: Salim Afiune Maya --- explorer/scan/discovery.go | 55 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/explorer/scan/discovery.go b/explorer/scan/discovery.go index 1405f7ed7..d645b7c09 100644 --- a/explorer/scan/discovery.go +++ b/explorer/scan/discovery.go @@ -168,45 +168,46 @@ func discoverAssets(rootAssetWithRuntime *AssetWithRuntime, resolvedRootAsset *i return } - pool := workerpool.New[bool](workers) + pool := workerpool.New[*AssetWithRuntime](workers) pool.Start() defer pool.Close() // for all discovered assets, we apply mondoo-specific labels and annotations that come from the root asset - for _, a := range rootAssetWithRuntime.Runtime.Provider.Connection.Inventory.Spec.Assets { - pool.Submit(func() (bool, error) { - // create runtime for root asset - assetWithRuntime, err := createRuntimeForAsset(a, upstream, recording) + for _, asset := range rootAssetWithRuntime.Runtime.Provider.Connection.Inventory.Spec.Assets { + pool.Submit(func() (*AssetWithRuntime, error) { + assetWithRuntime, err := createRuntimeForAsset(asset, upstream, recording) if err != nil { - log.Error().Err(err).Str("asset", a.Name).Msg("unable to create runtime for asset") - discoveredAssets.AddError(a, err) - return false, err + log.Error().Err(err).Str("asset", asset.GetName()).Msg("unable to create runtime for asset") + discoveredAssets.AddError(asset, err) } + return assetWithRuntime, nil + }) + } - // If no asset was returned and no error, then we observed a duplicate asset with a - // runtime that already exists. - if assetWithRuntime == nil { - return false, nil - } + // Wait for the workers to finish processing + pool.Wait() + + // Get all assets with runtimes from the pool + for _, assetWithRuntime := range pool.GetResults() { + // If asset is nil, then we observed a duplicate asset with a + // runtime that already exists. + if assetWithRuntime == nil { + continue + } - resolvedAsset := assetWithRuntime.Runtime.Provider.Connection.Asset - if len(resolvedAsset.PlatformIds) > 0 { - prepareAsset(resolvedAsset, resolvedRootAsset, runtimeLabels) + resolvedAsset := assetWithRuntime.Runtime.Provider.Connection.Asset + if len(resolvedAsset.PlatformIds) > 0 { + prepareAsset(resolvedAsset, resolvedRootAsset, runtimeLabels) - // If the asset has been already added, we should close its runtime - if !discoveredAssets.Add(resolvedAsset, assetWithRuntime.Runtime) { - assetWithRuntime.Runtime.Close() - } - } else { - discoverAssets(assetWithRuntime, resolvedRootAsset, discoveredAssets, runtimeLabels, upstream, recording) + // If the asset has been already added, we should close its runtime + if !discoveredAssets.Add(resolvedAsset, assetWithRuntime.Runtime) { assetWithRuntime.Runtime.Close() } - return true, nil - }) + } else { + discoverAssets(assetWithRuntime, resolvedRootAsset, discoveredAssets, runtimeLabels, upstream, recording) + assetWithRuntime.Runtime.Close() + } } - - // Wait for the workers to finish processing - pool.Wait() } func createRuntimeForAsset(asset *inventory.Asset, upstream *upstream.UpstreamConfig, recording llx.Recording) (*AssetWithRuntime, error) {