From 663c0964003e2450e3fb32c61eb62f8cb30bdfc0 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 8 Apr 2019 12:24:00 -0600 Subject: [PATCH] Cache failed capabilities lookups for shorter amounts of time This should fix https://github.com/vector-im/riot-web/issues/9225 for showing up too often/too long. --- src/client.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/client.js b/src/client.js index e8b8c1cf8..1b3b65baf 100644 --- a/src/client.js +++ b/src/client.js @@ -445,9 +445,10 @@ MatrixClient.prototype.setNotifTimelineSet = function(notifTimelineSet) { * @return {module:http-api.MatrixError} Rejects: with an error response. */ MatrixClient.prototype.getCapabilities = function() { + const now = new Date().getTime(); + if (this._cachedCapabilities) { - const now = new Date().getTime(); - if (now - this._cachedCapabilities.lastUpdated <= CAPABILITIES_CACHE_MS) { + if (now < this._cachedCapabilities.expiration) { console.log("Returning cached capabilities"); return Promise.resolve(this._cachedCapabilities.capabilities); } @@ -459,9 +460,16 @@ MatrixClient.prototype.getCapabilities = function() { ).catch(() => null).then((r) => { if (!r) r = {}; const capabilities = r["capabilities"] || {}; + + // If the capabilities missed the cache, cache it for a shorter amount + // of time to try and refresh them later. + const cacheMs = Object.keys(capabilities).length + ? CAPABILITIES_CACHE_MS + : 60000 + (Math.random() * 5000); + this._cachedCapabilities = { capabilities: capabilities, - lastUpdated: new Date().getTime(), + expiration: now + cacheMs, }; console.log("Caching capabilities: ", capabilities);