aboutsummaryrefslogtreecommitdiff
path: root/internal/stdlib
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2020-09-17 13:38:32 -0400
committerJonathan Amsterdam <jba@google.com>2020-09-18 14:13:08 +0000
commit21c3920079c6dab66055fb05418cf01dbded99fe (patch)
tree9750e755a882b71d85c66a04644503e9231333c1 /internal/stdlib
parent101e2fd552d20d264d9b8e9247bb7f1857db9f6b (diff)
downloadgo-x-pkgsite-21c3920079c6dab66055fb05418cf01dbded99fe.tar.xz
internal/fetch: refactor to get info separately
Reorganize FetchModule into two functions. First, one calls GetModuleInfo to get some prelimary, cheap information, including the size of the zip. Then one calls FetchModule with the result of GetModuleInfo. This allows us to make decisions based on the zip size before downloading the entire zip. For golang/go#41452 Change-Id: Ifbffa5882488e31a72e78670caf69c3d97a80546 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/255738 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Julie Qiu <julie@golang.org>
Diffstat (limited to 'internal/stdlib')
-rw-r--r--internal/stdlib/stdlib.go13
-rw-r--r--internal/stdlib/stdlib_test.go16
2 files changed, 29 insertions, 0 deletions
diff --git a/internal/stdlib/stdlib.go b/internal/stdlib/stdlib.go
index 2bfe6c48..6917c46e 100644
--- a/internal/stdlib/stdlib.go
+++ b/internal/stdlib/stdlib.go
@@ -258,6 +258,19 @@ func Directory(version string) string {
return "src"
}
+// Approximate size of Zip("v1.15.2").
+const estimatedZipSize = 16 * 1024 * 1024
+
+func ZipInfo(requestedVersion string) (resolvedVersion string, zipSize int64, err error) {
+ defer derrors.Wrap(&err, "stdlib.ZipInfo(%q)", requestedVersion)
+
+ resolvedVersion, err = semanticVersion(requestedVersion)
+ if err != nil {
+ return "", 0, err
+ }
+ return resolvedVersion, estimatedZipSize, nil
+}
+
// Zip creates a module zip representing the entire Go standard library at the
// given version and returns a reader to it. It also returns the time of the
// commit for that version. The zip file is in module form, with each path
diff --git a/internal/stdlib/stdlib_test.go b/internal/stdlib/stdlib_test.go
index 77a42c7c..253c84cd 100644
--- a/internal/stdlib/stdlib_test.go
+++ b/internal/stdlib/stdlib_test.go
@@ -174,6 +174,22 @@ func TestZip(t *testing.T) {
}
}
+func TestZipInfo(t *testing.T) {
+ UseTestData = true
+ defer func() { UseTestData = false }()
+
+ gotVersion, gotSize, err := ZipInfo("v1.14.6")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if want := "v1.14.6"; gotVersion != want {
+ t.Errorf("version: got %q, want %q", gotVersion, want)
+ }
+ if want := int64(estimatedZipSize); gotSize != want {
+ t.Errorf("size: got %d, want %d", gotSize, want)
+ }
+}
+
func TestVersions(t *testing.T) {
UseTestData = true
defer func() { UseTestData = false }()