summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJakob Borg <jakob@nym.se>2014-06-20 00:27:54 +0200
committerJakob Borg <jakob@nym.se>2014-06-20 00:28:45 +0200
commitefbdf72d200b3fa94a817d04b93f683689d07d54 (patch)
treecaf0cdf01c9a266216b97837ed97072f62c23067
parent0e59b5678ac4ceb513fa0a45e6f7db93f7678470 (diff)
Lower CPU usage at idle by reducing db polling
-rw-r--r--cmd/syncthing/gui.go13
-rw-r--r--gui/app.js17
-rw-r--r--model/model.go15
-rw-r--r--model/puller.go9
4 files changed, 49 insertions, 5 deletions
diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go
index 02e68a8093..8a57d53253 100644
--- a/cmd/syncthing/gui.go
+++ b/cmd/syncthing/gui.go
@@ -91,6 +91,7 @@ func startGUI(cfg config.GUIConfiguration, assetDir string, m *model.Model) erro
router.Get("/", getRoot)
router.Get("/rest/version", restGetVersion)
router.Get("/rest/model", restGetModel)
+ router.Get("/rest/model/version", restGetModelVersion)
router.Get("/rest/need", restGetNeed)
router.Get("/rest/connections", restGetConnections)
router.Get("/rest/config", restGetConfig)
@@ -144,6 +145,17 @@ func restGetVersion() string {
return Version
}
+func restGetModelVersion(m *model.Model, w http.ResponseWriter, r *http.Request) {
+ var qs = r.URL.Query()
+ var repo = qs.Get("repo")
+ var res = make(map[string]interface{})
+
+ res["version"] = m.Version(repo)
+
+ w.Header().Set("Content-Type", "application/json")
+ json.NewEncoder(w).Encode(res)
+}
+
func restGetModel(m *model.Model, w http.ResponseWriter, r *http.Request) {
var qs = r.URL.Query()
var repo = qs.Get("repo")
@@ -168,6 +180,7 @@ func restGetModel(m *model.Model, w http.ResponseWriter, r *http.Request) {
res["inSyncFiles"], res["inSyncBytes"] = globalFiles-needFiles, globalBytes-needBytes
res["state"] = m.State(repo)
+ res["version"] = m.Version(repo)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(res)
diff --git a/gui/app.js b/gui/app.js
index 222b9b3745..776f5518a2 100644
--- a/gui/app.js
+++ b/gui/app.js
@@ -103,9 +103,20 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http) {
getFailed();
});
Object.keys($scope.repos).forEach(function (id) {
- $http.get(urlbase + '/model?repo=' + encodeURIComponent(id)).success(function (data) {
- $scope.model[id] = data;
- });
+ if (typeof $scope.model[id] === 'undefined') {
+ // Never fetched before
+ $http.get(urlbase + '/model?repo=' + encodeURIComponent(id)).success(function (data) {
+ $scope.model[id] = data;
+ });
+ } else {
+ $http.get(urlbase + '/model/version?repo=' + encodeURIComponent(id)).success(function (data) {
+ if (data.version > $scope.model[id].version) {
+ $http.get(urlbase + '/model?repo=' + encodeURIComponent(id)).success(function (data) {
+ $scope.model[id] = data;
+ });
+ }
+ });
+ }
});
$http.get(urlbase + '/connections').success(function (data) {
var now = Date.now(),
diff --git a/model/model.go b/model/model.go
index 9f581e23a3..c87e5c11b4 100644
--- a/model/model.go
+++ b/model/model.go
@@ -872,3 +872,18 @@ func (m *Model) Override(repo string) {
r.Update(cid.LocalID, fs)
}
+
+// Version returns the change version for the given repository. This is
+// guaranteed to increment if the contents of the local or global repository
+// has changed.
+func (m *Model) Version(repo string) uint64 {
+ var ver uint64
+
+ m.rmut.Lock()
+ for _, n := range m.repoNodes[repo] {
+ ver += m.repoFiles[repo].Changes(m.cm.Get(n))
+ }
+ m.rmut.Unlock()
+
+ return ver
+}
diff --git a/model/puller.go b/model/puller.go
index 77eba070a5..ecb8c9913f 100644
--- a/model/puller.go
+++ b/model/puller.go
@@ -11,6 +11,7 @@ import (
"path/filepath"
"runtime"
"time"
+
"github.com/calmh/syncthing/cid"
"github.com/calmh/syncthing/config"
"github.com/calmh/syncthing/osutil"
@@ -135,6 +136,7 @@ func (p *puller) run() {
walkTicker := time.Tick(time.Duration(p.cfg.Options.RescanIntervalS) * time.Second)
timeout := time.Tick(5 * time.Second)
changed := true
+ var prevVer uint64
for {
// Run the pulling loop as long as there are blocks to fetch
@@ -197,8 +199,11 @@ func (p *puller) run() {
default:
}
- // Queue more blocks to fetch, if any
- p.queueNeededBlocks()
+ if v := p.model.Version(p.repoCfg.ID); v > prevVer {
+ // Queue more blocks to fetch, if any
+ p.queueNeededBlocks()
+ prevVer = v
+ }
}
}