rofolder.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright (C) 2014 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. "fmt"
  9. "math/rand"
  10. "time"
  11. "github.com/syncthing/syncthing/lib/sync"
  12. )
  13. type roFolder struct {
  14. stateTracker
  15. folder string
  16. intv time.Duration
  17. timer *time.Timer
  18. model *Model
  19. stop chan struct{}
  20. scanNow chan rescanRequest
  21. delayScan chan time.Duration
  22. }
  23. type rescanRequest struct {
  24. subs []string
  25. err chan error
  26. }
  27. func newROFolder(model *Model, folder string, interval time.Duration) *roFolder {
  28. return &roFolder{
  29. stateTracker: stateTracker{
  30. folder: folder,
  31. mut: sync.NewMutex(),
  32. },
  33. folder: folder,
  34. intv: interval,
  35. timer: time.NewTimer(time.Millisecond),
  36. model: model,
  37. stop: make(chan struct{}),
  38. scanNow: make(chan rescanRequest),
  39. delayScan: make(chan time.Duration),
  40. }
  41. }
  42. func (s *roFolder) Serve() {
  43. l.Debugln(s, "starting")
  44. defer l.Debugln(s, "exiting")
  45. defer func() {
  46. s.timer.Stop()
  47. }()
  48. reschedule := func() {
  49. if s.intv == 0 {
  50. return
  51. }
  52. // Sleep a random time between 3/4 and 5/4 of the configured interval.
  53. sleepNanos := (s.intv.Nanoseconds()*3 + rand.Int63n(2*s.intv.Nanoseconds())) / 4
  54. s.timer.Reset(time.Duration(sleepNanos) * time.Nanosecond)
  55. }
  56. initialScanCompleted := false
  57. for {
  58. select {
  59. case <-s.stop:
  60. return
  61. case <-s.timer.C:
  62. if err := s.model.CheckFolderHealth(s.folder); err != nil {
  63. l.Infoln("Skipping folder", s.folder, "scan due to folder error:", err)
  64. reschedule()
  65. continue
  66. }
  67. l.Debugln(s, "rescan")
  68. if err := s.model.internalScanFolderSubs(s.folder, nil); err != nil {
  69. // Potentially sets the error twice, once in the scanner just
  70. // by doing a check, and once here, if the error returned is
  71. // the same one as returned by CheckFolderHealth, though
  72. // duplicate set is handled by setError.
  73. s.setError(err)
  74. reschedule()
  75. continue
  76. }
  77. if !initialScanCompleted {
  78. l.Infoln("Completed initial scan (ro) of folder", s.folder)
  79. initialScanCompleted = true
  80. }
  81. if s.intv == 0 {
  82. continue
  83. }
  84. reschedule()
  85. case req := <-s.scanNow:
  86. if err := s.model.CheckFolderHealth(s.folder); err != nil {
  87. l.Infoln("Skipping folder", s.folder, "scan due to folder error:", err)
  88. req.err <- err
  89. continue
  90. }
  91. l.Debugln(s, "forced rescan")
  92. if err := s.model.internalScanFolderSubs(s.folder, req.subs); err != nil {
  93. // Potentially sets the error twice, once in the scanner just
  94. // by doing a check, and once here, if the error returned is
  95. // the same one as returned by CheckFolderHealth, though
  96. // duplicate set is handled by setError.
  97. s.setError(err)
  98. req.err <- err
  99. continue
  100. }
  101. req.err <- nil
  102. case next := <-s.delayScan:
  103. s.timer.Reset(next)
  104. }
  105. }
  106. }
  107. func (s *roFolder) Stop() {
  108. close(s.stop)
  109. }
  110. func (s *roFolder) IndexUpdated() {
  111. }
  112. func (s *roFolder) Scan(subs []string) error {
  113. req := rescanRequest{
  114. subs: subs,
  115. err: make(chan error),
  116. }
  117. s.scanNow <- req
  118. return <-req.err
  119. }
  120. func (s *roFolder) String() string {
  121. return fmt.Sprintf("roFolder/%s@%p", s.folder, s)
  122. }
  123. func (s *roFolder) BringToFront(string) {}
  124. func (s *roFolder) Jobs() ([]string, []string) {
  125. return nil, nil
  126. }
  127. func (s *roFolder) DelayScan(next time.Duration) {
  128. s.delayScan <- next
  129. }