@@ -186,6 +186,12 @@ func main() {
186186 flPeriod := pflag .Duration ("period" ,
187187 envDuration (10 * time .Second , "GITSYNC_PERIOD" , "GIT_SYNC_PERIOD" ),
188188 "how long to wait between syncs, must be >= 10ms; --wait overrides this" )
189+ flInitPeriod := pflag .Duration ("init-period" ,
190+ envDuration (0 , "GITSYNC_INIT_PERIOD" ),
191+ "how long to wait between sync attempts until the first success, must be >= 10ms if set; if unset, --period is used" )
192+ flInitTimeout := pflag .Duration ("init-timeout" ,
193+ envDuration (0 , "GITSYNC_INIT_TIMEOUT" ),
194+ "the max time allowed for the initial sync to succeed; if unset, there is no timeout (retries forever until --max-failures)" )
189195 flSyncTimeout := pflag .Duration ("sync-timeout" ,
190196 envDuration (120 * time .Second , "GITSYNC_SYNC_TIMEOUT" , "GIT_SYNC_SYNC_TIMEOUT" ),
191197 "the total time allowed for one complete sync, must be >= 10ms; --timeout overrides this" )
@@ -469,6 +475,12 @@ func main() {
469475 if * flPeriod < 10 * time .Millisecond {
470476 fatalConfigErrorf (log , true , "invalid flag: --period must be at least 10ms" )
471477 }
478+ if * flInitPeriod != 0 && * flInitPeriod < 10 * time .Millisecond {
479+ fatalConfigErrorf (log , true , "invalid flag: --init-period must be at least 10ms" )
480+ }
481+ if * flInitTimeout != 0 && * flInitTimeout < 10 * time .Millisecond {
482+ fatalConfigErrorf (log , true , "invalid flag: --init-timeout must be at least 10ms" )
483+ }
472484
473485 if * flDeprecatedChmod != 0 {
474486 fatalConfigErrorf (log , true , "deprecated flag: --change-permissions is no longer supported" )
@@ -912,20 +924,35 @@ func main() {
912924
913925 failCount := 0
914926 syncCount := uint64 (0 )
927+ initialSyncDone := false
928+ var initStart time.Time
929+ if * flInitPeriod != 0 || * flInitTimeout != 0 {
930+ initStart = time .Now ()
931+ }
915932 for {
916933 start := time .Now ()
917934 ctx , cancel := context .WithTimeout (context .Background (), * flSyncTimeout )
918935
919936 if changed , hash , err := git .SyncRepo (ctx , refreshCreds ); err != nil {
920937 failCount ++
921938 updateSyncMetrics (metricKeyError , start )
939+ if isInitTimedOut (* flInitTimeout , initialSyncDone , initStart ) {
940+ log .Error (err , "initial sync timed out" , "initTimeout" , flInitTimeout .String (), "failCount" , failCount )
941+ os .Exit (1 )
942+ }
922943 if * flMaxFailures >= 0 && failCount >= * flMaxFailures {
923944 // Exit after too many retries, maybe the error is not recoverable.
924945 log .Error (err , "too many failures, aborting" , "failCount" , failCount )
925946 os .Exit (1 )
926947 }
927948 log .Error (err , "error syncing repo, will retry" , "failCount" , failCount )
928949 } else {
950+ if ! initialSyncDone {
951+ initialSyncDone = true
952+ if * flInitPeriod != 0 {
953+ log .V (0 ).Info ("initial sync complete, switching to normal period" , "initPeriod" , flInitPeriod .String (), "period" , flPeriod .String ())
954+ }
955+ }
929956 // this might have been called before, but also might not have
930957 setRepoReady ()
931958 // We treat the first loop as a sync, including sending hooks.
@@ -989,12 +1016,14 @@ func main() {
9891016 log .DeleteErrorFile ()
9901017 }
9911018
992- log .V (3 ).Info ("next sync" , "waitTime" , flPeriod .String (), "syncCount" , syncCount )
1019+ // Use init-period for retries before the first successful sync.
1020+ waitTime := chooseWaitTime (* flPeriod , * flInitPeriod , initialSyncDone )
1021+ log .V (3 ).Info ("next sync" , "waitTime" , waitTime .String (), "syncCount" , syncCount )
9931022 cancel ()
9941023
9951024 // Sleep until the next sync. If syncSig is set then the sleep may
9961025 // be interrupted by that signal.
997- t := time .NewTimer (* flPeriod )
1026+ t := time .NewTimer (waitTime )
9981027 select {
9991028 case <- t .C :
10001029 case <- sigChan :
@@ -1144,6 +1173,24 @@ func setRepoReady() {
11441173 repoReady = true
11451174}
11461175
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+ // 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 {
1189+ return false
1190+ }
1191+ return time .Since (initStart ) >= initTimeout
1192+ }
1193+
11471194// Do no work, but don't do something that triggers go's runtime into thinking
11481195// it is deadlocked.
11491196func sleepForever () {
@@ -2589,6 +2636,18 @@ OPTIONS
25892636 dynamic credentials from an external secrets system).
25902637 See also $GITSYNC_PASSWORD.
25912638
2639+ --init-period <duration>, $GITSYNC_INIT_PERIOD
2640+ How long to wait between sync attempts until the first successful
2641+ sync. Once the initial sync succeeds, --period is used instead.
2642+ This must be at least 10ms if set. If not specified, --period is
2643+ used for all sync attempts.
2644+
2645+ --init-timeout <duration>, $GITSYNC_INIT_TIMEOUT
2646+ The maximum amount of time to keep retrying the initial sync
2647+ before failing. If not specified, git-sync retries forever (or
2648+ until --max-failures is reached). This must be at least 10ms if
2649+ set.
2650+
25922651 --period <duration>, $GITSYNC_PERIOD
25932652 How long to wait between sync attempts. This must be at least
25942653 10ms. This flag obsoletes --wait, but if --wait is specified, it
0 commit comments