-
Notifications
You must be signed in to change notification settings - Fork 460
add --init-period and --init-max-failures flags #980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+151
−5
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a3f31b
add init-period flag for faster initial sync retries
knQzx 38c837b
address review: alphabetize flags, add readme docs, add e2e tests
knQzx e599c33
move init flags to alphabetical order in man text
knQzx 5c9a247
fix init-timeout e2e test to actually trigger init timeout
knQzx 94c27c0
switch init-timeout to init-max-failures per review
knQzx b34572c
address review: simplify closure, use pflag.Changed for init-max-fail…
knQzx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,6 +186,12 @@ func main() { | |
| flPeriod := pflag.Duration("period", | ||
| envDuration(10*time.Second, "GITSYNC_PERIOD", "GIT_SYNC_PERIOD"), | ||
| "how long to wait between syncs, must be >= 10ms; --wait overrides this") | ||
| flInitPeriod := pflag.Duration("init-period", | ||
| envDuration(0, "GITSYNC_INIT_PERIOD"), | ||
| "how long to wait between sync attempts until the first success, must be >= 10ms if set; if unset, --period is used") | ||
| flInitTimeout := pflag.Duration("init-timeout", | ||
| envDuration(0, "GITSYNC_INIT_TIMEOUT"), | ||
| "the max time allowed for the initial sync to succeed; if unset, there is no timeout (retries forever until --max-failures)") | ||
| flSyncTimeout := pflag.Duration("sync-timeout", | ||
| envDuration(120*time.Second, "GITSYNC_SYNC_TIMEOUT", "GIT_SYNC_SYNC_TIMEOUT"), | ||
| "the total time allowed for one complete sync, must be >= 10ms; --timeout overrides this") | ||
|
|
@@ -469,6 +475,12 @@ func main() { | |
| if *flPeriod < 10*time.Millisecond { | ||
| fatalConfigErrorf(log, true, "invalid flag: --period must be at least 10ms") | ||
| } | ||
| if *flInitPeriod != 0 && *flInitPeriod < 10*time.Millisecond { | ||
| fatalConfigErrorf(log, true, "invalid flag: --init-period must be at least 10ms") | ||
| } | ||
| if *flInitTimeout != 0 && *flInitTimeout < 10*time.Millisecond { | ||
| fatalConfigErrorf(log, true, "invalid flag: --init-timeout must be at least 10ms") | ||
| } | ||
|
|
||
| if *flDeprecatedChmod != 0 { | ||
| fatalConfigErrorf(log, true, "deprecated flag: --change-permissions is no longer supported") | ||
|
|
@@ -912,20 +924,35 @@ func main() { | |
|
|
||
| failCount := 0 | ||
| syncCount := uint64(0) | ||
| initialSyncDone := false | ||
| var initStart time.Time | ||
| if *flInitPeriod != 0 || *flInitTimeout != 0 { | ||
| initStart = time.Now() | ||
| } | ||
| for { | ||
| start := time.Now() | ||
| ctx, cancel := context.WithTimeout(context.Background(), *flSyncTimeout) | ||
|
thockin marked this conversation as resolved.
|
||
|
|
||
| if changed, hash, err := git.SyncRepo(ctx, refreshCreds); err != nil { | ||
| failCount++ | ||
| updateSyncMetrics(metricKeyError, start) | ||
| if isInitTimedOut(*flInitTimeout, initialSyncDone, initStart) { | ||
| log.Error(err, "initial sync timed out", "initTimeout", flInitTimeout.String(), "failCount", failCount) | ||
| os.Exit(1) | ||
| } | ||
| if *flMaxFailures >= 0 && failCount >= *flMaxFailures { | ||
| // Exit after too many retries, maybe the error is not recoverable. | ||
| log.Error(err, "too many failures, aborting", "failCount", failCount) | ||
| os.Exit(1) | ||
| } | ||
| log.Error(err, "error syncing repo, will retry", "failCount", failCount) | ||
| } else { | ||
| if !initialSyncDone { | ||
| initialSyncDone = true | ||
| if *flInitPeriod != 0 { | ||
| log.V(0).Info("initial sync complete, switching to normal period", "initPeriod", flInitPeriod.String(), "period", flPeriod.String()) | ||
| } | ||
| } | ||
| // this might have been called before, but also might not have | ||
| setRepoReady() | ||
| // We treat the first loop as a sync, including sending hooks. | ||
|
|
@@ -989,12 +1016,14 @@ func main() { | |
| log.DeleteErrorFile() | ||
| } | ||
|
|
||
| log.V(3).Info("next sync", "waitTime", flPeriod.String(), "syncCount", syncCount) | ||
| // Use init-period for retries before the first successful sync. | ||
| waitTime := chooseWaitTime(*flPeriod, *flInitPeriod, initialSyncDone) | ||
|
knQzx marked this conversation as resolved.
Outdated
|
||
| log.V(3).Info("next sync", "waitTime", waitTime.String(), "syncCount", syncCount) | ||
| cancel() | ||
|
|
||
| // Sleep until the next sync. If syncSig is set then the sleep may | ||
| // be interrupted by that signal. | ||
| t := time.NewTimer(*flPeriod) | ||
| t := time.NewTimer(waitTime) | ||
| select { | ||
| case <-t.C: | ||
| case <-sigChan: | ||
|
|
@@ -1144,6 +1173,24 @@ func setRepoReady() { | |
| repoReady = true | ||
| } | ||
|
|
||
| // chooseWaitTime returns the appropriate wait duration based on whether the | ||
| // initial sync has completed. If initPeriod is non-zero and the initial sync | ||
| // is not yet done, it returns initPeriod. Otherwise it returns period. | ||
| func chooseWaitTime(period, initPeriod time.Duration, initialSyncDone bool) time.Duration { | ||
| if !initialSyncDone && initPeriod != 0 { | ||
| return initPeriod | ||
| } | ||
| return period | ||
| } | ||
|
|
||
| // isInitTimedOut returns true if the initial sync has exceeded the init-timeout. | ||
| func isInitTimedOut(initTimeout time.Duration, initialSyncDone bool, initStart time.Time) bool { | ||
| if initTimeout == 0 || initialSyncDone { | ||
| return false | ||
| } | ||
| return time.Since(initStart) >= initTimeout | ||
| } | ||
|
|
||
| // Do no work, but don't do something that triggers go's runtime into thinking | ||
| // it is deadlocked. | ||
| func sleepForever() { | ||
|
|
@@ -2589,6 +2636,18 @@ OPTIONS | |
| dynamic credentials from an external secrets system). | ||
| See also $GITSYNC_PASSWORD. | ||
|
|
||
| --init-period <duration>, $GITSYNC_INIT_PERIOD | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please keep this section alphabetical -- these should go earlier
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, please add to README |
||
| How long to wait between sync attempts until the first successful | ||
| sync. Once the initial sync succeeds, --period is used instead. | ||
| This must be at least 10ms if set. If not specified, --period is | ||
| used for all sync attempts. | ||
|
|
||
| --init-timeout <duration>, $GITSYNC_INIT_TIMEOUT | ||
| The maximum amount of time to keep retrying the initial sync | ||
| before failing. If not specified, git-sync retries forever (or | ||
| until --max-failures is reached). This must be at least 10ms if | ||
| set. | ||
|
|
||
| --period <duration>, $GITSYNC_PERIOD | ||
| How long to wait between sync attempts. This must be at least | ||
| 10ms. This flag obsoletes --wait, but if --wait is specified, it | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.