lib/model: Refactor {ro,rw}folder serve loop into just folder (#4939)

The actual pull method (which is really the only thing that differs
between them) is now an interface member which gets overridden by the
subclass.

"Subclass?!" Well, this is dynamic dispatch with overriding, I guess.
This commit is contained in:
Jakob Borg
2018-05-11 10:45:13 +02:00
committed by GitHub
parent a2f51c85c2
commit 344e52e311
3 changed files with 119 additions and 166 deletions

View File

@@ -25,47 +25,11 @@ type sendOnlyFolder struct {
}
func newSendOnlyFolder(model *Model, cfg config.FolderConfiguration, _ versioner.Versioner, _ fs.Filesystem) service {
return &sendOnlyFolder{folder: newFolder(model, cfg)}
}
func (f *sendOnlyFolder) Serve() {
l.Debugln(f, "starting")
defer l.Debugln(f, "exiting")
defer func() {
f.scan.timer.Stop()
}()
if f.FSWatcherEnabled && f.CheckHealth() == nil {
f.startWatch()
}
for {
select {
case <-f.ctx.Done():
return
case <-f.pullScheduled:
f.pull()
case <-f.restartWatchChan:
f.restartWatch()
case <-f.scan.timer.C:
l.Debugln(f, "Scanning subdirectories")
f.scanTimerFired()
case req := <-f.scan.now:
req.err <- f.scanSubdirs(req.subdirs)
case next := <-f.scan.delay:
f.scan.timer.Reset(next)
case fsEvents := <-f.watchChan:
l.Debugln(f, "filesystem notification rescan")
f.scanSubdirs(fsEvents)
}
f := &sendOnlyFolder{
folder: newFolder(model, cfg),
}
f.folder.puller = f
return f
}
func (f *sendOnlyFolder) String() string {
@@ -77,12 +41,12 @@ func (f *sendOnlyFolder) PullErrors() []FileError {
}
// pull checks need for files that only differ by metadata (no changes on disk)
func (f *sendOnlyFolder) pull() {
func (f *sendOnlyFolder) pull() bool {
select {
case <-f.initialScanFinished:
default:
// Once the initial scan finished, a pull will be scheduled
return
return false
}
f.model.fmut.RLock()
@@ -133,4 +97,6 @@ func (f *sendOnlyFolder) pull() {
if len(batch) > 0 {
f.model.updateLocalsFromPulling(f.folderID, batch)
}
return true
}