From 9f7c801316a4d281067aaa23b75b44269e511fd8 Mon Sep 17 00:00:00 2001 From: Vilsol Date: Thu, 7 Dec 2023 01:01:48 +0200 Subject: [PATCH] fix: prevent concurrent map access --- cli/dependency_resolver.go | 10 ++++++---- cli/resolving_test.go | 6 ------ go.mod | 1 + go.sum | 2 ++ 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/cli/dependency_resolver.go b/cli/dependency_resolver.go index 2d15698..e5158d2 100644 --- a/cli/dependency_resolver.go +++ b/cli/dependency_resolver.go @@ -9,6 +9,7 @@ import ( "github.com/mircearoata/pubgrub-go/pubgrub/helpers" "github.com/mircearoata/pubgrub-go/pubgrub/semver" "github.com/pkg/errors" + "github.com/puzpuzpuz/xsync/v3" "github.com/spf13/viper" "github.com/satisfactorymodding/ficsit-cli/cli/provider" @@ -35,7 +36,7 @@ type ficsitAPISource struct { provider provider.Provider lockfile *LockFile toInstall map[string]semver.Constraint - modVersionInfo map[string]ficsit.ModVersionsWithDependenciesResponse + modVersionInfo *xsync.MapOf[string, ficsit.ModVersionsWithDependenciesResponse] gameVersion semver.Version smlVersions []ficsit.SMLVersionsSmlVersionsGetSMLVersionsSml_versionsSMLVersion } @@ -74,7 +75,7 @@ func (f *ficsitAPISource) GetPackageVersions(pkg string) ([]pubgrub.PackageVersi if response.Mod.Id == "" { return nil, errors.Errorf("mod %s not found", pkg) } - f.modVersionInfo[pkg] = *response + f.modVersionInfo.Store(pkg, *response) versions := make([]pubgrub.PackageVersion, len(response.Mod.Versions)) for i, modVersion := range response.Mod.Versions { v, err := semver.NewVersion(modVersion.Version) @@ -145,7 +146,7 @@ func (d DependencyResolver) ResolveModDependencies(constraints map[string]string gameVersion: gameVersionSemver, lockfile: lockFile, toInstall: toInstall, - modVersionInfo: make(map[string]ficsit.ModVersionsWithDependenciesResponse), + modVersionInfo: xsync.NewMapOf[string, ficsit.ModVersionsWithDependenciesResponse](), } result, err := pubgrub.Solve(helpers.NewCachingSource(ficsitSource), rootPkg) @@ -170,7 +171,8 @@ func (d DependencyResolver) ResolveModDependencies(constraints map[string]string } continue } - versions := ficsitSource.modVersionInfo[k].Mod.Versions + value, _ := ficsitSource.modVersionInfo.Load(k) + versions := value.Mod.Versions for _, ver := range versions { if ver.Version == v.RawString() { outputLock[k] = LockedMod{ diff --git a/cli/resolving_test.go b/cli/resolving_test.go index a40aa7f..51439c7 100644 --- a/cli/resolving_test.go +++ b/cli/resolving_test.go @@ -34,12 +34,6 @@ func installWatcher() chan<- InstallUpdate { } else { log.Info().Msg("overall completed") } - } else { - if i.Type != InstallUpdateTypeOverall { - if int(i.Progress.Percentage()*100000)%10 == 0 { - log.Info().Str("mod_reference", i.Item.Mod).Str("version", i.Item.Version).Str("type", string(i.Type)).Float64("percent", i.Progress.Percentage()*100).Msg("progress") - } - } } } }() diff --git a/go.mod b/go.mod index 1386e3b..3004199 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/muesli/reflow v0.3.0 github.com/pkg/errors v0.9.1 github.com/pterm/pterm v0.12.67 + github.com/puzpuzpuz/xsync/v3 v3.0.2 github.com/rs/zerolog v1.28.0 github.com/sahilm/fuzzy v0.1.0 github.com/spf13/cobra v1.6.0 diff --git a/go.sum b/go.sum index 2351901..f1238e7 100644 --- a/go.sum +++ b/go.sum @@ -310,6 +310,8 @@ github.com/pterm/pterm v0.12.36/go.mod h1:NjiL09hFhT/vWjQHSj1athJpx6H8cjpHXNAK5b github.com/pterm/pterm v0.12.40/go.mod h1:ffwPLwlbXxP+rxT0GsgDTzS3y3rmpAO1NMjUkGTYf8s= github.com/pterm/pterm v0.12.67 h1:5iB7ajIQROYfxYD7+sFJ4+KJhFJ+xn7QOVBm4s6RUF0= github.com/pterm/pterm v0.12.67/go.mod h1:nFuT9ZVkkCi8o4L1dtWuYPwDQxggLh4C263qG5nTLpQ= +github.com/puzpuzpuz/xsync/v3 v3.0.2 h1:3yESHrRFYr6xzkz61LLkvNiPFXxJEAABanTQpKbAaew= +github.com/puzpuzpuz/xsync/v3 v3.0.2/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=