diff --git a/app/modules/ws2p/lib/WS2PConnection.ts b/app/modules/ws2p/lib/WS2PConnection.ts index 752a3cf408c9a309b9780a78edb46b396fd70d3d..ce3ce059e88a24a140de7996c7330e85e87c2b97 100644 --- a/app/modules/ws2p/lib/WS2PConnection.ts +++ b/app/modules/ws2p/lib/WS2PConnection.ts @@ -6,8 +6,8 @@ import {CertificationDTO} from "../../../lib/dto/CertificationDTO" import {MembershipDTO} from "../../../lib/dto/MembershipDTO" import {TransactionDTO} from "../../../lib/dto/TransactionDTO" import {PeerDTO} from "../../../lib/dto/PeerDTO" -import { WS2PConstants } from './constants'; -import { ProxiesConf } from '../../../lib/proxy'; +import {WS2PConstants} from './constants'; + const ws = require('ws') const SocksProxyAgent = require('socks-proxy-agent'); const nuuid = require('node-uuid'); @@ -240,6 +240,20 @@ export interface WS2PRequest { params?:any } +export class WS2PMessageExchange { + + promise: Promise<any> + extras: { + resolve: (data:any) => void + reject: (err:any) => void + } + + constructor(extras: { resolve: () => void; reject: () => void }, race: any) { + this.promise = race + this.extras = extras + } +} + /** * The handler of a WS2P connection. * @@ -257,14 +271,8 @@ export class WS2PConnection { private nbResponsesCount = 0 private nbPushsToRemoteCount = 0 private nbPushsByRemoteCount = 0 - private exchanges:{ - [uuid:string]: { - promise: Promise<any>, - extras: { - resolve: (data:any) => void - reject: (err:any) => void - } - } + private exchanges: { + [uuid: string]: WS2PMessageExchange } = {} constructor( @@ -562,9 +570,9 @@ export class WS2PConnection { resolve: () => { console.error('resolution not implemented') }, reject: () => { console.error('rejection not implemented') } } - this.exchanges[uuid] = { + this.exchanges[uuid] = new WS2PMessageExchange( extras, - promise: Promise.race([ + Promise.race([ // The answer new Promise((res, rej) => { extras.resolve = res @@ -580,11 +588,13 @@ export class WS2PConnection { }, this.options.requestTimeout) }) ]) - } + ) try { resolve(await this.exchanges[uuid].promise) } catch(e) { reject(e) + } finally { + delete this.exchanges[uuid] } } }) diff --git a/app/modules/ws2p/lib/interface/WS2PServerMessageHandler.ts b/app/modules/ws2p/lib/interface/WS2PServerMessageHandler.ts index 1a64ac0fc5edc995ded732603f41e34d81e35a48..1d5727e56dfaa87b22e1512c5f20d2b5a631e140 100644 --- a/app/modules/ws2p/lib/interface/WS2PServerMessageHandler.ts +++ b/app/modules/ws2p/lib/interface/WS2PServerMessageHandler.ts @@ -102,6 +102,11 @@ export class WS2PServerMessageHandler implements WS2PMessageHandler { } this.server.logger.warn(message) } + setTimeout(() => { + if (this.errors[documentHash]) { + delete this.errors[documentHash] + } + }, 1000 * WS2PConstants.ERROR_RECALL_DURATION_IN_SECONDS) } else { // Remember the error for some time if (!this.errors[documentHash]) { @@ -112,7 +117,9 @@ export class WS2PServerMessageHandler implements WS2PMessageHandler { } this.errors[documentHash].pubkeys[c.pubkey] = [json.body] setTimeout(() => { - delete this.errors[documentHash] + if (this.errors[documentHash]) { + delete this.errors[documentHash] + } }, 1000 * WS2PConstants.ERROR_RECALL_DURATION_IN_SECONDS) } if (e !== "Block already known"