Source File
fetch.go
Belonging Package
golang.org/x/pkgsite/internal/worker
package worker
import (
)
var ProxyRemoved = map[string]bool{}
func ( *Fetcher) ( context.Context, , , string) ( int, string, error) {
defer derrors.Wrap(&, "FetchAndUpdateState(%q, %q, %q)", , , )
, := trace.StartSpan(, "FetchAndUpdateState")
= experiment.NewContext(, experiment.FromContext().Active()...)
= log.NewContextWithLabel(, "fetch", +"@"+)
if !utf8.ValidString() {
log.Errorf(, "module path %q is not valid UTF-8", )
}
if == internal.UnknownModulePath {
return http.StatusInternalServerError, "", errors.New("called with internal.UnknownModulePath")
}
if !utf8.ValidString() {
log.Errorf(, "requested version %q is not valid UTF-8", )
}
.AddAttributes(
trace.StringAttribute("modulePath", ),
trace.StringAttribute("version", ))
defer .End()
if != nil {
return derrors.ToStatus(), "",
}
:= .fetchAndInsertModule(, , , )
.AddAttributes(trace.Int64Attribute("numPackages", int64(len(.PackageVersionStates))))
if := updateVersionMap(, .DB, ); != nil {
log.Error(, )
if .Status != http.StatusInternalServerError {
.Error =
.Status = http.StatusInternalServerError
}
return .Status, .ResolvedVersion, .Error
}
if .Status != derrors.ToStatus(derrors.AlternativeModule) || == nil || .CookedVersion != .ResolvedVersion {
if := .DB.ReInsertLatestVersion(, ); != nil {
log.Error(, )
if .Status != http.StatusInternalServerError {
.Error =
.Status = http.StatusInternalServerError
}
}
:= time.Now()
:= &postgres.ModuleVersionStateForUpsert{
ModulePath: .ModulePath,
Version: .ResolvedVersion,
AppVersion: ,
Status: .Status,
HasGoMod: .HasGoMod,
GoModPath: .GoModPath,
FetchErr: .Error,
PackageVersionStates: .PackageVersionStates,
}
= .DB.UpsertModuleVersionState(, )
.timings["db.UpsertModuleVersionState"] = time.Since()
if != nil {
log.Error(, )
if .Error != nil {
.Status = http.StatusInternalServerError
.Error = fmt.Errorf("db.UpsertModuleVersionState: %v, original error: %v", , .Error)
}
logTaskResult(, , "Failed to update module version state")
return http.StatusInternalServerError, .ResolvedVersion, .Error
}
logTaskResult(, , "Updated module version state")
return .Status, .ResolvedVersion, .Error
}
func ( *Fetcher) ( context.Context, , string, *internal.LatestModuleVersions) *fetchTask {
:= &fetchTask{
FetchResult: fetch.FetchResult{
ModulePath: ,
RequestedVersion: ,
},
timings: map[string]time.Duration{},
}
defer func() {
derrors.Wrap(&.Error, "fetchAndInsertModule(%q, %q)", , )
if .Error != nil {
.Status = derrors.ToStatus(.Error)
.ResolvedVersion =
}
}()
if ProxyRemoved[+"@"+] {
log.Infof(, "not fetching %s@%s because it is on the ProxyRemoved list", , )
.Error = derrors.Excluded
return
}
, := .DB.IsExcluded(, )
if != nil {
.Error =
return
}
if {
.Error = derrors.Excluded
return
}
var sync.WaitGroup
.Add(1)
go func() {
defer .Done()
:= time.Now()
:= fetch.FetchModule(, , , .ProxyClient, .SourceClient)
if == nil {
panic("fetch.FetchModule should never return a nil FetchResult")
}
defer .Defer()
.FetchResult = *
.timings["fetch.FetchModule"] = time.Since()
var string
.Add(1)
go func() {
defer .Done()
if !.ProxyClient.FetchDisabled() {
= resolvedVersion(, , internal.MainVersion, .ProxyClient)
}
}()
var string
.Add(1)
go func() {
defer .Done()
if !.ProxyClient.FetchDisabled() {
= resolvedVersion(, , internal.MasterVersion, .ProxyClient)
}
}()
.Wait()
.MainVersion =
.MasterVersion =
log.Infof(, "fetch.FetchModule succeeded for %s@%s", .ModulePath, .RequestedVersion)
if {
log.Errorf(, "failed to invalidate cache for %s: %v", .ModulePath, )
} else {
log.Infof(, "invalidated cache for %s", .ModulePath)
}
}
return
}
for , := range "/@?#" {
if := .Cache.DeletePrefix(, fmt.Sprintf("/%s%c", , )); != nil {
= append(, )
}
}
if len() > 0 {
return fmt.Errorf("%d errors, first is %w", len(), [0])
}
return nil
}
func ( context.Context, , string, *proxy.Client) string {
if == stdlib.ModulePath && == internal.MainVersion {
return ""
}
, := fetch.GetInfo(, , , )
if != nil {
log.Warningf(, "fetch.GetInfo(ctx, %q, %q, f.ProxyClient, false): %v", , , )
}
return ""
}
return .Version
}
func ( context.Context, *postgres.DB, *fetchTask) ( error) {
:= time.Now()
defer func() {
.timings["worker.updatedVersionMap"] = time.Since()
derrors.Wrap(&, "updateVersionMap(%q, %q, %q, %d, %v)",
.ModulePath, .RequestedVersion, .ResolvedVersion, .Status, .Error)
}()
, := trace.StartSpan(, "worker.updateVersionMap")
defer .End()
var string
if .Error != nil {
= .Error.Error()
}
:= []string{.RequestedVersion}
if .MainVersion == .ResolvedVersion {
= append(, internal.MainVersion)
}
if .MasterVersion == .ResolvedVersion {
= append(, internal.MasterVersion)
}
for , := range {
:=
:= &internal.VersionMap{
ModulePath: .ModulePath,
RequestedVersion: ,
ResolvedVersion: .ResolvedVersion,
Status: .Status,
GoModPath: .GoModPath,
Error: ,
}
if := .UpsertVersionMap(, ); != nil {
return
}
}
return nil
}
func ( context.Context, *postgres.DB, *fetchTask) ( error) {
:= time.Now()
defer func() {
.timings["worker.deleteModule"] = time.Since()
derrors.Wrap(&, "deleteModule(%q, %q, %q, %d, %v)",
.ModulePath, .RequestedVersion, .ResolvedVersion, .Status, .Error)
}()
, := trace.StartSpan(, "worker.deleteModule")
defer .End()
log.Infof(, "%s@%s: code=%d, deleting", .ModulePath, .ResolvedVersion, .Status)
if := .DeleteModule(, .ModulePath, .ResolvedVersion); != nil {
return
if .Status == derrors.ToStatus(derrors.AlternativeModule) {
log.Infof(, "%s@%s: code=491, deleting older version from search", .ModulePath, .ResolvedVersion)
if := .DeleteOlderVersionFromSearchDocuments(, .ModulePath, .ResolvedVersion); != nil {
return
}
}
return nil
}
func ( context.Context, *fetchTask, string) {
var []string
for , := range .timings {
= append(, fmt.Sprintf("%s=%.3fs", , .Seconds()))
}
sort.Strings()
:= strings.Join(, ", ")
:= log.Infof
if .Status == http.StatusInternalServerError {
= log.Errorf
}
(, "%s for %s@%s: code=%d, num_packages=%d, err=%v; timings: %s",
, .ModulePath, .ResolvedVersion, .Status, len(.PackageVersionStates), .Error, )
}
= 404
default:
= derrors.ToStatus()
}
if != 200 {
return nil, .DB.UpdateLatestModuleVersionsStatus(, , )
}
return .DB.UpdateLatestModuleVersions(, )
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |