Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1767 from weaveworks/user-defined-git-timeout-all…
Browse files Browse the repository at this point in the history
…-ops

Apply user defined Git timeout on all operations
  • Loading branch information
hiddeco authored Feb 26, 2019
2 parents 27a89fa + dc17b51 commit 4d4ba8f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
1 change: 1 addition & 0 deletions cmd/fluxd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ func main() {
LoopVars: &daemon.LoopVars{
SyncInterval: *syncInterval,
RegistryPollInterval: *registryPollInterval,
GitOpTimeout: *gitTimeout,
},
}

Expand Down
2 changes: 1 addition & 1 deletion daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ func mockDaemon(t *testing.T) (*Daemon, func(), func(), *cluster.Mock, *mockEven
JobStatusCache: &job.StatusCache{Size: 100},
EventWriter: events,
Logger: logger,
LoopVars: &LoopVars{},
LoopVars: &LoopVars{GitOpTimeout: 5 * time.Second},
}

start := func() {
Expand Down
24 changes: 10 additions & 14 deletions daemon/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ import (
"github.com/weaveworks/flux/update"
)

const (
// Timeout for git operations we're prepared to abandon
gitOpTimeout = 15 * time.Second
)

type LoopVars struct {
SyncInterval time.Duration
RegistryPollInterval time.Duration
GitOpTimeout time.Duration

initOnce sync.Once
syncSoon chan struct{}
Expand Down Expand Up @@ -95,7 +91,7 @@ func (d *Daemon) Loop(stop chan struct{}, wg *sync.WaitGroup, logger log.Logger)
case <-syncTimer.C:
d.AskForSync()
case <-d.Repo.C:
ctx, cancel := context.WithTimeout(context.Background(), gitOpTimeout)
ctx, cancel := context.WithTimeout(context.Background(), d.GitOpTimeout)
newSyncHead, err := d.Repo.Revision(ctx, d.GitConfig.Branch)
cancel()
if err != nil {
Expand Down Expand Up @@ -123,7 +119,7 @@ func (d *Daemon) Loop(stop chan struct{}, wg *sync.WaitGroup, logger log.Logger)
jobLogger.Log("state", "done", "success", "false", "err", err)
} else {
jobLogger.Log("state", "done", "success", "true")
ctx, cancel := context.WithTimeout(context.Background(), gitOpTimeout)
ctx, cancel := context.WithTimeout(context.Background(), d.GitOpTimeout)
err := d.Repo.Refresh(ctx)
if err != nil {
logger.Log("err", err)
Expand Down Expand Up @@ -170,7 +166,7 @@ func (d *Daemon) doSync(logger log.Logger, lastKnownSyncTagRev *string, warnedAb
var working *git.Checkout
{
var err error
ctx, cancel := context.WithTimeout(ctx, gitOpTimeout)
ctx, cancel := context.WithTimeout(ctx, d.GitOpTimeout)
defer cancel()
working, err = d.Repo.Clone(ctx, d.GitConfig)
if err != nil {
Expand Down Expand Up @@ -228,7 +224,7 @@ func (d *Daemon) doSync(logger log.Logger, lastKnownSyncTagRev *string, warnedAb
var commits []git.Commit
{
var err error
ctx, cancel := context.WithTimeout(ctx, gitOpTimeout)
ctx, cancel := context.WithTimeout(ctx, d.GitOpTimeout)
if oldTagRev != "" {
commits, err = d.Repo.CommitsBetween(ctx, oldTagRev, newTagRev, d.GitConfig.Paths...)
} else {
Expand All @@ -248,7 +244,7 @@ func (d *Daemon) doSync(logger log.Logger, lastKnownSyncTagRev *string, warnedAb
// no synctag, We are syncing everything from scratch
changedResources = allResources
} else {
ctx, cancel := context.WithTimeout(ctx, gitOpTimeout)
ctx, cancel := context.WithTimeout(ctx, d.GitOpTimeout)
changedFiles, err := working.ChangedFiles(ctx, oldTagRev)
if err == nil && len(changedFiles) > 0 {
// We had some changed files, we're syncing a diff
Expand All @@ -268,7 +264,7 @@ func (d *Daemon) doSync(logger log.Logger, lastKnownSyncTagRev *string, warnedAb

var notes map[string]struct{}
{
ctx, cancel := context.WithTimeout(ctx, gitOpTimeout)
ctx, cancel := context.WithTimeout(ctx, d.GitOpTimeout)
notes, err = working.NoteRevList(ctx)
cancel()
if err != nil {
Expand All @@ -291,7 +287,7 @@ func (d *Daemon) doSync(logger log.Logger, lastKnownSyncTagRev *string, warnedAb
includes[event.NoneOfTheAbove] = true
continue
}
ctx, cancel := context.WithTimeout(ctx, gitOpTimeout)
ctx, cancel := context.WithTimeout(ctx, d.GitOpTimeout)
var n note
ok, err := working.GetNote(ctx, commits[i].Revision, &n)
cancel()
Expand Down Expand Up @@ -426,7 +422,7 @@ func (d *Daemon) doSync(logger log.Logger, lastKnownSyncTagRev *string, warnedAb
// Move the tag and push it so we know how far we've gotten.
if oldTagRev != newTagRev {
{
ctx, cancel := context.WithTimeout(ctx, gitOpTimeout)
ctx, cancel := context.WithTimeout(ctx, d.GitOpTimeout)
err := working.MoveSyncTagAndPush(ctx, newTagRev, "Sync pointer")
cancel()
if err != nil {
Expand All @@ -436,7 +432,7 @@ func (d *Daemon) doSync(logger log.Logger, lastKnownSyncTagRev *string, warnedAb
}
logger.Log("tag", d.GitConfig.SyncTag, "old", oldTagRev, "new", newTagRev)
{
ctx, cancel := context.WithTimeout(ctx, gitOpTimeout)
ctx, cancel := context.WithTimeout(ctx, d.GitOpTimeout)
err := d.Repo.Refresh(ctx)
cancel()
return err
Expand Down
2 changes: 1 addition & 1 deletion daemon/loop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func daemon(t *testing.T) (*Daemon, func()) {
JobStatusCache: &job.StatusCache{Size: 100},
EventWriter: events,
Logger: log.NewLogfmtLogger(os.Stdout),
LoopVars: &LoopVars{},
LoopVars: &LoopVars{GitOpTimeout: 5 * time.Second},
}
return d, func() {
close(shutdown)
Expand Down

0 comments on commit 4d4ba8f

Please sign in to comment.