1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-03-21 10:50:11 +03:00

Add lock for a repository pull mirror (#33876)

Fix #33647

This PR add a global lock for repository pulling mirror.
This commit is contained in:
Lunny Xiao 2025-03-15 10:23:18 -07:00 committed by GitHub
parent 7e8168f555
commit 3e2e7bf4e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/git"
giturl "code.gitea.io/gitea/modules/git/url"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/globallock"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/process"
@ -425,6 +426,10 @@ func runSync(ctx context.Context, m *repo_model.Mirror) ([]*mirrorSyncResult, bo
return parseRemoteUpdateOutput(output, m.GetRemoteName()), true
}
func getRepoPullMirrorLockKey(repoID int64) string {
return fmt.Sprintf("repo_pull_mirror_%d", repoID)
}
// SyncPullMirror starts the sync of the pull mirror and schedules the next run.
func SyncPullMirror(ctx context.Context, repoID int64) bool {
log.Trace("SyncMirrors [repo_id: %v]", repoID)
@ -437,6 +442,13 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
log.Error("PANIC whilst SyncMirrors[repo_id: %d] Panic: %v\nStacktrace: %s", repoID, err, log.Stack(2))
}()
releaser, err := globallock.Lock(ctx, getRepoPullMirrorLockKey(repoID))
if err != nil {
log.Error("globallock.Lock(): %v", err)
return false
}
defer releaser()
m, err := repo_model.GetMirrorByRepoID(ctx, repoID)
if err != nil {
log.Error("SyncMirrors [repo_id: %v]: unable to GetMirrorByRepoID: %v", repoID, err)