Merge pull request #565 from matiu/feature/reconnectInsight

insight REST API retry
This commit is contained in:
Gustavo Maximiliano Cortez 2014-06-05 14:54:52 -03:00
commit 69e31e2a79
7 changed files with 62 additions and 47 deletions

View File

@ -109,12 +109,15 @@ var defaultConfig = {
// blockchain service API config // blockchain service API config
blockchain: { blockchain: {
host: 'test.insight.is', host: 'test.insight.is',
port: 80 port: 80,
retryDelay: 1000,
}, },
// socket service API config // socket service API config
socket: { socket: {
host: 'test.insight.is', host: 'test.insight.is',
port: 80 port: 80,
// will duplicate itself after each try
reconnectDelay: 500,
}, },
// local encryption/security config // local encryption/security config

View File

@ -82,12 +82,12 @@
</div> </div>
</div> </div>
<div class="row" ng-if='$root.insightError'> <div class="row" ng-if='$root.insightError>1'>
<div class="small-8 large-centered columns"> <div class="small-8 large-centered columns">
<div data-alert class="alert-box radius error"> <div data-alert class="alert-box radius error">
Error connecting to Insight server. Check Having troubles connecting to Insight server. Check
you settings and Internet connection. you settings and Internet connection.<br>
Trying to reconnect... Reconnect Atempt #{{$root.insightError}}
</div> </div>
</div> </div>
</div> </div>

View File

@ -35,19 +35,15 @@ angular.module('copayApp.controllers').controller('HeaderController',
// Initialize alert notification (not show when init wallet) // Initialize alert notification (not show when init wallet)
$rootScope.txAlertCount = 0; $rootScope.txAlertCount = 0;
$rootScope.insightError = -1;
$rootScope.$watch('blockChainStatus', function(status) { $rootScope.$watch('insightError', function(status) {
var h = config.blockchain.host + ':' + config.blockchain.port; if (status === 0) {
if (status === 'error') $rootScope.$flashMessage = {
$rootScope.insightError = 1; type: 'success',
else message: 'Networking Restored :)',
if (status === 'restored') { };
$rootScope.insightError = 0; }
$rootScope.$flashMessage = {
type: 'success',
message: 'Networking Restored',
};
}
}); });

View File

@ -8,6 +8,7 @@ function Insight(opts) {
this.host = opts.host || 'localhost'; this.host = opts.host || 'localhost';
this.port = opts.port || '3001'; this.port = opts.port || '3001';
this.scheme = opts.scheme || 'http'; this.scheme = opts.scheme || 'http';
this.retryDelay = opts.retryDelay || 5000;
} }
function _asyncForEach(array, fn, callback) { function _asyncForEach(array, fn, callback) {
@ -123,11 +124,13 @@ Insight.prototype.getUnspent = function(addresses, cb) {
} }
}; };
var self = this;
this._request(options, function(err, res) { this._request(options, function(err, res) {
if (err) { if (err) {
return cb(err); return cb(err);
} }
if (res && res.length > 0) { if (res && res.length > 0) {
all = all.concat(res); all = all.concat(res);
} }
@ -157,6 +160,9 @@ Insight.prototype.sendRawTransaction = function(rawtx, cb) {
}; };
Insight.prototype._request = function(options, callback) { Insight.prototype._request = function(options, callback) {
var self = this;
if (typeof process === 'undefined' || !process.version) { if (typeof process === 'undefined' || !process.version) {
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
@ -174,28 +180,38 @@ Insight.prototype._request = function(options, callback) {
} }
request.open(options.method, url, true); request.open(options.method, url, true);
request.timeout = 10000; request.timeout = 5000;
request.ontimeout = function() { request.ontimeout = function() {
return callback({ console.log('Insight timeout...retrying', err, self.retryDelay); //TODO
message: 'Insight request timeout. Please check your Insight settings or the Insight server status.' setTimeout(function() {
}); return self._request(options,callback);
}, self.retryDelay);
return callback(new Error('Insight request timeout'));
}; };
request.onreadystatechange = function() { request.onreadystatechange = function() {
if (request.readyState === 4) { if (request.readyState === 4) {
if (request.status === 200 || request.status === 304 || request.status === 0) { if (request.status === 200 || request.status === 304) {
try { try {
var ret = JSON.parse(request.responseText); var ret = JSON.parse(request.responseText);
return callback(null, ret); return callback(null, ret);
} catch (e) { } catch (e) {
return callback({ return callback(new Error('CRITICAL: Wrong response from insight'));
message: 'Wrong response from insight'
});
} }
} else { }
return callback({ // User error
message: 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText else if (request.status >= 400 && request.status < 499) {
}); return callback(new Error('CRITICAL: Bad request to insight. Probably wrong transaction to broadcast?.'));
}
else {
var err= 'Error code: ' + request.status + ' - Status: ' + request.statusText
+ ' - Description: ' + request.responseText;
console.log('Insight Temporary error (will retry):', err);
setTimeout(function() {
console.log('### Retrying Insight Request....'); //TODO
return self._request(options,callback);
}, self.retryDelay);
return callback(new Error(err));
} }
} }
}; };
@ -205,7 +221,9 @@ Insight.prototype._request = function(options, callback) {
} }
request.send(options.data || null); request.send(options.data || null);
} else { }
else {
var http = require('http'); var http = require('http');
var req = http.request(options, function(response) { var req = http.request(options, function(response) {
var ret; var ret;

View File

@ -101,22 +101,21 @@ angular.module('copayApp.services')
root.updateBalance = function(cb) { root.updateBalance = function(cb) {
var w = $rootScope.wallet; var w = $rootScope.wallet;
if (!w) return root.onErrorDigest();
$rootScope.balanceByAddr = {}; $rootScope.balanceByAddr = {};
$rootScope.updatingBalance = true; $rootScope.updatingBalance = true;
w.getBalance(function(err, balance, balanceByAddr, safeBalance) { w.getBalance(function(err, balance, balanceByAddr, safeBalance) {
if (err) { if (err) {
$rootScope.$flashMessage = {
type: 'error',
message: 'Error: ' + err.message
};
$rootScope.$digest();
console.error('Error: ' + err.message); //TODO console.error('Error: ' + err.message); //TODO
root._setCommError();
return null; return null;
} }
else {
root._clearCommError();
}
$rootScope.totalBalance = balance; $rootScope.totalBalance = balance;
$rootScope.balanceByAddr = balanceByAddr; $rootScope.balanceByAddr = balanceByAddr;
$rootScope.availableBalance = safeBalance; $rootScope.availableBalance = safeBalance;
@ -175,13 +174,15 @@ angular.module('copayApp.services')
}; };
root._setCommError = function(e) { root._setCommError = function(e) {
$rootScope.blockChainStatus='error'; // first error ever?
if ($rootScope.insightError<0)
$rootScope.insightError=0;
$rootScope.insightError++;
}; };
root._clearCommError = function(e) { root._clearCommError = function(e) {
if ($rootScope.blockChainStatus==='error') $rootScope.insightError=0;
$rootScope.blockChainStatus='restored';
}; };
root.setSocketHandlers = function() { root.setSocketHandlers = function() {

View File

@ -6,7 +6,7 @@ angular.module('copayApp.services').factory('Socket',
var url = 'http://' + config.socket.host + ':' + config.socket.port; var url = 'http://' + config.socket.host + ':' + config.socket.port;
var socket = io(url, { var socket = io(url, {
'reconnection': true, 'reconnection': true,
'reconnectionDelay': 500, 'reconnectionDelay': config.socket.reconnectDelay || 500,
}); });
return { return {

View File

@ -103,15 +103,12 @@ describe("Unit: Controllers", function() {
it('should check blockChainStatus', function() { it('should check blockChainStatus', function() {
$httpBackend.expectGET(GH); $httpBackend.expectGET(GH);
$httpBackend.flush(); $httpBackend.flush();
rootScope.blockChainStatus='error'; rootScope.insightError=1;
scope.$apply(); scope.$apply();
expect(rootScope.insightError).equal(1); expect(rootScope.insightError).equal(1);
rootScope.blockChainStatus='ok';
scope.$apply(); scope.$apply();
expect(rootScope.insightError).equal(1); expect(rootScope.insightError).equal(1);
rootScope.blockChainStatus='restored';
scope.$apply(); scope.$apply();
expect(rootScope.insightError).equal(0);
}); });
}); });