| 263 | } |
| 264 | |
| 265 | func (d *Downstream) getWatchState() map[string]*remote.Change { |
| 266 | d.changesMutex.Lock() |
| 267 | defer d.changesMutex.Unlock() |
| 268 | |
| 269 | var ( |
| 270 | now = time.Now() |
| 271 | shouldRescan = d.watchedFiles == nil || d.lastRescan == nil || d.lastRescan.Add(rescanPeriod).Before(now) |
| 272 | changeAmount = len(d.changes) |
| 273 | ) |
| 274 | |
| 275 | if changeAmount > 100 || shouldRescan { |
| 276 | // we rescan so reset all changes |
| 277 | d.changes = map[string]bool{} |
| 278 | d.lastRescan = &now |
| 279 | |
| 280 | newState := make(map[string]*remote.Change) |
| 281 | walkDir(d.options.RemotePath, d.options.RemotePath, d.ignoreMatcher, newState, d.options.NoRecursiveWatch, 0) |
| 282 | return newState |
| 283 | } else if changeAmount == 0 { |
| 284 | return nil |
| 285 | } |
| 286 | |
| 287 | // copy state from old |
| 288 | newState := copyState(d.watchedFiles) |
| 289 | |
| 290 | // copy changes |
| 291 | changes := []string{} |
| 292 | for k := range d.changes { |
| 293 | changes = append(changes, k) |
| 294 | } |
| 295 | d.changes = map[string]bool{} |
| 296 | |
| 297 | // apply changes |
| 298 | for _, change := range changes { |
| 299 | d.applyChange(newState, change) |
| 300 | } |
| 301 | |
| 302 | return newState |
| 303 | } |
| 304 | |
| 305 | // Changes retrieves all changes from the watch path |
| 306 | func (d *Downstream) Changes(empty *remote.Empty, stream remote.Downstream_ChangesServer) error { |