Merge pull request #248 from kenshaw/fix-gomod-concurrency

Fixes a concurrency issue with go module package path cache
This commit is contained in:
GoWebProd
2020-03-30 23:02:35 +03:00
committed by GitHub
+12 -5
View File
@@ -89,18 +89,25 @@ func getPkgPathFromGoMod(fname string, isDir bool, goModPath string) (string, er
return path.Clean(rel), nil
}
var (
pkgPathFromGoModCache = make(map[string]string)
)
var pkgPathFromGoModCache = struct {
paths map[string]string
sync.RWMutex
}{
paths: make(map[string]string),
}
func getModulePath(goModPath string) string {
pkgPath, ok := pkgPathFromGoModCache[goModPath]
pkgPathFromGoModCache.RLock()
pkgPath, ok := pkgPathFromGoModCache.paths[goModPath]
pkgPathFromGoModCache.RUnlock()
if ok {
return pkgPath
}
defer func() {
pkgPathFromGoModCache[goModPath] = pkgPath
pkgPathFromGoModCache.Lock()
pkgPathFromGoModCache.paths[goModPath] = pkgPath
pkgPathFromGoModCache.Unlock()
}()
data, err := ioutil.ReadFile(goModPath)