From 78cefb07767628425b85b0b16423761230a34218 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Wed, 29 Jul 2020 15:32:04 +1200 Subject: [PATCH] Fix simultaneous accesses to inflight RequestCache --- src/lib/cache.js | 14 ++++++-------- src/lib/collectors.js | 7 ------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/src/lib/cache.js b/src/lib/cache.js index b683def..fbdb280 100644 --- a/src/lib/cache.js +++ b/src/lib/cache.js @@ -92,7 +92,7 @@ class TtlCache { } /** - * @extends TtlCache + * @extends TtlCache> * @template T */ class RequestCache extends TtlCache { @@ -110,14 +110,12 @@ class RequestCache extends TtlCache { */ getOrFetch(key, callback) { this.cleanKey(key) - if (this.cache.has(key)) return Promise.resolve({result: this.get(key), fromCache: true}) - else { - const pending = callback().then(result => { - this.set(key, result) - return {result, fromCache: false} - }) + if (this.cache.has(key)) { + return this.getWithoutClean(key).then(result => ({result, fromCache: true})) + } else { + const pending = callback() this.set(key, pending) - return pending + return pending.then(result => ({result, fromCache: false})) } } diff --git a/src/lib/collectors.js b/src/lib/collectors.js index 31c70ce..6e23fd1 100644 --- a/src/lib/collectors.js +++ b/src/lib/collectors.js @@ -400,19 +400,12 @@ function fetchShortcodeData(shortcode) { const p = new URLSearchParams() p.set("query_hash", constants.external.shortcode_query_hash) p.set("variables", JSON.stringify({shortcode})) - let status return requestCache.getOrFetchPromise("shortcode/"+shortcode, () => { return switcher.request("post_graphql", `https://www.instagram.com/graphql/query/?${p.toString()}`, async res => { - status = res.status if (res.status === 429) throw constants.symbols.RATE_LIMITED }).then(res => res.json()).then(root => { /** @type {import("./types").TimelineEntryN3} */ const data = root.data.shortcode_media - if (data && !data.__typename) { // empty data? that's really weird. - console.error(`Shortcode request ${shortcode} gave empty data object. status: ${status}, root:`) - console.error(root) - console.error("will just proceed to crash as normal.") - } if (data == null) { // the thing doesn't exist throw constants.symbols.NOT_FOUND