Skip to content

Commit

Permalink
[watch] Debounce rapid file changes
Browse files Browse the repository at this point in the history
Fixes #1004

Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Sep 22, 2018
1 parent 88093f4 commit c93acdc
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions pkg/skaffold/watch/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,45 @@ func (w *watchList) Run(ctx context.Context, pollInterval time.Duration, onChang
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()

changedComponents := map[int]bool{}

for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
changed := 0

for _, component := range *w {
for i, component := range *w {
state, err := stat(component.deps)
if err != nil {
return errors.Wrap(err, "listing files")
}

if hasChanged(component.state, state) {
component.onChange()
changedComponents[i] = true
component.state = state
changed++
}
}

if changed > 0 {
// Rapid file changes that are more frequent than the poll interval would trigger
// multiple rebuilds.
// To prevent that, we debounce changes that happen too quickly
// by waiting for a full turn where nothing happens and trigger a rebuild for
// the accumulated changes.
if changed == 0 && len(changedComponents) > 0 {
for i, component := range *w {
if changedComponents[i] {
component.onChange()
}
}

if err := onChange(); err != nil {
return errors.Wrap(err, "calling final callback")
}

changedComponents = map[int]bool{}
}
}
}
Expand Down

0 comments on commit c93acdc

Please sign in to comment.