@@ -183,12 +183,12 @@ 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)" )
186189 flInitPeriod := pflag .Duration ("init-period" ,
187190 envDuration (0 , "GITSYNC_INIT_PERIOD" ),
188191 "how long to wait between sync attempts until the first success, must be >= 10ms if set; if unset, --period is used" )
189- flInitTimeout := pflag .Duration ("init-timeout" ,
190- envDuration (0 , "GITSYNC_INIT_TIMEOUT" ),
191- "the max time allowed for the initial sync to succeed; if unset, there is no timeout (retries forever until --max-failures)" )
192192 flPeriod := pflag .Duration ("period" ,
193193 envDuration (10 * time .Second , "GITSYNC_PERIOD" , "GIT_SYNC_PERIOD" ),
194194 "how long to wait between syncs, must be >= 10ms; --wait overrides this" )
@@ -478,8 +478,8 @@ func main() {
478478 if * flInitPeriod != 0 && * flInitPeriod < 10 * time .Millisecond {
479479 fatalConfigErrorf (log , true , "invalid flag: --init-period must be at least 10ms" )
480480 }
481- if * flInitTimeout != 0 && * flInitTimeout < 10 * time . Millisecond {
482- fatalConfigErrorf (log , true , "invalid flag: --init-timeout must be at least 10ms " )
481+ if * flInitMaxFailures < 0 {
482+ fatalConfigErrorf (log , true , "invalid flag: --init-max-failures must not be negative " )
483483 }
484484
485485 if * flDeprecatedChmod != 0 {
@@ -925,22 +925,22 @@ func main() {
925925 failCount := 0
926926 syncCount := uint64 (0 )
927927 initialSyncDone := false
928- var initStart time.Time
929- if * flInitPeriod != 0 || * flInitTimeout != 0 {
930- initStart = time .Now ()
931- }
932928 for {
933929 start := time .Now ()
934930 ctx , cancel := context .WithTimeout (context .Background (), * flSyncTimeout )
935931
936932 if changed , hash , err := git .SyncRepo (ctx , refreshCreds ); err != nil {
937933 failCount ++
938934 updateSyncMetrics (metricKeyError , start )
939- if isInitTimedOut ( * flInitTimeout , initialSyncDone , initStart ) {
940- log .Error (err , "initial sync timed out " , "initTimeout " , flInitTimeout . String () , "failCount" , failCount )
935+ if isInitFailuresExceeded ( * flInitMaxFailures , initialSyncDone , failCount ) {
936+ log .Error (err , "too many initial sync failures, aborting " , "initMaxFailures " , * flInitMaxFailures , "failCount" , failCount )
941937 os .Exit (1 )
942938 }
943- if * flMaxFailures >= 0 && failCount >= * flMaxFailures {
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 {
944944 // Exit after too many retries, maybe the error is not recoverable.
945945 log .Error (err , "too many failures, aborting" , "failCount" , failCount )
946946 os .Exit (1 )
@@ -1183,12 +1183,14 @@ func chooseWaitTime(period, initPeriod time.Duration, initialSyncDone bool) time
11831183 return period
11841184}
11851185
1186- // isInitTimedOut returns true if the initial sync has exceeded the init-timeout.
1187- func isInitTimedOut (initTimeout time.Duration , initialSyncDone bool , initStart time.Time ) bool {
1188- if initTimeout == 0 || initialSyncDone {
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 {
11891191 return false
11901192 }
1191- return time . Since ( initStart ) >= initTimeout
1193+ return failCount >= initMaxFailures
11921194}
11931195
11941196// Do no work, but don't do something that triggers go's runtime into thinking
@@ -2444,6 +2446,24 @@ CONTRACT
24442446 use as little disk space as possible (see the --depth and --git-gc flags),
24452447 but this is not part of the contract.
24462448
2449+ SYNC PHASES
2450+
2451+ git-sync operates in two phases:
2452+
2453+ Initial sync:
2454+ git-sync retries until its first successful sync with the remote
2455+ repo. During this phase, the retry interval is controlled by
2456+ --init-period (falling back to --period if unset) and the failure
2457+ limit is controlled by --init-max-failures (falling back to
2458+ --max-failures when set to 0). This phase is useful for tolerating
2459+ transient connectivity issues at startup while still giving up
2460+ eventually.
2461+
2462+ Steady state:
2463+ Once the first sync succeeds, git-sync polls the remote at the
2464+ --period interval and tolerates failures up to --max-failures before
2465+ aborting. --init-period and --init-max-failures no longer apply.
2466+
24472467OPTIONS
24482468
24492469 Many options can be specified as either a commandline flag or an environment
@@ -2603,18 +2623,19 @@ OPTIONS
26032623 Enable the pprof debug endpoints on git-sync's HTTP endpoint at
26042624 /debug/pprof. Requires --http-bind to be specified.
26052625
2626+ --init-max-failures <int>, $GITSYNC_INIT_MAX_FAILURES
2627+ The number of consecutive failures allowed before aborting during
2628+ 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.
2632+
26062633 --init-period <duration>, $GITSYNC_INIT_PERIOD
26072634 How long to wait between sync attempts until the first successful
26082635 sync. Once the initial sync succeeds, --period is used instead.
26092636 This must be at least 10ms if set. If not specified, --period is
26102637 used for all sync attempts.
26112638
2612- --init-timeout <duration>, $GITSYNC_INIT_TIMEOUT
2613- The maximum amount of time to keep retrying the initial sync
2614- before failing. If not specified, git-sync retries forever (or
2615- until --max-failures is reached). This must be at least 10ms if
2616- set.
2617-
26182639 --link <string>, $GITSYNC_LINK
26192640 The path to at which to create a symlink which points to the
26202641 current git directory, at the currently synced hash. This may be
0 commit comments