From 922abb002ed2e05b6f928d4b9e989eb36b7a97de Mon Sep 17 00:00:00 2001 From: Thorsten de Buhr Date: Thu, 31 Aug 2023 14:55:58 +0200 Subject: [PATCH] Test with semaphore and moved wg.Add(1) --- cmd/installer/root.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/cmd/installer/root.go b/cmd/installer/root.go index 3400794..7c7875d 100644 --- a/cmd/installer/root.go +++ b/cmd/installer/root.go @@ -4,6 +4,7 @@ package installer import ( + "context" "fmt" "net/url" "os" @@ -20,6 +21,7 @@ import ( "github.com/open-cmsis-pack/cpackget/cmd/xml" log "github.com/sirupsen/logrus" "golang.org/x/mod/semver" + "golang.org/x/sync/semaphore" ) const KeilDefaultPackRoot = "https://www.keil.com/pack/" @@ -308,22 +310,24 @@ func DownloadPDSCFiles(skipInstalledPdscFiles bool, concurrency int, timeout int log.Infof("[J%d:F\"%s\"]", numPdsc, Installation.PublicIndex) } + ctx := context.TODO() maxWorkers := runtime.GOMAXPROCS(0) if maxWorkers > concurrency { maxWorkers = concurrency } - cnt := int(0) + sem := semaphore.NewWeighted(int64(maxWorkers)) + for _, pdscTag := range pdscTags { + if err := sem.Acquire(ctx, 1); err != nil { + log.Errorf("Failed to acquire semaphore: %v", err) + break + } + wg.Add(1) go func(pdscTag xml.PdscTag) { + defer sem.Release(1) massDownloadPdscFiles(pdscTag, skipInstalledPdscFiles, &wg, timeout) }(pdscTag) - - cnt++ - if cnt > maxWorkers { - cnt = 0 - wg.Wait() - } } wg.Wait() @@ -338,10 +342,12 @@ func UpdateInstalledPDSCFiles(pidxXML *xml.PidxXML, concurrency int, timeout int return err } + ctx := context.TODO() maxWorkers := runtime.GOMAXPROCS(0) if maxWorkers > concurrency { maxWorkers = concurrency } + sem := semaphore.NewWeighted(int64(maxWorkers)) for _, pdscFile := range pdscFiles { log.Debugf("Checking if \"%s\" needs updating", pdscFile) @@ -368,23 +374,22 @@ func UpdateInstalledPDSCFiles(pidxXML *xml.PidxXML, concurrency int, timeout int continue } - cnt := int(0) versionInIndex := tags[0].Version latestVersion := pdscXML.LatestVersion() if versionInIndex != latestVersion { log.Infof("%s::%s can be upgraded from \"%s\" to \"%s\"", pdscXML.Vendor, pdscXML.Name, latestVersion, versionInIndex) + if err := sem.Acquire(ctx, 1); err != nil { + log.Errorf("Failed to acquire semaphore: %v", err) + break + } wg.Add(1) + pdscTag := tags[0] go func(pdscTag xml.PdscTag) { + defer sem.Release(1) massDownloadPdscFiles(pdscTag, false, &wg, timeout) }(pdscTag) - - cnt++ - if cnt > maxWorkers { - cnt = 0 - wg.Wait() - } } }