Skip to content

Commit

Permalink
Test with semaphore and moved wg.Add(1)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorstendb-ARM committed Aug 31, 2023
1 parent 7c18a2f commit 922abb0
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions cmd/installer/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package installer

import (
"context"
"fmt"
"net/url"
"os"
Expand All @@ -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/"
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand All @@ -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()
}
}
}

Expand Down

0 comments on commit 922abb0

Please sign in to comment.