From 6de9d209bc92cf9a43489c2f93cd4d3190525a7c Mon Sep 17 00:00:00 2001 From: Ryan Ghods Date: Tue, 13 Feb 2018 09:11:01 -0800 Subject: [PATCH] Better throttling logic --- modules/ipc/methods/base.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/modules/ipc/methods/base.js b/modules/ipc/methods/base.js index d9f8cf018..b92981594 100644 --- a/modules/ipc/methods/base.js +++ b/modules/ipc/methods/base.js @@ -25,10 +25,17 @@ module.exports = class BaseProcessor { 'eth_accounts' ]; - this.throttleMethods = [ - 'eth_getStorageAt', - 'eth_getFilterChanges', - ]; + this.throttleTimeout = { + 'eth_getFilterChanges': 5000, + 'eth_getStorageAt': 5000, + 'eth_syncing': 2000, + }; + + this.lastThrottleBypass = { + 'eth_getStorageAt': 1, + 'eth_getFilterChanges': 1, + 'eth_syncing': 1, + }; } @@ -69,15 +76,17 @@ module.exports = class BaseProcessor { } _throttle(payload, conn) { - // throttle by immediately returning every 2 calls - if (this.throttleMethods.includes(payload.method) && payload.id % 20 !== 1) { - console.log('throttling', payload.method, payload.id); - - if (payload.method === 'eth_getFilterChanges') { + if (this.lastThrottleBypass[payload.method]) { + console.log(payload.method, Date.now(), this.lastThrottleBypass[payload.method], Date.now() - this.lastThrottleBypass[payload.method]); + if (Date.now() - this.lastThrottleBypass[payload.method] > this.throttleTimeout[payload.method]) { + // Bypass + this.lastThrottleBypass[payload.method] = Date.now(); + } else { + // Throttle + console.log('throttling', payload.method, payload.id); return { jsonrpc: '2.0', - id: payload.id, - result: [] + id: payload.id } } }