Skip to content

Commit

Permalink
Optimize serving of default Go branch by using local GOROOT.
Browse files Browse the repository at this point in the history
Previously, we would always serve code in GOROOT via the Go repository.
However, it's slightly faster to use files on disk in /usr/local/go,
which can be done when the requested version matches the version
specified in /usr/local/go/VERSION file.
  • Loading branch information
dmitshur committed Aug 16, 2017
1 parent 41b4e9f commit 024640e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
17 changes: 17 additions & 0 deletions localgoroot.go
@@ -0,0 +1,17 @@
package main

import (
"go/build"
"io/ioutil"
"path/filepath"
)

var LocalGoVersion string

func localGoVersion() (string, error) {
version, err := ioutil.ReadFile(filepath.Join(build.Default.GOROOT, "VERSION"))
if err != nil {
return "", err
}
return string(version), nil
}
37 changes: 35 additions & 2 deletions main.go
Expand Up @@ -106,6 +106,15 @@ func main() {
vs = &localVCSStore{dir: *vcsStoreDirFlag}
}

if *remoteGorootFlag {
var err error
LocalGoVersion, err = localGoVersion()
if err != nil {
log.Fatalln("no local Go version available:", err)
}
fmt.Printf("using local Go version %q\n", LocalGoVersion)
}

http.HandleFunc("/", codeHandler)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.HandleFunc("/robots.txt", func(w http.ResponseWriter, req *http.Request) {
Expand Down Expand Up @@ -606,12 +615,15 @@ func tryRemoteGoroot(importPath, rev string) (
return nil, nil, "", "", err
}

// TODO: Use local Go version as default.
defaultBranch = "go1.8.3"
// Use local Go version as default.
defaultBranch = LocalGoVersion

var local bool
if rev != "" {
local = rev == LocalGoVersion
commitId, err = repo.ResolveRevision(rev)
} else {
local = true
commitId, err = repo.ResolveTag(defaultBranch)
}
if err != nil {
Expand All @@ -634,10 +646,31 @@ func tryRemoteGoroot(importPath, rev string) (
fmt.Println("tryRemote: worked on first try")
}

if local {
repo = repoWithLocal{
Repository: repo,
localGoVersion: commitId,
}
}

rs := repoSpec{vcsType: "git", cloneURL: "https://go.googlesource.com/go"} // TODO: Avoid having to return a pointer. It's not optional in this context.
return repo, &rs, commitId, defaultBranch, nil
}

type repoWithLocal struct {
vcs.Repository
localGoVersion vcs.CommitID
}

func (r repoWithLocal) FileSystem(at vcs.CommitID) (vfs.FileSystem, error) {
if at == r.localGoVersion {
fmt.Println("using LocalGoVersion:", at)
return vfs.OS(build.Default.GOROOT), nil
}
fmt.Println("using vcs repo", at)
return r.Repository.FileSystem(at)
}

func tryLocalGopath(importPath, rev string) (
repo vcs.Repository,
repoImportPath string,
Expand Down

2 comments on commit 024640e

@gopherci
Copy link

@gopherci gopherci bot commented on 024640e Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GopherCI found 2 issues in the last 2 commits, see: https://gci.gopherci.io/analysis/937

@gopherci
Copy link

@gopherci gopherci bot commented on 024640e Aug 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GopherCI found 2 issues in the last 2 commits, see: https://gci.gopherci.io/analysis/939

Please sign in to comment.