Skip to content

Commit e7881fd

Browse files
authored
avoid concurrent fetches on events or restarts (#58)
* avoid concurrent fetches on events or restarts * fix tests
1 parent 4b5e778 commit e7881fd

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
pkg-test:
1212
strategy:
1313
matrix:
14-
go-version: [1.23.x, 1.24.x]
14+
go-version: [1.24.x]
1515
os: [ubuntu-latest]
1616
runs-on: ${{ matrix.os }}
1717
steps:

internal/integration_test/e2e_race_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func Test_mirror_detect_race_clone(t *testing.T) {
5757
t.Log("TEST-2: forward HEAD")
5858
fileSHA2 := mustCommit(t, upstream, "file", testName+"-2")
5959

60+
time.Sleep(2 * time.Second)
61+
6062
t.Run("clone-test", func(t *testing.T) {
6163
wg := &sync.WaitGroup{}
6264
// all following assertions will always be true
@@ -170,7 +172,7 @@ func Test_mirror_detect_race_slow_fetch(t *testing.T) {
170172

171173
ctx := context.Background()
172174

173-
time.Sleep(time.Second) // wait for repo.Mirror to grab lock
175+
time.Sleep(2 * time.Second) // wait for repo.Mirror to grab lock
174176

175177
gotHash, err := repo.Hash(ctx, "HEAD", "")
176178
if err != nil {
@@ -212,7 +214,7 @@ func Test_mirror_detect_race_slow_fetch(t *testing.T) {
212214
wg.Add(1)
213215
go func() {
214216
defer wg.Done()
215-
time.Sleep(time.Second) // wait for repo.Mirror to grab lock
217+
time.Sleep(2 * time.Second) // wait for repo.Mirror to grab lock
216218

217219
ctx1, cancel1 := context.WithTimeout(context.Background(), 5*time.Second)
218220
if _, err := repo.Hash(ctx1, "HEAD", ""); err == nil {
@@ -281,9 +283,9 @@ func Test_mirror_detect_race_repo_pool(t *testing.T) {
281283

282284
t.Run("add-remove-repo-test", func(t *testing.T) {
283285
wg := &sync.WaitGroup{}
284-
wg.Add(1)
285286

286287
// add/remove 2 repositories
288+
wg.Add(1)
287289
go func() {
288290
defer wg.Done()
289291
for i := 0; i < 10; i++ {
@@ -315,7 +317,7 @@ func Test_mirror_detect_race_repo_pool(t *testing.T) {
315317

316318
go func() {
317319
for {
318-
time.Sleep(1 * time.Second)
320+
time.Sleep(2 * time.Second)
319321
select {
320322
case <-ctx.Done():
321323
close(readStopped)
@@ -354,6 +356,7 @@ func Test_mirror_detect_race_repo_pool(t *testing.T) {
354356

355357
}()
356358

359+
wg.Add(1)
357360
go func() {
358361
defer wg.Done()
359362
for i := 0; i < 10; i++ {
@@ -385,7 +388,7 @@ func Test_mirror_detect_race_repo_pool(t *testing.T) {
385388
// start loop to trigger read on repo pool
386389
go func() {
387390
for {
388-
time.Sleep(1 * time.Second)
391+
time.Sleep(2 * time.Second)
389392
select {
390393
case <-ctx.Done():
391394
close(readStopped)

internal/integration_test/e2e_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,13 +1297,13 @@ func Test_mirror_loop(t *testing.T) {
12971297

12981298
go repo.StartLoop(txtCtx)
12991299

1300-
time.Sleep(testInterval)
1300+
time.Sleep(testInterval + time.Second)
13011301
if repo.IsRunning() != true {
13021302
t.Errorf("repo running state is still false after starting mirror loop")
13031303
}
13041304

13051305
// wait for the mirror
1306-
time.Sleep(testInterval)
1306+
time.Sleep(testInterval + time.Second)
13071307

13081308
// verify checkout files
13091309
assertLinkedFile(t, root, link1, "file", t.Name()+"-1")
@@ -1314,7 +1314,7 @@ func Test_mirror_loop(t *testing.T) {
13141314
mustCommit(t, upstream, "file", t.Name()+"-2")
13151315

13161316
// wait for the mirror
1317-
time.Sleep(testInterval)
1317+
time.Sleep(testInterval + time.Second)
13181318

13191319
assertLinkedFile(t, root, link1, "file", t.Name()+"-2")
13201320
assertLinkedFile(t, root, link2, "file", t.Name()+"-2")
@@ -1324,7 +1324,7 @@ func Test_mirror_loop(t *testing.T) {
13241324
mustExec(t, upstream, "git", "reset", "-q", "--hard", "HEAD^")
13251325

13261326
// wait for the mirror
1327-
time.Sleep(testInterval)
1327+
time.Sleep(testInterval + time.Second)
13281328

13291329
assertLinkedFile(t, root, link1, "file", t.Name()+"-1")
13301330
assertLinkedFile(t, root, link2, "file", t.Name()+"-1")
@@ -1577,7 +1577,7 @@ func Test_RepoPool_Error(t *testing.T) {
15771577
// start mirror loop
15781578
rp.StartLoop()
15791579

1580-
time.Sleep(time.Second)
1580+
time.Sleep(2 * time.Second)
15811581

15821582
// verify Hash and checked out files
15831583
if got, err := rp.Hash(txtCtx, remote1, "HEAD", ""); err != nil {

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ func main() {
192192

193193
// register handler if skip validation flag is set or secret is set
194194
if *flagGithubWhSkipValidation || *flagGithubWhSecret != "" {
195+
logger.Info("registering github webhook", "path", *flagGithubWhPath)
195196
mux.Handle(*flagGithubWhPath, wh)
196197
}
197198

repository/repository.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,13 +451,16 @@ func (r *Repository) StartLoop(ctx context.Context) {
451451
}()
452452

453453
for {
454+
// to avoid concurrent fetches on events or restarts
455+
time.Sleep(jitter(r.interval, 0.2))
456+
454457
// to stop mirror running indefinitely we will use time-out
455458
mCtx, cancel := context.WithTimeout(ctx, r.mirrorTimeout)
456459
err := r.Mirror(mCtx)
457460
cancel()
458461
recordGitMirror(r.gitURL.Repo, err == nil)
459462

460-
t := time.NewTimer(jitter(r.interval, 0.2))
463+
t := time.NewTimer(r.interval)
461464
select {
462465
case <-t.C:
463466
case <-r.queueMirror:

0 commit comments

Comments
 (0)