diff options
author | Kristian Lyngstol <kristian@bohemians.org> | 2016-05-31 21:24:35 +0200 |
---|---|---|
committer | Kristian Lyngstol <kristian@bohemians.org> | 2016-05-31 21:24:35 +0200 |
commit | e12d4aec1153c77fd39db7c163e159d9d53a93a5 (patch) | |
tree | ba724aaab1d3e109ddf1b8c4fe52f95ad4a0288c /web/js/nms-data.js | |
parent | c215b9142b4fb03f18e132391538cd1d1e916d61 (diff) |
api: Fix bad hash-logic, front: Performance and more
Fixes #93
Fixes #92
Fixes #91
The hash logic with FreezeThaw wasn't consistent, which broke a lot of
optimizations. This might be slower (?) but it's consistent which means
we'll make up for it by better cache utilization.
To utilize this I also added delayed JSON parsing (#91) with a big
description of why we want it. This also means that #90 and #89 is a lot
less important. One issue right now is that "time" isn't part of the ETag.
This is both a blessing and a curse. If we are going through time we will
actually use in-app cache (not browser cache - the URL changes). This
makes for nice CPU savings, but ultimately means we can't tell that the
data for two distinct times is identical because we only see one. Oh
well...
On the simpler side, I fixed/improved the clearBox logic to fix the
artifacts. Might need to tune this. I also added some optimizations to
avoid redrawing identical text. This is what pointed out to me that the
hash was broken too: The resizeEvent was triggering all the time, forcing
redraws all the time. This happened because nmsData.switches "updated"
frequently which should only happen if someone adds/moves/removes a switch
or linknet - regardless of time travel. This was caused by the inconsistent
hash causing a false cache miss.
Anyway, if you are still reading this, you almost got a patch with "kjell
magne bondevik uten mellomnavn" as a literal text string... TOO BAD.
Diffstat (limited to 'web/js/nms-data.js')
-rw-r--r-- | web/js/nms-data.js | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/web/js/nms-data.js b/web/js/nms-data.js index 9242313..5a219e8 100644 --- a/web/js/nms-data.js +++ b/web/js/nms-data.js @@ -224,13 +224,31 @@ nmsData._genericUpdater = function(name, cacheok) { heads['Cache-Control'] = "max-age=0, no-cache, stale-while-revalidate=0"; } + /* + * Note that we intentionally set dataType: "text" here. + * + * We can be smarter than jQuery here. We know that the ETag can be + * used to evaluate against our cached copy. If the ETag is a + * match, we never have to do the potentially extensive JSON + * parsing. + * + * Also note that we accept weakened ETags too (ETags with W/ + * prefixed). This is typically if the intermediate cache has + * compressed the content for us, so this is fine. + * + * This is particularly important because we poll everything even + * though we _know_ it will hit both browser cache and most likely + * Varnish. JSON.Parse was one of the biggest CPU hogs before this. + */ $.ajax({ type: "GET", headers: heads, url: this._sources[name].target + now, - dataType: "json", - success: function (data, textStatus, jqXHR) { - if (nmsData[name] == undefined || nmsData[name]['hash'] != data['hash']) { + dataType: "text", + success: function (indata, textStatus, jqXHR) { + let etag = jqXHR.getResponseHeader("ETag"); + if (nmsData[name] == undefined || (nmsData[name]['hash'] != etag && nmsData[name]['hash'] != etag.slice(2))) { + var data = JSON.parse(indata); if (name == "ping") { nmsData._last = data['time']; nmsMap.drawNow(); |