Skip to content
Snippets Groups Projects
Commit c283df5b authored by Éloïs's avatar Éloïs
Browse files

Merge branch '1278-memory-leak-on-ws2p' into '1.6'

Resolve "Memory leak on WS2P"

Closes #1278

See merge request nodes/typescript/duniter!1247
parents 019a4a7a d5a5aafd
Branches
Tags
1 merge request!1247Resolve "Memory leak on WS2P"
...@@ -20,7 +20,7 @@ import {MembershipDTO} from "../../../lib/dto/MembershipDTO" ...@@ -20,7 +20,7 @@ import {MembershipDTO} from "../../../lib/dto/MembershipDTO"
import {TransactionDTO} from "../../../lib/dto/TransactionDTO" import {TransactionDTO} from "../../../lib/dto/TransactionDTO"
import {PeerDTO} from "../../../lib/dto/PeerDTO" import {PeerDTO} from "../../../lib/dto/PeerDTO"
import {WS2PConstants} from './constants'; import {WS2PConstants} from './constants';
import { ProxiesConf } from '../../../lib/proxy';
const ws = require('ws') const ws = require('ws')
const SocksProxyAgent = require('socks-proxy-agent'); const SocksProxyAgent = require('socks-proxy-agent');
const nuuid = require('node-uuid'); const nuuid = require('node-uuid');
...@@ -253,6 +253,20 @@ export interface WS2PRequest { ...@@ -253,6 +253,20 @@ export interface WS2PRequest {
params?:any 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. * The handler of a WS2P connection.
* *
...@@ -271,13 +285,7 @@ export class WS2PConnection { ...@@ -271,13 +285,7 @@ export class WS2PConnection {
private nbPushsToRemoteCount = 0 private nbPushsToRemoteCount = 0
private nbPushsByRemoteCount = 0 private nbPushsByRemoteCount = 0
private exchanges: { private exchanges: {
[uuid:string]: { [uuid: string]: WS2PMessageExchange
promise: Promise<any>,
extras: {
resolve: (data:any) => void
reject: (err:any) => void
}
}
} = {} } = {}
constructor( constructor(
...@@ -575,9 +583,9 @@ export class WS2PConnection { ...@@ -575,9 +583,9 @@ export class WS2PConnection {
resolve: () => { console.error('resolution not implemented') }, resolve: () => { console.error('resolution not implemented') },
reject: () => { console.error('rejection not implemented') } reject: () => { console.error('rejection not implemented') }
} }
this.exchanges[uuid] = { this.exchanges[uuid] = new WS2PMessageExchange(
extras, extras,
promise: Promise.race([ Promise.race([
// The answer // The answer
new Promise((res, rej) => { new Promise((res, rej) => {
extras.resolve = res extras.resolve = res
...@@ -593,11 +601,13 @@ export class WS2PConnection { ...@@ -593,11 +601,13 @@ export class WS2PConnection {
}, this.options.requestTimeout) }, this.options.requestTimeout)
}) })
]) ])
} )
try { try {
resolve(await this.exchanges[uuid].promise) resolve(await this.exchanges[uuid].promise)
} catch(e) { } catch(e) {
reject(e) reject(e)
} finally {
delete this.exchanges[uuid]
} }
} }
}) })
......
...@@ -115,6 +115,11 @@ export class WS2PServerMessageHandler implements WS2PMessageHandler { ...@@ -115,6 +115,11 @@ export class WS2PServerMessageHandler implements WS2PMessageHandler {
} }
this.server.logger.warn(message) this.server.logger.warn(message)
} }
setTimeout(() => {
if (this.errors[documentHash]) {
delete this.errors[documentHash]
}
}, 1000 * WS2PConstants.ERROR_RECALL_DURATION_IN_SECONDS)
} else { } else {
// Remember the error for some time // Remember the error for some time
if (!this.errors[documentHash]) { if (!this.errors[documentHash]) {
...@@ -125,7 +130,9 @@ export class WS2PServerMessageHandler implements WS2PMessageHandler { ...@@ -125,7 +130,9 @@ export class WS2PServerMessageHandler implements WS2PMessageHandler {
} }
this.errors[documentHash].pubkeys[c.pubkey] = [json.body] this.errors[documentHash].pubkeys[c.pubkey] = [json.body]
setTimeout(() => { setTimeout(() => {
if (this.errors[documentHash]) {
delete this.errors[documentHash] delete this.errors[documentHash]
}
}, 1000 * WS2PConstants.ERROR_RECALL_DURATION_IN_SECONDS) }, 1000 * WS2PConstants.ERROR_RECALL_DURATION_IN_SECONDS)
} }
if (e !== "Block already known" if (e !== "Block already known"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment