frontend: Return error early when user added repos exceed limit (#19920)

This commit is contained in:
Ryan Slade 2021-04-12 13:35:28 +02:00 committed by GitHub
parent bfa26c3fa3
commit 32f10a208b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 20 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/sourcegraph/sourcegraph/cmd/frontend/backend"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/database"
"github.com/sourcegraph/sourcegraph/internal/timeutil"
)
@ -23,7 +24,8 @@ func (r *schemaResolver) SetExternalServiceRepos(ctx context.Context, args struc
return nil, err
}
es, err := database.GlobalExternalServices.GetByID(ctx, id)
extsvcStore := database.ExternalServices(r.db)
es, err := extsvcStore.GetByID(ctx, id)
if err != nil {
return nil, err
}
@ -46,6 +48,12 @@ func (r *schemaResolver) SetExternalServiceRepos(ctx context.Context, args struc
var repos []string
if args.Repos != nil {
repos = *args.Repos
// If we know the number of repos up front we can ensure that they don't exceed
// their limit before hitting the code host
maxAllowed := conf.UserReposMaxPerUser()
if es.NamespaceUserID != 0 && len(repos) > maxAllowed {
return nil, errors.Errorf("Too many repositories, %d. Sourcegraph supports adding a maximum of %d repositories.", len(repos), maxAllowed)
}
}
err = ra.SetRepos(args.AllRepos, repos)
if err != nil {
@ -62,7 +70,7 @@ func (r *schemaResolver) SetExternalServiceRepos(ctx context.Context, args struc
es.NextSyncAt = time.Time{}
es.UpdatedAt = timeutil.Now()
err = database.GlobalExternalServices.Upsert(ctx, es)
err = extsvcStore.Upsert(ctx, es)
if err != nil {
return nil, err
}

View File

@ -383,3 +383,19 @@ func GitMaxCodehostRequestsPerSecond() int {
}
return *val
}
func UserReposMaxPerUser() int {
v := Get().UserReposMaxPerUser
if v == 0 {
return 2000
}
return v
}
func UserReposMaxPerSite() int {
v := Get().UserReposMaxPerSite
if v == 0 {
return 200000
}
return v
}

View File

@ -21,19 +21,3 @@ func ConfRepoConcurrentExternalServiceSyncers() int {
}
return v
}
func ConfUserReposMaxPerUser() int {
v := conf.Get().UserReposMaxPerUser
if v == 0 {
return 2000
}
return v
}
func ConfUserReposMaxPerSite() int {
v := conf.Get().UserReposMaxPerSite
if v == 0 {
return 200000
}
return v
}

View File

@ -174,7 +174,7 @@ func (s *Syncer) SyncExternalService(ctx context.Context, tx *Store, externalSer
// If we are over our limit for user added repos we abort the sync
totalAllowed := uint64(s.UserReposMaxPerSite)
if totalAllowed == 0 {
totalAllowed = uint64(ConfUserReposMaxPerSite())
totalAllowed = uint64(conf.UserReposMaxPerSite())
}
userAdded, err := tx.CountUserAddedRepos(ctx)
if err != nil {
@ -189,7 +189,7 @@ func (s *Syncer) SyncExternalService(ctx context.Context, tx *Store, externalSer
var sourcedRepoCount int64
maxAllowed := s.UserReposMaxPerUser
if maxAllowed == 0 {
maxAllowed = ConfUserReposMaxPerUser()
maxAllowed = conf.UserReposMaxPerUser()
}
onSourced = func(r *types.Repo) error {
newCount := atomic.AddInt64(&sourcedRepoCount, 1)