From 8b1fada052b65988977e1425b486e06f3b2c39f8 Mon Sep 17 00:00:00 2001 From: ChaituVR Date: Thu, 3 Aug 2023 01:15:32 +0530 Subject: [PATCH 1/2] Replace EE with requestDeduplicator --- src/graphql/operations/votes.ts | 2 +- src/helpers/ee.ts | 20 -------------------- src/helpers/requestDeduplicator.ts | 22 ++++++++++++++++++++++ 3 files changed, 23 insertions(+), 21 deletions(-) delete mode 100644 src/helpers/ee.ts create mode 100644 src/helpers/requestDeduplicator.ts diff --git a/src/graphql/operations/votes.ts b/src/graphql/operations/votes.ts index fcf058d2..1036abfc 100644 --- a/src/graphql/operations/votes.ts +++ b/src/graphql/operations/votes.ts @@ -1,7 +1,7 @@ import graphqlFields from 'graphql-fields'; import db from '../../helpers/mysql'; import { buildWhereQuery, checkLimits, formatProposal, formatSpace, formatVote } from '../helpers'; -import serve from '../../helpers/ee'; +import serve from '../../helpers/requestDeduplicator'; import log from '../../helpers/log'; import { capture } from '@snapshot-labs/snapshot-sentry'; diff --git a/src/helpers/ee.ts b/src/helpers/ee.ts deleted file mode 100644 index fc227a0f..00000000 --- a/src/helpers/ee.ts +++ /dev/null @@ -1,20 +0,0 @@ -import events from 'events'; -import { sha256 } from './utils'; - -const eventEmitter = new events.EventEmitter(); -eventEmitter.setMaxListeners(1000); // https://stackoverflow.com/a/26176922 - -export default async function serve(id, action, args) { - const key = sha256(id); - // eslint-disable-next-line no-async-promise-executor - return new Promise(async (resolve, reject) => { - eventEmitter.once(key, data => (data.error ? reject(data.e) : resolve(data))); - if (eventEmitter.listenerCount(key) === 1) { - try { - eventEmitter.emit(key, await action(...args)); - } catch (e) { - eventEmitter.emit(key, { error: true, e }); - } - } - }); -} diff --git a/src/helpers/requestDeduplicator.ts b/src/helpers/requestDeduplicator.ts new file mode 100644 index 00000000..2cfe431a --- /dev/null +++ b/src/helpers/requestDeduplicator.ts @@ -0,0 +1,22 @@ +import { sha256 } from './utils'; + +const ongoingRequests = new Map(); + +export default async function serve(id, action, args) { + const key = sha256(id); + if (!ongoingRequests.has(key)) { + const requestPromise = action(...args) + .then(result => { + ongoingRequests.delete(key); + return result; + }) + .catch(e => { + console.log('EventEmitter Error', e); + ongoingRequests.delete(key); + throw { error: true, e }; + }); + ongoingRequests.set(key, requestPromise); + } + + return ongoingRequests.get(key); +} From 60a151cf349a73a8e80bdb579e3b82ceae2de599 Mon Sep 17 00:00:00 2001 From: Chaitanya Date: Fri, 4 Aug 2023 00:13:48 +0530 Subject: [PATCH 2/2] Update src/helpers/requestDeduplicator.ts --- src/helpers/requestDeduplicator.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/helpers/requestDeduplicator.ts b/src/helpers/requestDeduplicator.ts index 2cfe431a..55e21964 100644 --- a/src/helpers/requestDeduplicator.ts +++ b/src/helpers/requestDeduplicator.ts @@ -11,9 +11,8 @@ export default async function serve(id, action, args) { return result; }) .catch(e => { - console.log('EventEmitter Error', e); ongoingRequests.delete(key); - throw { error: true, e }; + throw e; }); ongoingRequests.set(key, requestPromise); }