mirror of
https://github.com/gogs/gogs.git
synced 2026-02-27 08:40:54 +01:00
Fix code review issues: FindInBatches usage and memory optimization
Co-authored-by: unknwon <2946214+unknwon@users.noreply.github.com>
This commit is contained in:
@@ -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]
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user