Fixes a concurrency issue with go module package path cache

The `chromedp-gen` tool, which makes use of the raw code, always
encounters issues with slow builds. This adds a simple `sync.RWMutex` to
the Go module map package path cache variable that's causing problems.
This commit is contained in:
Kenneth Shaw
2019-10-03 07:30:27 +07:00
parent 1b2b06f5f2
commit baa893c8f6
+14 -6
View File
@@ -90,19 +90,27 @@ func getPkgPathFromGoMod(fname string, isDir bool, goModPath string) (string, er
return path.Clean(rel), nil
}
var (
modulePrefix = []byte("\nmodule ")
pkgPathFromGoModCache = make(map[string]string)
)
var modulePrefix = []byte("\nmodule ")
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)