Cache results of sub-repo perms enabled checks (#53463)

This commit is contained in:
Petri-Johan Last 2023-06-14 16:28:02 +02:00 committed by GitHub
parent 6fdfa61a3b
commit 5c346a9fc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 8 deletions

View File

@ -164,14 +164,6 @@ func canReadPaths(ctx context.Context, checker SubRepoPermissionChecker, repo ap
return true, nil
}
enabled, err := SubRepoEnabledForRepo(ctx, checker, repo)
if err != nil {
return false, err
}
if !enabled {
return true, nil
}
start := time.Now()
var checkPathPermsCount int
defer func() {

View File

@ -8,6 +8,7 @@ import (
"go.opentelemetry.io/otel/attribute"
"github.com/sourcegraph/sourcegraph/internal/actor"
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/authz"
"github.com/sourcegraph/sourcegraph/internal/search"
"github.com/sourcegraph/sourcegraph/internal/search/job"
@ -84,7 +85,34 @@ func applySubRepoFiltering(ctx context.Context, checker authz.SubRepoPermissionC
// Filter matches in place
filtered := matches[:0]
subRepoPermsCache := map[api.RepoName]bool{}
errCache := map[api.RepoName]struct{}{} // cache repos that errored
for _, m := range matches {
// If the check errored before, skip the repo
if _, ok := errCache[m.RepoName().Name]; ok {
continue
}
// Skip check if sub-repo perms are disabled for the repository
enabled, ok := subRepoPermsCache[m.RepoName().Name]
if ok && !enabled {
filtered = append(filtered, m)
continue
}
if !ok {
enabled, err := authz.SubRepoEnabledForRepo(ctx, checker, m.RepoName().Name)
if err != nil {
// If an error occurs while checking sub-repo perms, we omit it from the results
logger.Error("Could not determine if sub-repo permissions are enabled for repo, skipping", log.String("repoName", string(m.RepoName().Name)))
errCache[m.RepoName().Name] = struct{}{}
continue
}
subRepoPermsCache[m.RepoName().Name] = enabled // cache the result for this repo name
if !enabled {
filtered = append(filtered, m)
continue
}
}
switch mm := m.(type) {
case *result.FileMatch:
repo := mm.Repo.Name