diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-11-17 01:23:53 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-11-17 01:23:53 +0100 |
commit | 86fb71f563eb8b44cf97e9957fdfe1d5fd2a829a (patch) | |
tree | e4dd574cba8fb415cb89a8e3fbc00dcb3ad7eba5 | |
parent | d088aec009ff43a3d668539d3b0790b86974d3eb (diff) |
rate-limit concurrent processPreCoin
-rw-r--r-- | src/wallet.ts | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/src/wallet.ts b/src/wallet.ts index 9e96bffd3..354072130 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -379,6 +379,8 @@ export class Wallet { private notifier: Notifier; public cryptoApi: CryptoApi; + private processPreCoinConcurrent = 0; + /** * Set of identifiers for running operations. */ @@ -725,22 +727,29 @@ export class Wallet { private async processPreCoin(preCoin: PreCoinRecord, - retryDelayMs = 100): Promise<void> { - - 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"); + retryDelayMs = 200): Promise<void> { + if (this.processPreCoinConcurrent >= 1) { + console.log("delaying processPreCoin"); + setTimeout(() => this.processPreCoin(preCoin, retryDelayMs * 2), + retryDelayMs); return; } - + console.log("executing processPreCoin"); + this.processPreCoinConcurrent++; 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 mutateReserve = (r: ReserveRecord) => { @@ -784,6 +793,8 @@ export class Wallet { let nextRetryDelayMs = Math.min(retryDelayMs * 2, 1000 * 60); setTimeout(() => this.processPreCoin(preCoin, nextRetryDelayMs), retryDelayMs); + } finally { + this.processPreCoinConcurrent--; } } |