Skip to content

Commit b34572c

Browse files
committed
address review: simplify closure, use pflag.Changed for init-max-failures
- move flInitMaxFailures next to flMaxFailures - default flInitPeriod to flPeriod to kill conditional in hot path - replace isInitFailuresExceeded + useMainLimit with getMaxFailures closure - track waitTime as a loop-outer variable, flip on init-done - detect --init-max-failures via pflag.Changed || GITSYNC_INIT_MAX_FAILURES env; -1 = unlimited, 0 = abort immediately, unset = fall through to --max-failures - drop now-stale unit tests for removed helpers, update --man and README
1 parent 94c27c0 commit b34572c

3 files changed

Lines changed: 35 additions & 150 deletions

File tree

‎README.md‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ SYNC PHASES
215215
repo. During this phase, the retry interval is controlled by
216216
--init-period (falling back to --period if unset) and the failure
217217
limit is controlled by --init-max-failures (falling back to
218-
--max-failures when set to 0). This phase is useful for tolerating
218+
--max-failures when unset). This phase is useful for tolerating
219219
transient connectivity issues at startup while still giving up
220220
eventually.
221221
@@ -386,9 +386,10 @@ OPTIONS
386386
--init-max-failures <int>, $GITSYNC_INIT_MAX_FAILURES
387387
The number of consecutive failures allowed before aborting during
388388
the initial sync phase (before the first successful sync). Once
389-
the initial sync succeeds, --max-failures applies instead. Set
390-
to 0 (the default) to disable this separate limit and fall
391-
through to --max-failures for the entire run.
389+
the initial sync succeeds, --max-failures applies instead.
390+
Setting this to a negative value will retry forever during the
391+
initial sync. If this flag is not set, --max-failures applies
392+
to the initial sync phase as well.
392393
393394
--init-period <duration>, $GITSYNC_INIT_PERIOD
394395
How long to wait between sync attempts until the first successful

‎main.go‎

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,6 @@ func main() {
183183
flErrorFile := pflag.String("error-file",
184184
envString("", "GITSYNC_ERROR_FILE", "GIT_SYNC_ERROR_FILE"),
185185
"the path (absolute or relative to --root) to an optional file into which errors will be written (defaults to disabled)")
186-
flInitMaxFailures := pflag.Int("init-max-failures",
187-
envInt(0, "GITSYNC_INIT_MAX_FAILURES"),
188-
"the number of consecutive failures allowed before aborting during the initial sync phase; 0 disables the check (falls through to --max-failures)")
189186
flInitPeriod := pflag.Duration("init-period",
190187
envDuration(0, "GITSYNC_INIT_PERIOD"),
191188
"how long to wait between sync attempts until the first success, must be >= 10ms if set; if unset, --period is used")
@@ -204,6 +201,9 @@ func main() {
204201
flMaxFailures := pflag.Int("max-failures",
205202
envInt(0, "GITSYNC_MAX_FAILURES", "GIT_SYNC_MAX_FAILURES"),
206203
"the number of consecutive failures allowed before aborting (-1 will retry forever")
204+
flInitMaxFailures := pflag.Int("init-max-failures",
205+
envInt(0, "GITSYNC_INIT_MAX_FAILURES"),
206+
"the number of consecutive failures allowed before aborting during the initial sync phase; a negative value retries forever; if unset, --max-failures applies instead")
207207
flTouchFile := pflag.String("touch-file",
208208
envString("", "GITSYNC_TOUCH_FILE", "GIT_SYNC_TOUCH_FILE"),
209209
"the path (absolute or relative to --root) to an optional file which will be touched whenever a sync completes (defaults to disabled)")
@@ -382,6 +382,11 @@ func main() {
382382

383383
pflag.Parse()
384384

385+
// Was --init-max-failures explicitly set (CLI or env)? envInt applies
386+
// the env value as the pflag default, so pflag.Changed alone misses it.
387+
_, initMaxFailuresEnv := os.LookupEnv("GITSYNC_INIT_MAX_FAILURES")
388+
initMaxFailuresSet := initMaxFailuresEnv || pflag.Lookup("init-max-failures").Changed
389+
385390
// Handle print-and-exit cases.
386391
if *flVersion {
387392
fmt.Fprintln(os.Stdout, version.VERSION)
@@ -475,12 +480,11 @@ func main() {
475480
if *flPeriod < 10*time.Millisecond {
476481
fatalConfigErrorf(log, true, "invalid flag: --period must be at least 10ms")
477482
}
478-
if *flInitPeriod != 0 && *flInitPeriod < 10*time.Millisecond {
483+
if *flInitPeriod == 0 {
484+
*flInitPeriod = *flPeriod
485+
} else if *flInitPeriod < 10*time.Millisecond {
479486
fatalConfigErrorf(log, true, "invalid flag: --init-period must be at least 10ms")
480487
}
481-
if *flInitMaxFailures < 0 {
482-
fatalConfigErrorf(log, true, "invalid flag: --init-max-failures must not be negative")
483-
}
484488

485489
if *flDeprecatedChmod != 0 {
486490
fatalConfigErrorf(log, true, "deprecated flag: --change-permissions is no longer supported")
@@ -925,31 +929,33 @@ func main() {
925929
failCount := 0
926930
syncCount := uint64(0)
927931
initialSyncDone := false
932+
waitTime := *flInitPeriod
933+
// getMaxFailures returns the effective max-failure limit for the current
934+
// phase. During the initial sync phase, --init-max-failures (if set)
935+
// overrides --max-failures; otherwise --max-failures applies.
936+
getMaxFailures := func() int {
937+
if !initialSyncDone && initMaxFailuresSet {
938+
return *flInitMaxFailures
939+
}
940+
return *flMaxFailures
941+
}
928942
for {
929943
start := time.Now()
930944
ctx, cancel := context.WithTimeout(context.Background(), *flSyncTimeout)
931945

932946
if changed, hash, err := git.SyncRepo(ctx, refreshCreds); err != nil {
933947
failCount++
934948
updateSyncMetrics(metricKeyError, start)
935-
if isInitFailuresExceeded(*flInitMaxFailures, initialSyncDone, failCount) {
936-
log.Error(err, "too many initial sync failures, aborting", "initMaxFailures", *flInitMaxFailures, "failCount", failCount)
937-
os.Exit(1)
938-
}
939-
// During the initial sync phase, --init-max-failures (if set) is
940-
// the authoritative limit; --max-failures applies once init is
941-
// done, or when --init-max-failures is not set.
942-
useMainLimit := initialSyncDone || *flInitMaxFailures == 0
943-
if useMainLimit && *flMaxFailures >= 0 && failCount >= *flMaxFailures {
944-
// Exit after too many retries, maybe the error is not recoverable.
945-
log.Error(err, "too many failures, aborting", "failCount", failCount)
949+
if maxFails := getMaxFailures(); maxFails >= 0 && failCount >= maxFails {
950+
log.Error(err, "too many failures, aborting", "failCount", failCount, "maxFailures", maxFails)
946951
os.Exit(1)
947952
}
948953
log.Error(err, "error syncing repo, will retry", "failCount", failCount)
949954
} else {
950955
if !initialSyncDone {
951956
initialSyncDone = true
952-
if *flInitPeriod != 0 {
957+
waitTime = *flPeriod
958+
if *flInitPeriod != *flPeriod {
953959
log.V(0).Info("initial sync complete, switching to normal period", "initPeriod", flInitPeriod.String(), "period", flPeriod.String())
954960
}
955961
}
@@ -1016,8 +1022,6 @@ func main() {
10161022
log.DeleteErrorFile()
10171023
}
10181024

1019-
// Use init-period for retries before the first successful sync.
1020-
waitTime := chooseWaitTime(*flPeriod, *flInitPeriod, initialSyncDone)
10211025
log.V(3).Info("next sync", "waitTime", waitTime.String(), "syncCount", syncCount)
10221026
cancel()
10231027

@@ -1173,26 +1177,6 @@ func setRepoReady() {
11731177
repoReady = true
11741178
}
11751179

1176-
// chooseWaitTime returns the appropriate wait duration based on whether the
1177-
// initial sync has completed. If initPeriod is non-zero and the initial sync
1178-
// is not yet done, it returns initPeriod. Otherwise it returns period.
1179-
func chooseWaitTime(period, initPeriod time.Duration, initialSyncDone bool) time.Duration {
1180-
if !initialSyncDone && initPeriod != 0 {
1181-
return initPeriod
1182-
}
1183-
return period
1184-
}
1185-
1186-
// isInitFailuresExceeded returns true if the initial sync has failed more than
1187-
// initMaxFailures consecutive times. A non-positive initMaxFailures disables
1188-
// the check.
1189-
func isInitFailuresExceeded(initMaxFailures int, initialSyncDone bool, failCount int) bool {
1190-
if initMaxFailures <= 0 || initialSyncDone {
1191-
return false
1192-
}
1193-
return failCount >= initMaxFailures
1194-
}
1195-
11961180
// Do no work, but don't do something that triggers go's runtime into thinking
11971181
// it is deadlocked.
11981182
func sleepForever() {
@@ -2455,7 +2439,7 @@ SYNC PHASES
24552439
repo. During this phase, the retry interval is controlled by
24562440
--init-period (falling back to --period if unset) and the failure
24572441
limit is controlled by --init-max-failures (falling back to
2458-
--max-failures when set to 0). This phase is useful for tolerating
2442+
--max-failures when unset). This phase is useful for tolerating
24592443
transient connectivity issues at startup while still giving up
24602444
eventually.
24612445
@@ -2626,9 +2610,10 @@ OPTIONS
26262610
--init-max-failures <int>, $GITSYNC_INIT_MAX_FAILURES
26272611
The number of consecutive failures allowed before aborting during
26282612
the initial sync phase (before the first successful sync). Once
2629-
the initial sync succeeds, --max-failures applies instead. Set
2630-
to 0 (the default) to disable this separate limit and fall
2631-
through to --max-failures for the entire run.
2613+
the initial sync succeeds, --max-failures applies instead.
2614+
Setting this to a negative value will retry forever during the
2615+
initial sync. If this flag is not set, --max-failures applies
2616+
to the initial sync phase as well.
26322617
26332618
--init-period <duration>, $GITSYNC_INIT_PERIOD
26342619
How long to wait between sync attempts until the first successful

‎main_test.go‎

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -425,107 +425,6 @@ func TestTouch(t *testing.T) {
425425
}
426426
}
427427

428-
func TestChooseWaitTime(t *testing.T) {
429-
period := 10 * time.Second
430-
initPeriod := 500 * time.Millisecond
431-
432-
cases := []struct {
433-
name string
434-
period time.Duration
435-
initPeriod time.Duration
436-
initialSyncDone bool
437-
expected time.Duration
438-
}{{
439-
name: "no init-period, not done",
440-
period: period,
441-
initPeriod: 0,
442-
initialSyncDone: false,
443-
expected: period,
444-
}, {
445-
name: "no init-period, done",
446-
period: period,
447-
initPeriod: 0,
448-
initialSyncDone: true,
449-
expected: period,
450-
}, {
451-
name: "init-period set, not done",
452-
period: period,
453-
initPeriod: initPeriod,
454-
initialSyncDone: false,
455-
expected: initPeriod,
456-
}, {
457-
name: "init-period set, done",
458-
period: period,
459-
initPeriod: initPeriod,
460-
initialSyncDone: true,
461-
expected: period,
462-
}}
463-
464-
for _, tc := range cases {
465-
t.Run(tc.name, func(t *testing.T) {
466-
got := chooseWaitTime(tc.period, tc.initPeriod, tc.initialSyncDone)
467-
if got != tc.expected {
468-
t.Errorf("expected %v, got %v", tc.expected, got)
469-
}
470-
})
471-
}
472-
}
473-
474-
func TestIsInitFailuresExceeded(t *testing.T) {
475-
cases := []struct {
476-
name string
477-
initMaxFailures int
478-
initialSyncDone bool
479-
failCount int
480-
expected bool
481-
}{{
482-
name: "disabled (zero)",
483-
initMaxFailures: 0,
484-
initialSyncDone: false,
485-
failCount: 100,
486-
expected: false,
487-
}, {
488-
name: "disabled (negative)",
489-
initMaxFailures: -1,
490-
initialSyncDone: false,
491-
failCount: 100,
492-
expected: false,
493-
}, {
494-
name: "limit set, already done",
495-
initMaxFailures: 3,
496-
initialSyncDone: true,
497-
failCount: 100,
498-
expected: false,
499-
}, {
500-
name: "limit set, not done, under limit",
501-
initMaxFailures: 5,
502-
initialSyncDone: false,
503-
failCount: 3,
504-
expected: false,
505-
}, {
506-
name: "limit set, not done, at limit",
507-
initMaxFailures: 3,
508-
initialSyncDone: false,
509-
failCount: 3,
510-
expected: true,
511-
}, {
512-
name: "limit set, not done, over limit",
513-
initMaxFailures: 3,
514-
initialSyncDone: false,
515-
failCount: 5,
516-
expected: true,
517-
}}
518-
519-
for _, tc := range cases {
520-
t.Run(tc.name, func(t *testing.T) {
521-
got := isInitFailuresExceeded(tc.initMaxFailures, tc.initialSyncDone, tc.failCount)
522-
if got != tc.expected {
523-
t.Errorf("expected %v, got %v", tc.expected, got)
524-
}
525-
})
526-
}
527-
}
528-
529428
func TestHasGitLockFile(t *testing.T) {
530429
testCases := map[string]struct {
531430
inputFilePath []string

0 commit comments

Comments
 (0)