mirror of
https://github.com/netbirdio/easyjson.git
synced 2026-05-22 18:44:42 -07:00
Merge pull request #248 from kenshaw/fix-gomod-concurrency
Fixes a concurrency issue with go module package path cache
This commit is contained in:
+12
-5
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user