Lower CPU usage at idle by reducing db polling

This commit is contained in:
Jakob Borg 2014-06-20 00:27:54 +02:00
parent 0e59b5678a
commit efbdf72d20
4 changed files with 49 additions and 5 deletions

View File

@ -91,6 +91,7 @@ func startGUI(cfg config.GUIConfiguration, assetDir string, m *model.Model) erro
router.Get("/", getRoot) router.Get("/", getRoot)
router.Get("/rest/version", restGetVersion) router.Get("/rest/version", restGetVersion)
router.Get("/rest/model", restGetModel) router.Get("/rest/model", restGetModel)
router.Get("/rest/model/version", restGetModelVersion)
router.Get("/rest/need", restGetNeed) router.Get("/rest/need", restGetNeed)
router.Get("/rest/connections", restGetConnections) router.Get("/rest/connections", restGetConnections)
router.Get("/rest/config", restGetConfig) router.Get("/rest/config", restGetConfig)
@ -144,6 +145,17 @@ func restGetVersion() string {
return Version 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) { func restGetModel(m *model.Model, w http.ResponseWriter, r *http.Request) {
var qs = r.URL.Query() var qs = r.URL.Query()
var repo = qs.Get("repo") 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["inSyncFiles"], res["inSyncBytes"] = globalFiles-needFiles, globalBytes-needBytes
res["state"] = m.State(repo) res["state"] = m.State(repo)
res["version"] = m.Version(repo)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)

View File

@ -103,9 +103,20 @@ syncthing.controller('SyncthingCtrl', function ($scope, $http) {
getFailed(); getFailed();
}); });
Object.keys($scope.repos).forEach(function (id) { Object.keys($scope.repos).forEach(function (id) {
if (typeof $scope.model[id] === 'undefined') {
// Never fetched before
$http.get(urlbase + '/model?repo=' + encodeURIComponent(id)).success(function (data) { $http.get(urlbase + '/model?repo=' + encodeURIComponent(id)).success(function (data) {
$scope.model[id] = 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) { $http.get(urlbase + '/connections').success(function (data) {
var now = Date.now(), var now = Date.now(),

View File

@ -872,3 +872,18 @@ func (m *Model) Override(repo string) {
r.Update(cid.LocalID, fs) 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
}

View File

@ -11,6 +11,7 @@ import (
"path/filepath" "path/filepath"
"runtime" "runtime"
"time" "time"
"github.com/calmh/syncthing/cid" "github.com/calmh/syncthing/cid"
"github.com/calmh/syncthing/config" "github.com/calmh/syncthing/config"
"github.com/calmh/syncthing/osutil" "github.com/calmh/syncthing/osutil"
@ -135,6 +136,7 @@ func (p *puller) run() {
walkTicker := time.Tick(time.Duration(p.cfg.Options.RescanIntervalS) * time.Second) walkTicker := time.Tick(time.Duration(p.cfg.Options.RescanIntervalS) * time.Second)
timeout := time.Tick(5 * time.Second) timeout := time.Tick(5 * time.Second)
changed := true changed := true
var prevVer uint64
for { for {
// Run the pulling loop as long as there are blocks to fetch // Run the pulling loop as long as there are blocks to fetch
@ -197,8 +199,11 @@ func (p *puller) run() {
default: default:
} }
if v := p.model.Version(p.repoCfg.ID); v > prevVer {
// Queue more blocks to fetch, if any // Queue more blocks to fetch, if any
p.queueNeededBlocks() p.queueNeededBlocks()
prevVer = v
}
} }
} }