From ff6c51153612c189b0dc4e8a8dcc2a289c768a87 Mon Sep 17 00:00:00 2001 From: mosu forge Date: Wed, 6 Feb 2019 23:27:41 -0800 Subject: [PATCH 1/2] Use queue for RPC calls --- package.json | 1 + src-electron/main-process/modules/daemon.js | 59 ++++++++++-------- .../main-process/modules/wallet-rpc.js | 60 ++++++++++--------- 3 files changed, 69 insertions(+), 51 deletions(-) diff --git a/package.json b/package.json index 4d80e3a..cc8d567 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "electron-window-state": "^5.0.2", "object-assign-deep": "^0.4.0", "portscanner": "^2.2.0", + "promise-queue": "^2.2.5", "qrcode.vue": "^1.6.0", "request": "^2.87.0", "request-promise": "^4.2.2", diff --git a/src-electron/main-process/modules/daemon.js b/src-electron/main-process/modules/daemon.js index 7cf5e32..206feff 100644 --- a/src-electron/main-process/modules/daemon.js +++ b/src-electron/main-process/modules/daemon.js @@ -1,6 +1,8 @@ import child_process from "child_process"; -import request from "request-promise"; -const fs = require('fs'); +const request = require("request-promise"); +const queue = require("promise-queue"); +const http = require("http"); +const fs = require("fs"); const path = require("path"); export class Daemon { @@ -11,6 +13,10 @@ export class Daemon { this.id = 0 this.testnet = false this.local = false // do we have a local daemon ? + + this.agent = new http.Agent({keepAlive: true, maxSockets: 1}) + this.queue = new queue(1, Infinity) + } @@ -341,42 +347,46 @@ export class Daemon { sendRPC(method, params={}) { let id = this.id++ let options = { - forever: true, + uri: `${this.protocol}${this.hostname}:${this.port}/json_rpc`, + method: "POST", json: { jsonrpc: "2.0", id: id, method: method }, + agent: this.agent }; - if (Object.keys(params).length !== 0) { + if(Object.keys(params).length !== 0) { options.json.params = params; } - return request.post(`${this.protocol}${this.hostname}:${this.port}/json_rpc`, options) - .then((response) => { - if(response.hasOwnProperty("error")) { + return this.queue.add(() => { + return request(options) + .then((response) => { + if(response.hasOwnProperty("error")) { + return { + method: method, + params: params, + error: response.error + } + } return { method: method, params: params, - error: response.error + result: response.result } - } - return { - method: method, - params: params, - result: response.result - } - }).catch(error => { - return { - method: method, - params: params, - error: { - code: -1, - message: "Cannot connect to daemon-rpc", - cause: error.cause + }).catch(error => { + return { + method: method, + params: params, + error: { + code: -1, + message: "Cannot connect to daemon-rpc", + cause: error.cause + } } - } - }) + }) + }) } /** @@ -391,6 +401,7 @@ export class Daemon { return new Promise((resolve, reject) => { if (this.daemonProcess) { this.daemonProcess.on("close", code => { + this.agent.destroy() resolve() }) this.daemonProcess.kill() diff --git a/src-electron/main-process/modules/wallet-rpc.js b/src-electron/main-process/modules/wallet-rpc.js index fd7b887..58a0e35 100644 --- a/src-electron/main-process/modules/wallet-rpc.js +++ b/src-electron/main-process/modules/wallet-rpc.js @@ -1,5 +1,7 @@ import child_process from "child_process"; const request = require("request-promise"); +const queue = require("promise-queue"); +const http = require("http"); const os = require("os"); const fs = require("fs"); const path = require("path"); @@ -28,6 +30,8 @@ export class WalletRPC { this.height_regex2 = /Skipped block by height: (\d+)/ this.height_regex3 = /Skipped block by timestamp, height: (\d+)/ + this.agent = new http.Agent({keepAlive: true, maxSockets: 1}) + this.queue = new queue(1, Infinity) } @@ -52,8 +56,7 @@ export class WalletRPC { ] const args = [ - //"--rpc-login", this.auth[0]+":"+this.auth[1], - "--disable-rpc-login", + "--rpc-login", this.auth[0]+":"+this.auth[1], "--rpc-bind-port", options.wallet.rpc_bind_port, "--daemon-address", daemon_address, //"--log-level", options.wallet.log_level, @@ -1228,52 +1231,54 @@ export class WalletRPC { sendRPC(method, params={}, timeout=0) { let id = this.id++ let options = { - forever: true, + uri: `${this.protocol}${this.hostname}:${this.port}/json_rpc`, + method: "POST", json: { jsonrpc: "2.0", id: id, method: method }, - /* auth: { user: this.auth[0], pass: this.auth[1], sendImmediately: false - } - */ + }, + agent: this.agent }; - if (Object.keys(params).length !== 0) { + if(Object.keys(params).length !== 0) { options.json.params = params } if(timeout) { options.timeout = timeout } - return request.post(`${this.protocol}${this.hostname}:${this.port}/json_rpc`, options) - .then((response) => { - if(response.hasOwnProperty("error")) { + return this.queue.add(() => { + return request(options) + .then((response) => { + if(response.hasOwnProperty("error")) { + return { + method: method, + params: params, + error: response.error + } + } return { method: method, params: params, - error: response.error + result: response.result } - } - return { - method: method, - params: params, - result: response.result - } - }).catch(error => { - return { - method: method, - params: params, - error: { - code: -1, - message: "Cannot connect to wallet-rpc", - cause: error.cause + }).catch(error => { + return { + method: method, + params: params, + error: { + code: -1, + message: "Cannot connect to wallet-rpc", + cause: error.cause + } } - } - }) + }) + }) } getRPC(parameter, params={}) { @@ -1290,6 +1295,7 @@ export class WalletRPC { }) setTimeout(() => { this.walletRPCProcess.on("close", code => { + this.agent.destroy() resolve() }) this.walletRPCProcess.kill() From cecd1843916766d7ecf38397fb446e668355bca9 Mon Sep 17 00:00:00 2001 From: mosu forge Date: Thu, 7 Feb 2019 09:49:33 -0800 Subject: [PATCH 2/2] More efficient handling for tx history page --- src/components/tx_list.vue | 133 ++++++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 54 deletions(-) diff --git a/src/components/tx_list.vue b/src/components/tx_list.vue index e621844..fee3ace 100644 --- a/src/components/tx_list.vue +++ b/src/components/tx_list.vue @@ -1,21 +1,21 @@