Skip to content
This repository was archived by the owner on Sep 5, 2020. It is now read-only.

Commit

Permalink
Better throttling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanio authored and wolovim committed Mar 14, 2018
1 parent 54daa75 commit 6de9d20
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions modules/ipc/methods/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}


Expand Down Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit 6de9d20

Please sign in to comment.