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

View File

@ -73,14 +73,8 @@ exports.serve = async function (ipc, host, port, ws, origin) {
utils.pingEndpoint(
canonicalHost(host), port, ws ? 'ws': false, 'http', _origin, async (err) => {
if (!err || (Date.now() - start > 10000)) {
// if (Date.now() - start > 10000) {
// console.warn('!!! TIMEOUT !!!');
// } else {
// console.warn('!!! CONNECT !!!');
// }
return resolve();
}
// console.warn('!!! WAITING !!!');
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;
server.listen(listenPort, defaultHost);
return server;
return new Promise(resolve => {
server.listen(listenPort, defaultHost, () => {
resolve(server);
});
});
};