From ccfae818e20607207bd42b51eff3fdfa751b661b Mon Sep 17 00:00:00 2001 From: Fabian Vogelsteller Date: Tue, 30 Jun 2015 11:43:18 +0200 Subject: [PATCH] add timeout to the providor --- lib/web3/ipcprovider.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/web3/ipcprovider.js b/lib/web3/ipcprovider.js index c38062e..a7c758d 100644 --- a/lib/web3/ipcprovider.js +++ b/lib/web3/ipcprovider.js @@ -25,6 +25,8 @@ var utils = require('../utils/utils'); var errors = require('./errors'); +var errorTimeout = '{"jsonrpc": "2.0", "error": {"code": -32603, "message": "FRONTEND Request timed out for method \'__method__\'"}, "id": "__id__"}'; + var IpcProvider = function (path, net) { var _this = this; @@ -37,6 +39,12 @@ var IpcProvider = function (path, net) { this.connection.on('error', function(e){ console.error('IPC Connection error', e); + _this._timeout(); + }); + + this.connection.on('end', function(e){ + console.error('IPC Connection ended', e); + _this._timeout(); }); @@ -63,7 +71,6 @@ var IpcProvider = function (path, net) { id = result.id; } - // fire the callback if(_this.responseCallbacks[id]) { _this.responseCallbacks[id](null, result); @@ -81,11 +88,31 @@ which will be called if a response matching the response Id will arrive. */ IpcProvider.prototype._getResponse = function(payload, callback) { var id = payload.id || payload[0].id; + var method = payload.method || payload[0].method; this.responseCallbacks[id] = callback; + this.responseCallbacks[id].method = method; +}; + +/** +Timeout all requests when the end/error event is fired + +@method _timeout +*/ +IpcProvider.prototype._timeout = function() { + for(key in this.responseCallbacks) { + if(this.responseCallback.hasOwnProperty(key)){ + this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method)); + } + } }; +/** +Check if the current connection is still valid. + +@method isConnected +*/ IpcProvider.prototype.isConnected = function() { // try reconnect, when connection is gone if(!this.connection.writable)