shards: consolidate events before calling scan

We call scan for each fsnotify event. However, multiple events may
happen during a single call to scan, leading to multiple needless calls
to scan. Instead we can consolidate the events from fsnotify so we only
call scan if it isn't running, or only one more time if it is running.

Change-Id: I7b8c3891a202c28357beb8e4bcf74a3c33b3faf0
diff --git a/shards/watcher.go b/shards/watcher.go
index 766f851..0407bad 100644
--- a/shards/watcher.go
+++ b/shards/watcher.go
@@ -134,20 +134,35 @@
 		return err
 	}
 
+	// intermediate signal channel so if there are multiple watcher.Events we
+	// only call scan once.
+	signal := make(chan struct{}, 1)
+
 	go func() {
 		for {
 			select {
 			case <-watcher.Events:
-				s.scan()
+				select {
+				case signal <- struct{}{}:
+				default:
+				}
 			case err := <-watcher.Errors:
 				if err != nil {
 					log.Println("watcher error:", err)
 				}
 			case <-quitter:
 				watcher.Close()
+				close(signal)
 				return
 			}
 		}
 	}()
+
+	go func() {
+		for range signal {
+			s.scan()
+		}
+	}()
+
 	return nil
 }