Fix code review issues: FindInBatches usage and memory optimization

Co-authored-by: unknwon <2946214+unknwon@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-24 16:17:07 +00:00
parent 06b85ac5ba
commit ec5eede78f
3 changed files with 17 additions and 16 deletions

View File

@@ -844,8 +844,8 @@ func (pr *PullRequest) checkAndUpdateStatus() {
// TestPullRequests checks and tests untested patches of pull requests.
// TODO: test more pull requests at same time.
func TestPullRequests() {
prs := make([]*PullRequest, 0, 10)
db.Where("status = ?", PullRequestStatusChecking).FindInBatches(&prs, 100, func(tx *gorm.DB, batch int) error {
var prs []*PullRequest
_ = db.Where("status = ?", PullRequestStatusChecking).FindInBatches(&prs, 100, func(tx *gorm.DB, batch int) error {
for i := range prs {
pr := prs[i]

View File

@@ -1902,11 +1902,8 @@ func DeleteOldRepositoryArchives() {
formats := []string{"zip", "targz"}
oldestTime := time.Now().Add(-conf.Cron.RepoArchiveCleanup.OlderThan)
err := db.Where("id > 0").FindInBatches(&[]Repository{}, 100, func(tx *gorm.DB, batch int) error {
var repos []Repository
if err := tx.Find(&repos).Error; err != nil {
return err
}
var repos []Repository
err := db.Where("id > 0").FindInBatches(&repos, 100, func(tx *gorm.DB, batch int) error {
for _, repo := range repos {
basePath := filepath.Join(repo.RepoPath(), "archives")
for _, format := range formats {

View File

@@ -521,19 +521,23 @@ func RewriteAuthorizedKeys() error {
}
defer os.Remove(tmpPath)
var keys []*PublicKey
err = db.Find(&keys).Error
// Use FindInBatches to process keys in chunks to avoid memory issues with large datasets
err = db.FindInBatches(&[]PublicKey{}, 100, func(tx *gorm.DB, batch int) error {
var keys []PublicKey
if err := tx.Find(&keys).Error; err != nil {
return err
}
for _, key := range keys {
if _, err := f.WriteString(key.AuthorizedString()); err != nil {
return err
}
}
return nil
}).Error
if err != nil {
_ = f.Close()
return err
}
for _, key := range keys {
if _, err = f.WriteString(key.AuthorizedString()); err != nil {
_ = f.Close()
return err
}
}
_ = f.Close()
if com.IsExist(fpath) {