Skip to content

Commit 9cf02d9

Browse files
committed
api: throttle API calls initiated by stream events
1 parent 264dbe5 commit 9cf02d9

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

app/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"http-proxy-middleware": "1.0.4",
3131
"i18next": "19.4.4",
3232
"i18next-browser-languagedetector": "4.1.1",
33+
"lodash": "4.17.15",
3334
"lottie-web": "5.6.8",
3435
"mobx": "5.15.4",
3536
"mobx-react-lite": "2.0.6",
@@ -55,6 +56,7 @@
5556
"@types/debug": "4.1.5",
5657
"@types/google-protobuf": "3.7.2",
5758
"@types/jest": "^25.2.1",
59+
"@types/lodash": "4.14.155",
5860
"@types/node": "^13.13.5",
5961
"@types/react": "^16.9.34",
6062
"@types/react-dom": "^16.9.7",

app/src/store/stores/channelStore.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from 'mobx';
1010
import { ChannelEventUpdate, ChannelPoint } from 'types/generated/lnd_pb';
1111
import Big from 'big.js';
12+
import debounce from 'lodash/debounce';
1213
import { Store } from 'store';
1314
import { Channel } from '../models';
1415

@@ -98,6 +99,9 @@ export default class ChannelStore {
9899
}
99100
}
100101

102+
/** fetch channels at most once every 2 seconds when using this func */
103+
fetchChannelsThrottled = debounce(this.fetchChannels, 2000);
104+
101105
/** update the channel list based on events from the API */
102106
@action.bound
103107
onChannelEvent(event: ChannelEventUpdate.AsObject) {
@@ -125,11 +129,13 @@ export default class ChannelStore {
125129
const channel = this.channels.get(event.closedChannel.chanId);
126130
this.channels.delete(event.closedChannel.chanId);
127131
this._store.log.info('removed closed channel', toJS(channel));
132+
this._store.nodeStore.fetchBalancesThrottled();
128133
} else if (event.type === OPEN_CHANNEL && event.openChannel) {
129134
// add the new opened channel
130135
const channel = new Channel(this._store, event.openChannel);
131136
this.channels.set(channel.chanId, channel);
132137
this._store.log.info('added new open channel', toJS(channel));
138+
this._store.nodeStore.fetchBalancesThrottled();
133139
}
134140
}
135141

app/src/store/stores/nodeStore.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { action, observable, runInAction, toJS } from 'mobx';
22
import { Transaction } from 'types/generated/lnd_pb';
33
import Big from 'big.js';
4+
import debounce from 'lodash/debounce';
45
import { Store } from 'store';
56
import { Wallet } from '../models';
67

@@ -71,6 +72,9 @@ export default class NodeStore {
7172
}
7273
}
7374

75+
/** fetch balances at most once every 2 seconds when using this func */
76+
fetchBalancesThrottled = debounce(this.fetchBalances, 2000);
77+
7478
/**
7579
* updates the wallet balance from the transaction provided
7680
*/

app/src/store/stores/swapStore.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ export default class SwapStore {
100100
@action.bound
101101
onSwapUpdate(loopSwap: SwapStatus.AsObject) {
102102
this.addOrUpdateSwap(loopSwap);
103-
this._store.channelStore.fetchChannels();
103+
// the swap update likely caused a change in onchain/offchain balances
104+
// so fetch updated data from the server
105+
this._store.channelStore.fetchChannelsThrottled();
106+
this._store.nodeStore.fetchBalancesThrottled();
104107
}
105108

106109
/** exports the sorted list of swaps to CSV file */

app/yarn.lock

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2310,6 +2310,11 @@
23102310
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.4.tgz#38fd73ddfd9b55abb1e1b2ed578cb55bd7b7d339"
23112311
integrity sha512-8+KAKzEvSUdeo+kmqnKrqgeE+LcA0tjYWFY7RPProVYwnqDjukzO+3b6dLD56rYX5TdWejnEOLJYOIeh4CXKuA==
23122312

2313+
"@types/lodash@4.14.155":
2314+
version "4.14.155"
2315+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.155.tgz#e2b4514f46a261fd11542e47519c20ebce7bc23a"
2316+
integrity sha512-vEcX7S7aPhsBCivxMwAANQburHBtfN9RdyXFk84IJmu2Z4Hkg1tOFgaslRiEqqvoLtbCBi6ika1EMspE+NZ9Lg==
2317+
23132318
"@types/minimatch@*":
23142319
version "3.0.3"
23152320
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
@@ -9288,7 +9293,7 @@ lodash.uniq@4.5.0, lodash.uniq@^4.5.0:
92889293
resolved "https://registry.yarnpkg.com/lodash.uniq/-/lodash.uniq-4.5.0.tgz#d0225373aeb652adc1bc82e4945339a842754773"
92899294
integrity sha1-0CJTc662Uq3BvILklFM5qEJ1R3M=
92909295

9291-
"lodash@>=3.5 <5", lodash@^4.0.0, lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.4, lodash@^4.17.5, lodash@~4.17.12:
9296+
lodash@4.17.15, "lodash@>=3.5 <5", lodash@^4.0.0, lodash@^4.17.11, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.14, lodash@^4.17.15, lodash@^4.17.4, lodash@^4.17.5, lodash@~4.17.12:
92929297
version "4.17.15"
92939298
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
92949299
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==

0 commit comments

Comments
 (0)