Send ready only when the proxy is started

This commit is contained in:
Anthony Laibe 2018-09-20 10:16:48 +01:00
parent c477445896
commit 907b486531
2 changed files with 29 additions and 21 deletions

View File

@ -94,18 +94,21 @@ Blockchain.prototype.initProxy = function() {
this.config.wsPort += constants.blockchain.servicePortOnProxy; this.config.wsPort += constants.blockchain.servicePortOnProxy;
}; };
Blockchain.prototype.setupProxy = async function(type) { Blockchain.prototype.setupProxy = async function() {
const proxy = require('./proxy'); const proxy = require('./proxy');
const Ipc = require('../../core/ipc'); const Ipc = require('../../core/ipc');
if(!this.proxyIpc) this.proxyIpc = new Ipc({ipcRole: 'client'}); if(!this.proxyIpc) this.proxyIpc = new Ipc({ipcRole: 'client'});
if (type === 'rpc') { let wsProxy;
this.rpcProxy = await proxy.serve(this.proxyIpc, this.config.rpcHost, this.config.rpcPort, false); if(this.config.wsRPC) {
} wsProxy = proxy.serve(this.proxyIpc, this.config.wsHost, this.config.wsPort, true, this.config.wsOrigins);
else if (type === 'ws'){
this.wsProxy = await proxy.serve(this.proxyIpc, this.config.wsHost, this.config.wsPort, true, this.config.wsOrigins);
} }
[this.rpcProxy, this.wsProxy] = await Promise.all([
proxy.serve(this.proxyIpc, this.config.rpcHost, this.config.rpcPort, false),
wsProxy
]);
}; };
Blockchain.prototype.shutdownProxy = function() { Blockchain.prototype.shutdownProxy = function() {
@ -180,25 +183,33 @@ Blockchain.prototype.run = function() {
self.child.stdout.on('data', (data) => { self.child.stdout.on('data', (data) => {
console.error(`Geth error: ${data}`); console.error(`Geth error: ${data}`);
}); });
let httpReady = false;
let wsReady = !self.config.wsRPC;
// Geth logs appear in stderr somehow // Geth logs appear in stderr somehow
self.child.stderr.on('data', async (data) => { self.child.stderr.on('data', async (data) => {
data = data.toString(); data = data.toString();
if (!self.readyCalled && data.indexOf('HTTP endpoint opened') > -1 && self.config.proxy) { if (data.indexOf('HTTP endpoint opened') > -1) {
await self.setupProxy('rpc'); httpReady = true;
} }
if (!self.readyCalled && data.indexOf('WebSocket endpoint opened') > -1) {
if (self.config.proxy) { if (data.indexOf('WebSocket endpoint opened') > -1) {
await self.setupProxy('ws'); wsReady = true;
} }
if (!self.readyCalled && wsReady && httpReady) {
self.readyCalled = true;
if (self.isDev) { if (self.isDev) {
self.createFundAndUnlockAccounts((err) => { self.createFundAndUnlockAccounts((err) => {
// TODO: this is never called! // TODO: this is never called!
if(err) console.error('Error creating, unlocking, and funding accounts', err); if(err) console.error('Error creating, unlocking, and funding accounts', err);
}); });
} }
self.readyCalled = true; if (self.config.proxy) {
await self.setupProxy();
}
self.readyCallback(); self.readyCallback();
} }
console.log('Geth: ' + data); console.log('Geth: ' + data);
}); });
self.child.on('exit', (code) => { self.child.on('exit', (code) => {

View File

@ -73,14 +73,8 @@ exports.serve = async function (ipc, host, port, ws, origin) {
utils.pingEndpoint( utils.pingEndpoint(
canonicalHost(host), port, ws ? 'ws': false, 'http', _origin, async (err) => { canonicalHost(host), port, ws ? 'ws': false, 'http', _origin, async (err) => {
if (!err || (Date.now() - start > 10000)) { if (!err || (Date.now() - start > 10000)) {
// if (Date.now() - start > 10000) {
// console.warn('!!! TIMEOUT !!!');
// } else {
// console.warn('!!! CONNECT !!!');
// }
return resolve(); return resolve();
} }
// console.warn('!!! WAITING !!!');
await utils.timer(250).then(awaitTarget).then(resolve); await utils.timer(250).then(awaitTarget).then(resolve);
} }
); );
@ -152,6 +146,9 @@ exports.serve = async function (ipc, host, port, ws, origin) {
}); });
} }
const listenPort = port - constants.blockchain.servicePortOnProxy; const listenPort = port - constants.blockchain.servicePortOnProxy;
server.listen(listenPort, defaultHost); return new Promise(resolve => {
return server; server.listen(listenPort, defaultHost, () => {
resolve(server);
});
});
}; };