rate-limit concurrent processPreCoin

This commit is contained in:
Florian Dold 2016-11-17 01:23:53 +01:00
parent d088aec009
commit 86fb71f563

View File

@ -379,6 +379,8 @@ export class Wallet {
private notifier: Notifier; private notifier: Notifier;
public cryptoApi: CryptoApi; public cryptoApi: CryptoApi;
private processPreCoinConcurrent = 0;
/** /**
* Set of identifiers for running operations. * Set of identifiers for running operations.
*/ */
@ -725,22 +727,29 @@ export class Wallet {
private async processPreCoin(preCoin: PreCoinRecord, private async processPreCoin(preCoin: PreCoinRecord,
retryDelayMs = 100): Promise<void> { retryDelayMs = 200): Promise<void> {
if (this.processPreCoinConcurrent >= 1) {
const exchange = await this.q().get(Stores.exchanges, console.log("delaying processPreCoin");
preCoin.exchangeBaseUrl); setTimeout(() => this.processPreCoin(preCoin, retryDelayMs * 2),
if (!exchange) { retryDelayMs);
console.error("db inconsistend: exchange for precoin not found");
return; return;
} }
const denom = await this.q().get(Stores.denominations, console.log("executing processPreCoin");
[preCoin.exchangeBaseUrl, preCoin.denomPub]); this.processPreCoinConcurrent++;
if (!denom) {
console.error("db inconsistent: denom for precoin not found");
return;
}
try { try {
const exchange = await this.q().get(Stores.exchanges,
preCoin.exchangeBaseUrl);
if (!exchange) {
console.error("db inconsistend: exchange for precoin not found");
return;
}
const denom = await this.q().get(Stores.denominations,
[preCoin.exchangeBaseUrl, preCoin.denomPub]);
if (!denom) {
console.error("db inconsistent: denom for precoin not found");
return;
}
const coin = await this.withdrawExecute(preCoin); const coin = await this.withdrawExecute(preCoin);
const mutateReserve = (r: ReserveRecord) => { const mutateReserve = (r: ReserveRecord) => {
@ -784,6 +793,8 @@ export class Wallet {
let nextRetryDelayMs = Math.min(retryDelayMs * 2, 1000 * 60); let nextRetryDelayMs = Math.min(retryDelayMs * 2, 1000 * 60);
setTimeout(() => this.processPreCoin(preCoin, nextRetryDelayMs), setTimeout(() => this.processPreCoin(preCoin, nextRetryDelayMs),
retryDelayMs); retryDelayMs);
} finally {
this.processPreCoinConcurrent--;
} }
} }