rofolder.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if debug {
  44. l.Debugln(s, "starting")
  45. defer l.Debugln(s, "exiting")
  46. }
  47. defer func() {
  48. s.timer.Stop()
  49. }()
  50. reschedule := func() {
  51. if s.intv == 0 {
  52. return
  53. }
  54. // Sleep a random time between 3/4 and 5/4 of the configured interval.
  55. sleepNanos := (s.intv.Nanoseconds()*3 + rand.Int63n(2*s.intv.Nanoseconds())) / 4
  56. s.timer.Reset(time.Duration(sleepNanos) * time.Nanosecond)
  57. }
  58. initialScanCompleted := false
  59. for {
  60. select {
  61. case <-s.stop:
  62. return
  63. case <-s.timer.C:
  64. if err := s.model.CheckFolderHealth(s.folder); err != nil {
  65. l.Infoln("Skipping folder", s.folder, "scan due to folder error:", err)
  66. reschedule()
  67. continue
  68. }
  69. if debug {
  70. l.Debugln(s, "rescan")
  71. }
  72. if err := s.model.internalScanFolderSubs(s.folder, nil); err != nil {
  73. // Potentially sets the error twice, once in the scanner just
  74. // by doing a check, and once here, if the error returned is
  75. // the same one as returned by CheckFolderHealth, though
  76. // duplicate set is handled by setError.
  77. s.setError(err)
  78. reschedule()
  79. continue
  80. }
  81. if !initialScanCompleted {
  82. l.Infoln("Completed initial scan (ro) of folder", s.folder)
  83. initialScanCompleted = true
  84. }
  85. if s.intv == 0 {
  86. continue
  87. }
  88. reschedule()
  89. case req := <-s.scanNow:
  90. if err := s.model.CheckFolderHealth(s.folder); err != nil {
  91. l.Infoln("Skipping folder", s.folder, "scan due to folder error:", err)
  92. req.err <- err
  93. continue
  94. }
  95. if debug {
  96. l.Debugln(s, "forced rescan")
  97. }
  98. if err := s.model.internalScanFolderSubs(s.folder, req.subs); err != nil {
  99. // Potentially sets the error twice, once in the scanner just
  100. // by doing a check, and once here, if the error returned is
  101. // the same one as returned by CheckFolderHealth, though
  102. // duplicate set is handled by setError.
  103. s.setError(err)
  104. req.err <- err
  105. continue
  106. }
  107. req.err <- nil
  108. case next := <-s.delayScan:
  109. s.timer.Reset(next)
  110. }
  111. }
  112. }
  113. func (s *roFolder) Stop() {
  114. close(s.stop)
  115. }
  116. func (s *roFolder) IndexUpdated() {
  117. }
  118. func (s *roFolder) Scan(subs []string) error {
  119. req := rescanRequest{
  120. subs: subs,
  121. err: make(chan error),
  122. }
  123. s.scanNow <- req
  124. return <-req.err
  125. }
  126. func (s *roFolder) String() string {
  127. return fmt.Sprintf("roFolder/%s@%p", s.folder, s)
  128. }
  129. func (s *roFolder) BringToFront(string) {}
  130. func (s *roFolder) Jobs() ([]string, []string) {
  131. return nil, nil
  132. }
  133. func (s *roFolder) DelayScan(next time.Duration) {
  134. s.delayScan <- next
  135. }