@@ -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.
11981182func 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
0 commit comments