From 151dbbcd0c7471bb27363f206ad905da6cf47806 Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Fri, 27 Apr 2018 14:36:18 +0100 Subject: [PATCH] Make sync run immediately once git is ready Now that git mirroring is done in its own goroutine, something has to trigger the sync loop to do a sync. A "cell" (channel with buffer size 1) is used to prompt the sync loop every time the repo fetches from upstream. However, this only happens `git-poll-interval` _after_ the mirroring has started, and most of the time when the sync loop starts, the git repo isn't ready. That means the first successful sync, for a freshly-configured fluxd, is usually five minutes after starting. To compensate, this commit makes the repo signal on the cell when it moves to the ready state. --- git/repo.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/git/repo.go b/git/repo.go index ed03e7c31..456a82539 100644 --- a/git/repo.go +++ b/git/repo.go @@ -128,6 +128,14 @@ func (r *Repo) Notify() { } } +// refreshed indicates that the repo has successfully fetched from upstream. +func (r *Repo) refreshed() { + select { + case r.C <- struct{}{}: + default: + } +} + // errorIfNotReady returns the appropriate error if the repo is not // ready, and `nil` otherwise. func (r *Repo) errorIfNotReady() error { @@ -222,6 +230,9 @@ func (r *Repo) Start(shutdown <-chan struct{}, done *sync.WaitGroup) error { cancel() if err == nil { r.setStatus(RepoReady, nil) + // Treat every transition to ready as a refresh, so + // that any listeners can respond in the same way. + r.refreshed() continue // with new status, skipping timer } r.setStatus(RepoCloned, err) @@ -257,10 +268,7 @@ func (r *Repo) Refresh(ctx context.Context) error { if err := r.fetch(ctx); err != nil { return err } - select { - case r.C <- struct{}{}: - default: - } + r.refreshed() return nil }