From 543471919113f13de55498c1644033eee9ca844d Mon Sep 17 00:00:00 2001 From: Tom Kirkpatrick Date: Sat, 8 Dec 2018 01:20:21 +0100 Subject: [PATCH] perf(grpc): throttle calls to fetch channels --- app/reducers/channels.js | 15 ++++++++++++--- package.json | 5 +++-- yarn.lock | 4 ++-- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/app/reducers/channels.js b/app/reducers/channels.js index be45f3d4b1a..a4979daf328 100644 --- a/app/reducers/channels.js +++ b/app/reducers/channels.js @@ -1,6 +1,6 @@ import { createSelector } from 'reselect' import { ipcRenderer } from 'electron' -import { debounce } from 'lodash.debounce' +import throttle from 'lodash.throttle' import { btc } from 'lib/utils' import { requestSuggestedNodes } from 'lib/utils/api' import db from 'store/db' @@ -277,6 +277,15 @@ export const pushclosechannelstatus = () => dispatch => { dispatch(fetchChannels()) } +/** + * Throttled dispatch to fetchChannels. + * Calls fetchChannels no more than once per second. + */ +const throttledFetchChannels = throttle(dispatch => dispatch(fetchChannels()), 1000, { + leading: true, + trailing: true +}) + // IPC event for channel graph data export const channelGraphData = (event, data) => (dispatch, getState) => { const { info, channels } = getState() @@ -309,8 +318,8 @@ export const channelGraphData = (event, data) => (dispatch, getState) => { if (hasUpdates) { // We can receive a lot of channel updates from channel graph subscription in a short space of time. If these // nvolve our our channels we make a call to fetchChannels and then fetchBalances in order to refresh our channel - // and balance data. Debounce these calls so that we don't fire too many times in quick succession. - debounce(() => dispatch(fetchChannels), 1000, { leading: true, trailing: true, maxWait: 3000 }) + // and balance data. Throttle these calls so that we don't attempt to fetch channels to often. + throttledFetchChannels(dispatch) } } diff --git a/package.json b/package.json index 2c8fc013e31..21ebe0edf2a 100644 --- a/package.json +++ b/package.json @@ -174,7 +174,8 @@ "\\.(css|less|sass|scss)$": "identity-obj-proxy" }, "moduleFileExtensions": [ - "js", "json" + "js", + "json" ], "moduleDirectories": [ "app", @@ -313,9 +314,9 @@ "is-electron-renderer": "2.0.1", "javascript-state-machine": "3.1.0", "jstimezonedetect": "1.0.6", - "lodash.debounce": "4.0.8", "lodash.get": "4.4.2", "lodash.pick": "4.4.0", + "lodash.throttle": "4.1.1", "prop-types": "15.6.2", "qrcode.react": "0.8.0", "rc-menu": "7.4.20", diff --git a/yarn.lock b/yarn.lock index 3d6944ab4d2..ab59b9e2b0b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10696,7 +10696,7 @@ lodash.clonedeep@^4.3.2: resolved "https://registry.yarnpkg.com/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz#e23f3f9c4f8fbdde872529c1071857a086e5ccef" integrity sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8= -lodash.debounce@4.0.8, lodash.debounce@^4.0.8: +lodash.debounce@^4.0.8: version "4.0.8" resolved "https://registry.yarnpkg.com/lodash.debounce/-/lodash.debounce-4.0.8.tgz#82d79bff30a67c4005ffd5e2515300ad9ca4d7af" integrity sha1-gteb/zCmfEAF/9XiUVMArZyk168= @@ -10815,7 +10815,7 @@ lodash.templatesettings@^4.0.0: dependencies: lodash._reinterpolate "~3.0.0" -lodash.throttle@^4.1.1: +lodash.throttle@4.1.1, lodash.throttle@^4.1.1: version "4.1.1" resolved "https://registry.yarnpkg.com/lodash.throttle/-/lodash.throttle-4.1.1.tgz#c23e91b710242ac70c37f1e1cda9274cc39bf2f4" integrity sha1-wj6RtxAkKscMN/HhzaknTMOb8vQ=