Skip to content

Improve mirror iterator #18928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion models/repo/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ func DeleteMirrorByRepoID(repoID int64) error {
}

// MirrorsIterate iterates all mirror repositories.
func MirrorsIterate(f func(idx int, bean interface{}) error) error {
func MirrorsIterate(limit int, f func(idx int, bean interface{}) error) error {
return db.GetEngine(db.DefaultContext).
Where("next_update_unix<=?", time.Now().Unix()).
And("next_update_unix!=0").
OrderBy("updated_unix ASC").
Limit(limit).
Iterate(new(Mirror), f)
}

Expand Down
3 changes: 2 additions & 1 deletion models/repo/pushmirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ func GetPushMirrorsByRepoID(repoID int64) ([]*PushMirror, error) {
}

// PushMirrorsIterate iterates all push-mirror repositories.
func PushMirrorsIterate(f func(idx int, bean interface{}) error) error {
func PushMirrorsIterate(limit int, f func(idx int, bean interface{}) error) error {
return db.GetEngine(db.DefaultContext).
Where("last_update + (`interval` / ?) <= ?", time.Second, time.Now().Unix()).
And("`interval` != 0").
OrderBy("last_update ASC").
Limit(limit).
Iterate(new(PushMirror), f)
}
2 changes: 1 addition & 1 deletion models/repo/pushmirror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestPushMirrorsIterate(t *testing.T) {

time.Sleep(1 * time.Millisecond)

PushMirrorsIterate(func(idx int, bean interface{}) error {
PushMirrorsIterate(1, func(idx int, bean interface{}) error {
m, ok := bean.(*PushMirror)
assert.True(t, ok)
assert.Equal(t, "test-1", m.RemoteName)
Expand Down
30 changes: 14 additions & 16 deletions services/mirror/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
}
log.Trace("Doing: Update")

requested := 0

handler := func(idx int, bean interface{}, limit int) error {
handler := func(idx int, bean interface{}) error {
var item SyncRequest
var repo *repo_model.Repository
if m, ok := bean.(*repo_model.Mirror); ok {
Expand Down Expand Up @@ -104,35 +102,35 @@ func Update(ctx context.Context, pullLimit, pushLimit int) error {
}
return err
}

requested++
if limit > 0 && requested > limit {
return errLimit
}
return nil
}

pullMirrorsRequested := 0
if pullLimit != 0 {
requested = 0
if err := repo_model.MirrorsIterate(func(idx int, bean interface{}) error {
return handler(idx, bean, pullLimit)
if err := repo_model.MirrorsIterate(pullLimit, func(idx int, bean interface{}) error {
if err := handler(idx, bean); err != nil {
return err
}
pullMirrorsRequested++
return nil
}); err != nil && err != errLimit {
log.Error("MirrorsIterate: %v", err)
return err
}
pullMirrorsRequested, requested = requested, 0
}

pushMirrorsRequested := 0
if pushLimit != 0 {
requested = 0
if err := repo_model.PushMirrorsIterate(func(idx int, bean interface{}) error {
return handler(idx, bean, pushLimit)
if err := repo_model.PushMirrorsIterate(pushLimit, func(idx int, bean interface{}) error {
if err := handler(idx, bean); err != nil {
return err
}
pushMirrorsRequested++
return nil
}); err != nil && err != errLimit {
log.Error("PushMirrorsIterate: %v", err)
return err
}
pushMirrorsRequested, requested = requested, 0
}
log.Trace("Finished: Update: %d pull mirrors and %d push mirrors queued", pullMirrorsRequested, pushMirrorsRequested)
return nil
Expand Down