Skip to content

Commit 3e2e7bf

Browse files
authored
Add lock for a repository pull mirror (#33876)
Fix #33647 This PR add a global lock for repository pulling mirror.
1 parent 7e8168f commit 3e2e7bf

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

services/mirror/mirror_pull.go

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/git"
1616
giturl "code.gitea.io/gitea/modules/git/url"
1717
"code.gitea.io/gitea/modules/gitrepo"
18+
"code.gitea.io/gitea/modules/globallock"
1819
"code.gitea.io/gitea/modules/lfs"
1920
"code.gitea.io/gitea/modules/log"
2021
"code.gitea.io/gitea/modules/process"
@@ -425,6 +426,10 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
425426
return parseRemoteUpdateOutput(output, m.GetRemoteName()), true
426427
}
427428

429+
func getRepoPullMirrorLockKey(repoID int64) string {
430+
return fmt.Sprintf("repo_pull_mirror_%d", repoID)
431+
}
432+
428433
// SyncPullMirror starts the sync of the pull mirror and schedules the next run.
429434
func SyncPullMirror(ctx context.Context, repoID int64) bool {
430435
log.Trace("SyncMirrors [repo_id: %v]", repoID)
@@ -437,6 +442,13 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
437442
log.Error("PANIC whilst SyncMirrors[repo_id: %d] Panic: %v\nStacktrace: %s", repoID, err, log.Stack(2))
438443
}()
439444

445+
releaser, err := globallock.Lock(ctx, getRepoPullMirrorLockKey(repoID))
446+
if err != nil {
447+
log.Error("globallock.Lock(): %v", err)
448+
return false
449+
}
450+
defer releaser()
451+
440452
m, err := repo_model.GetMirrorByRepoID(ctx, repoID)
441453
if err != nil {
442454
log.Error("SyncMirrors [repo_id: %v]: unable to GetMirrorByRepoID: %v", repoID, err)

0 commit comments

Comments
 (0)