Skip to content

Commit 16fac5d

Browse files
committed
fix: use per-repo mutex to serialize snapshot generation
Replace scheduler-based serialization with a per-repo sync.Mutex. This prevents concurrent snapshot runs for the same repo without blocking on the scheduler's global worker pool, so on-demand HTTP requests are not delayed by unrelated repos' background jobs.
1 parent 9d57c10 commit 16fac5d

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

internal/strategy/git/git.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type Strategy struct {
5151
spoolsMu sync.Mutex
5252
spools map[string]*RepoSpools
5353
tokenManager *githubapp.TokenManager
54+
snapshotMu sync.Map // keyed by upstream URL, values are *sync.Mutex
5455
}
5556

5657
func New(

internal/strategy/git/snapshot.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os/exec"
1010
"path/filepath"
1111
"strings"
12+
"sync"
1213
"time"
1314

1415
"github.com/alecthomas/errors"
@@ -47,6 +48,10 @@ func (s *Strategy) generateAndUploadSnapshot(ctx context.Context, repo *gitclone
4748

4849
logger.InfoContext(ctx, "Snapshot generation started", slog.String("upstream", upstream))
4950

51+
mu := s.snapshotMutexFor(upstream)
52+
mu.Lock()
53+
defer mu.Unlock()
54+
5055
mirrorRoot := s.cloneManager.Config().MirrorRoot
5156
snapshotDir, err := snapshotDirForURL(mirrorRoot, upstream)
5257
if err != nil {
@@ -106,6 +111,11 @@ func (s *Strategy) scheduleSnapshotJobs(repo *gitclone.Repository) {
106111
})
107112
}
108113

114+
func (s *Strategy) snapshotMutexFor(upstreamURL string) *sync.Mutex {
115+
mu, _ := s.snapshotMu.LoadOrStore(upstreamURL, &sync.Mutex{})
116+
return mu.(*sync.Mutex)
117+
}
118+
109119
func (s *Strategy) handleSnapshotRequest(w http.ResponseWriter, r *http.Request, host, pathValue string) {
110120
ctx := r.Context()
111121
logger := logging.FromContext(ctx)

0 commit comments

Comments
 (0)