diff --git a/parsers/golang/exported.go b/parsers/golang/exported.go index 79c54c5..55b9a2c 100644 --- a/parsers/golang/exported.go +++ b/parsers/golang/exported.go @@ -11,6 +11,8 @@ import ( func Initialize() (parserinterface.Interface, string) { log.Println("Initializing Golang projects parser") + goDatas = make(map[string]*godata) + p := &golangParser{} return parserinterface.Interface(p), "golang" } diff --git a/parsers/golang/godata.go b/parsers/golang/godata.go index 7a8c66d..9097c06 100644 --- a/parsers/golang/godata.go +++ b/parsers/golang/godata.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "strings" + "sync" // local "go.dev.pztrn.name/glp/configuration" @@ -15,6 +16,20 @@ import ( "go.dev.pztrn.name/glp/structs" ) +var ( + goDatas map[string]*godata + goDatasMutex sync.Mutex +) + +// This structure used for caching data about dependencies and prevent +// unneeded requests. +type godata struct { + SourceURLDirTemplate string + SourceURLFileTemplate string + VCSPath string + VCS string +} + // attrValue returns the attribute value for the case-insensitive key // `name', or the empty string if nothing is found. func attrValue(attrs []xml.Attr, name string) string { @@ -28,6 +43,20 @@ func attrValue(attrs []xml.Attr, name string) string { // Gets go-import and go-source data and fill it in dependency. func getGoData(dependency *structs.Dependency) { + // Check if information about that dependency already cached. + // Use cached data if so. + goDatasMutex.Lock() + depInfo, cached := goDatas[dependency.Name+"@"+dependency.Version] + goDatasMutex.Unlock() + + if cached { + dependency.VCS.SourceURLDirTemplate = depInfo.SourceURLDirTemplate + dependency.VCS.SourceURLFileTemplate = depInfo.SourceURLFileTemplate + dependency.VCS.VCS = depInfo.VCS + dependency.VCS.VCSPath = depInfo.VCSPath + return + } + // Dependencies are imported using URL which can be called with // "?go-get=1" parameter to obtain required VCS data. req, _ := http.NewRequest("GET", "http://"+dependency.Name, nil) @@ -101,4 +130,14 @@ func getGoData(dependency *structs.Dependency) { if configuration.Cfg.Log.Debug { log.Printf("go-import and go-source data parsed: %+v\n", dependency.VCS) } + + // Cache parsed data. + goDatasMutex.Lock() + goDatas[dependency.Name+"@"+dependency.Version] = &godata{ + SourceURLDirTemplate: dependency.VCS.SourceURLDirTemplate, + SourceURLFileTemplate: dependency.VCS.SourceURLFileTemplate, + VCS: dependency.VCS.VCS, + VCSPath: dependency.VCS.VCSPath, + } + goDatasMutex.Unlock() }