folderscan.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "math/rand"
  9. "time"
  10. )
  11. type rescanRequest struct {
  12. subdirs []string
  13. err chan error
  14. }
  15. // bundle all folder scan activity
  16. type folderscan struct {
  17. interval time.Duration
  18. timer *time.Timer
  19. now chan rescanRequest
  20. delay chan time.Duration
  21. }
  22. func (s *folderscan) reschedule() {
  23. if s.interval == 0 {
  24. return
  25. }
  26. // Sleep a random time between 3/4 and 5/4 of the configured interval.
  27. sleepNanos := (s.interval.Nanoseconds()*3 + rand.Int63n(2*s.interval.Nanoseconds())) / 4
  28. interval := time.Duration(sleepNanos) * time.Nanosecond
  29. l.Debugln(s, "next rescan in", interval)
  30. s.timer.Reset(interval)
  31. }
  32. func (s *folderscan) Scan(subdirs []string) error {
  33. req := rescanRequest{
  34. subdirs: subdirs,
  35. err: make(chan error),
  36. }
  37. s.now <- req
  38. return <-req.err
  39. }
  40. func (s *folderscan) Delay(next time.Duration) {
  41. s.delay <- next
  42. }