diff --git a/lib/model/folder.go b/lib/model/folder.go index 1779aa288..69895bca4 100644 --- a/lib/model/folder.go +++ b/lib/model/folder.go @@ -25,6 +25,7 @@ import ( "github.com/syncthing/syncthing/lib/osutil" "github.com/syncthing/syncthing/lib/protocol" "github.com/syncthing/syncthing/lib/scanner" + "github.com/syncthing/syncthing/lib/stats" "github.com/syncthing/syncthing/lib/sync" "github.com/syncthing/syncthing/lib/watchaggregator" ) @@ -37,6 +38,8 @@ var errWatchNotStarted = errors.New("not started") type folder struct { stateTracker config.FolderConfiguration + *stats.FolderStatisticsReference + localFlags uint32 model *model @@ -79,8 +82,9 @@ func newFolder(model *model, fset *db.FileSet, ignores *ignore.Matcher, cfg conf ctx, cancel := context.WithCancel(context.Background()) return folder{ - stateTracker: newStateTracker(cfg.ID), - FolderConfiguration: cfg, + stateTracker: newStateTracker(cfg.ID), + FolderConfiguration: cfg, + FolderStatisticsReference: stats.NewFolderStatisticsReference(model.db, cfg.ID), model: model, shortID: model.shortID, @@ -532,7 +536,7 @@ func (f *folder) scanSubdirs(subDirs []string) error { return err } - f.model.folderStatRef(f.ID).ScanCompleted() + f.ScanCompleted() f.setState(FolderIdle) return nil } diff --git a/lib/model/folder_sendrecv.go b/lib/model/folder_sendrecv.go index ad211cb3f..60c152d94 100644 --- a/lib/model/folder_sendrecv.go +++ b/lib/model/folder_sendrecv.go @@ -1625,7 +1625,7 @@ func (f *sendReceiveFolder) dbUpdaterRoutine(dbUpdateChan <-chan dbUpdateJob) { f.model.updateLocalsFromPulling(f.folderID, files) if found { - f.model.receivedFile(f.folderID, lastFile) + f.ReceivedFile(lastFile.Name, lastFile.IsDeleted()) found = false } diff --git a/lib/model/model.go b/lib/model/model.go index ce6d05017..920a37dbc 100644 --- a/lib/model/model.go +++ b/lib/model/model.go @@ -65,6 +65,7 @@ type service interface { CheckHealth() error Errors() []FileError WatchError() error + GetStatistics() stats.FolderStatistics getState() (folderState, time.Time, error) setState(state folderState) @@ -148,7 +149,6 @@ type model struct { folderIgnores map[string]*ignore.Matcher // folder -> matcher object folderRunners map[string]service // folder -> puller or scanner folderRunnerTokens map[string][]suture.ServiceToken // folder -> tokens for puller or scanner - folderStatRefs map[string]*stats.FolderStatisticsReference // folder -> statsRef folderRestartMuts syncMutexMap // folder -> restart mutex pmut sync.RWMutex // protects the below @@ -208,7 +208,6 @@ func NewModel(cfg config.Wrapper, id protocol.DeviceID, clientName, clientVersio folderIgnores: make(map[string]*ignore.Matcher), folderRunners: make(map[string]service), folderRunnerTokens: make(map[string][]suture.ServiceToken), - folderStatRefs: make(map[string]*stats.FolderStatisticsReference), conn: make(map[protocol.DeviceID]connections.Connection), connRequestLimiters: make(map[protocol.DeviceID]*byteSemaphore), closed: make(map[protocol.DeviceID]chan struct{}), @@ -423,7 +422,6 @@ func (m *model) tearDownFolderLocked(cfg config.FolderConfiguration, err error) delete(m.folderIgnores, cfg.ID) delete(m.folderRunners, cfg.ID) delete(m.folderRunnerTokens, cfg.ID) - delete(m.folderStatRefs, cfg.ID) } func (m *model) RestartFolder(from, to config.FolderConfiguration) { @@ -651,8 +649,10 @@ func (m *model) DeviceStatistics() map[string]stats.DeviceStatistics { // FolderStatistics returns statistics about each folder func (m *model) FolderStatistics() map[string]stats.FolderStatistics { res := make(map[string]stats.FolderStatistics) - for id := range m.cfg.Folders() { - res[id] = m.folderStatRef(id).GetStatistics() + m.fmut.RLock() + defer m.fmut.RUnlock() + for id, runner := range m.folderRunners { + res[id] = runner.GetStatistics() } return res } @@ -1869,22 +1869,6 @@ func (m *model) deviceWasSeen(deviceID protocol.DeviceID) { m.deviceStatRef(deviceID).WasSeen() } -func (m *model) folderStatRef(folder string) *stats.FolderStatisticsReference { - m.fmut.Lock() - defer m.fmut.Unlock() - - sr, ok := m.folderStatRefs[folder] - if !ok { - sr = stats.NewFolderStatisticsReference(m.db, folder) - m.folderStatRefs[folder] = sr - } - return sr -} - -func (m *model) receivedFile(folder string, file protocol.FileInfo) { - m.folderStatRef(folder).ReceivedFile(file.Name, file.IsDeleted()) -} - func sendIndexes(conn protocol.Connection, folder string, fs *db.FileSet, prevSequence int64, dropSymlinks bool) { deviceID := conn.ID() var err error