Skip to content

Commit f245a6b

Browse files
committed
internal/frontend: limit number of paths allowed by frontend fetch
A upperbound of 10 candidateModulePaths is added for the number of fetch attempts that a single frontend fetch request will make. Based on our current dataset, there aren't any module paths that currently have more than 7 path elements. Change-Id: Ia098e538abb5a22ba23c9d6c147ffd1da4f1fbb7 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/248377 Run-TryBot: Julie Qiu <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 0405ee1 commit f245a6b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

internal/frontend/fetch.go

+8
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,11 @@ var vcsHostsWithThreeElementRepoName = map[string]bool{
443443
"golang.org": true,
444444
}
445445

446+
// maxPathsToFetch is the number of modulePaths that are fetched from a single
447+
// fetch request. The longest module path we've seen in our database had 7 path
448+
// elements. maxPathsToFetch is set to 10 as a buffer.
449+
var maxPathsToFetch = 10
450+
446451
// candidateModulePaths returns the potential module paths that could contain
447452
// the fullPath. The paths are returned in reverse length order.
448453
func candidateModulePaths(fullPath string) (_ []string, err error) {
@@ -462,6 +467,9 @@ func candidateModulePaths(fullPath string) (_ []string, err error) {
462467
parts = parts[2:]
463468
}
464469
for _, part := range parts {
470+
if len(modulePaths) == maxPathsToFetch {
471+
return modulePaths, nil
472+
}
465473
path += part
466474
if err := module.CheckImportPath(path); err != nil {
467475
continue

internal/frontend/fetch_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212
"time"
1313

14+
"github.com/google/go-cmp/cmp"
1415
"golang.org/x/pkgsite/internal"
1516
"golang.org/x/pkgsite/internal/derrors"
1617
"golang.org/x/pkgsite/internal/experiment"
@@ -177,3 +178,36 @@ func TestFetchPathAlreadyExists(t *testing.T) {
177178
})
178179
}
179180
}
181+
182+
func TestCandidateModulePaths(t *testing.T) {
183+
maxPathsToFetch = 7
184+
for _, test := range []struct {
185+
name, fullPath string
186+
want []string
187+
}{
188+
{"custom path", "my.module/foo", []string{
189+
"my.module/foo",
190+
"my.module",
191+
}},
192+
{"github.com module path", sample.ModulePath, []string{sample.ModulePath}},
193+
{"more than 7 possible paths", sample.ModulePath + "/1/2/3/4/5/6/7/8/9", []string{
194+
sample.ModulePath + "/1/2/3/4/5/6",
195+
sample.ModulePath + "/1/2/3/4/5",
196+
sample.ModulePath + "/1/2/3/4",
197+
sample.ModulePath + "/1/2/3",
198+
sample.ModulePath + "/1/2",
199+
sample.ModulePath + "/1",
200+
sample.ModulePath,
201+
}},
202+
} {
203+
t.Run(test.fullPath, func(t *testing.T) {
204+
got, err := candidateModulePaths(test.fullPath)
205+
if err != nil {
206+
t.Fatal(err)
207+
}
208+
if diff := cmp.Diff(test.want, got); diff != "" {
209+
t.Fatalf("mismatch (-want, +got):\n%s", diff)
210+
}
211+
})
212+
}
213+
}

0 commit comments

Comments
 (0)