mirror of https://github.com/BTCPrivate/copay.git
Merge pull request #565 from matiu/feature/reconnectInsight
insight REST API retry
This commit is contained in:
commit
69e31e2a79
|
@ -109,12 +109,15 @@ var defaultConfig = {
|
|||
// blockchain service API config
|
||||
blockchain: {
|
||||
host: 'test.insight.is',
|
||||
port: 80
|
||||
port: 80,
|
||||
retryDelay: 1000,
|
||||
},
|
||||
// socket service API config
|
||||
socket: {
|
||||
host: 'test.insight.is',
|
||||
port: 80
|
||||
port: 80,
|
||||
// will duplicate itself after each try
|
||||
reconnectDelay: 500,
|
||||
},
|
||||
|
||||
// local encryption/security config
|
||||
|
|
|
@ -82,12 +82,12 @@
|
|||
</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 data-alert class="alert-box radius error">
|
||||
Error connecting to Insight server. Check
|
||||
you settings and Internet connection.
|
||||
Trying to reconnect...
|
||||
Having troubles connecting to Insight server. Check
|
||||
you settings and Internet connection.<br>
|
||||
Reconnect Atempt #{{$root.insightError}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -35,19 +35,15 @@ angular.module('copayApp.controllers').controller('HeaderController',
|
|||
|
||||
// Initialize alert notification (not show when init wallet)
|
||||
$rootScope.txAlertCount = 0;
|
||||
$rootScope.insightError = -1;
|
||||
|
||||
$rootScope.$watch('blockChainStatus', function(status) {
|
||||
var h = config.blockchain.host + ':' + config.blockchain.port;
|
||||
if (status === 'error')
|
||||
$rootScope.insightError = 1;
|
||||
else
|
||||
if (status === 'restored') {
|
||||
$rootScope.insightError = 0;
|
||||
$rootScope.$flashMessage = {
|
||||
type: 'success',
|
||||
message: 'Networking Restored',
|
||||
};
|
||||
}
|
||||
$rootScope.$watch('insightError', function(status) {
|
||||
if (status === 0) {
|
||||
$rootScope.$flashMessage = {
|
||||
type: 'success',
|
||||
message: 'Networking Restored :)',
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@ function Insight(opts) {
|
|||
this.host = opts.host || 'localhost';
|
||||
this.port = opts.port || '3001';
|
||||
this.scheme = opts.scheme || 'http';
|
||||
this.retryDelay = opts.retryDelay || 5000;
|
||||
}
|
||||
|
||||
function _asyncForEach(array, fn, callback) {
|
||||
|
@ -123,11 +124,13 @@ Insight.prototype.getUnspent = function(addresses, cb) {
|
|||
}
|
||||
};
|
||||
|
||||
var self = this;
|
||||
this._request(options, function(err, res) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
|
||||
if (res && res.length > 0) {
|
||||
all = all.concat(res);
|
||||
}
|
||||
|
@ -157,6 +160,9 @@ Insight.prototype.sendRawTransaction = function(rawtx, cb) {
|
|||
};
|
||||
|
||||
Insight.prototype._request = function(options, callback) {
|
||||
|
||||
|
||||
var self = this;
|
||||
if (typeof process === 'undefined' || !process.version) {
|
||||
var request = new XMLHttpRequest();
|
||||
|
||||
|
@ -174,28 +180,38 @@ Insight.prototype._request = function(options, callback) {
|
|||
}
|
||||
|
||||
request.open(options.method, url, true);
|
||||
request.timeout = 10000;
|
||||
request.timeout = 5000;
|
||||
request.ontimeout = function() {
|
||||
return callback({
|
||||
message: 'Insight request timeout. Please check your Insight settings or the Insight server status.'
|
||||
});
|
||||
console.log('Insight timeout...retrying', err, self.retryDelay); //TODO
|
||||
setTimeout(function() {
|
||||
return self._request(options,callback);
|
||||
}, self.retryDelay);
|
||||
return callback(new Error('Insight request timeout'));
|
||||
};
|
||||
|
||||
request.onreadystatechange = function() {
|
||||
if (request.readyState === 4) {
|
||||
if (request.status === 200 || request.status === 304 || request.status === 0) {
|
||||
if (request.status === 200 || request.status === 304) {
|
||||
try {
|
||||
var ret = JSON.parse(request.responseText);
|
||||
return callback(null, ret);
|
||||
} catch (e) {
|
||||
return callback({
|
||||
message: 'Wrong response from insight'
|
||||
});
|
||||
return callback(new Error('CRITICAL: Wrong response from insight'));
|
||||
}
|
||||
} else {
|
||||
return callback({
|
||||
message: 'Error code: ' + request.status + ' - Status: ' + request.statusText + ' - Description: ' + request.responseText
|
||||
});
|
||||
}
|
||||
// User error
|
||||
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);
|
||||
} else {
|
||||
}
|
||||
|
||||
else {
|
||||
var http = require('http');
|
||||
var req = http.request(options, function(response) {
|
||||
var ret;
|
||||
|
|
|
@ -101,21 +101,20 @@ angular.module('copayApp.services')
|
|||
|
||||
root.updateBalance = function(cb) {
|
||||
var w = $rootScope.wallet;
|
||||
if (!w) return root.onErrorDigest();
|
||||
|
||||
|
||||
$rootScope.balanceByAddr = {};
|
||||
$rootScope.updatingBalance = true;
|
||||
w.getBalance(function(err, balance, balanceByAddr, safeBalance) {
|
||||
if (err) {
|
||||
$rootScope.$flashMessage = {
|
||||
type: 'error',
|
||||
message: 'Error: ' + err.message
|
||||
};
|
||||
|
||||
$rootScope.$digest();
|
||||
console.error('Error: ' + err.message); //TODO
|
||||
|
||||
root._setCommError();
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
root._clearCommError();
|
||||
}
|
||||
|
||||
$rootScope.totalBalance = balance;
|
||||
$rootScope.balanceByAddr = balanceByAddr;
|
||||
|
@ -175,13 +174,15 @@ angular.module('copayApp.services')
|
|||
};
|
||||
|
||||
root._setCommError = function(e) {
|
||||
$rootScope.blockChainStatus='error';
|
||||
// first error ever?
|
||||
if ($rootScope.insightError<0)
|
||||
$rootScope.insightError=0;
|
||||
$rootScope.insightError++;
|
||||
};
|
||||
|
||||
|
||||
root._clearCommError = function(e) {
|
||||
if ($rootScope.blockChainStatus==='error')
|
||||
$rootScope.blockChainStatus='restored';
|
||||
$rootScope.insightError=0;
|
||||
};
|
||||
|
||||
root.setSocketHandlers = function() {
|
||||
|
|
|
@ -6,7 +6,7 @@ angular.module('copayApp.services').factory('Socket',
|
|||
var url = 'http://' + config.socket.host + ':' + config.socket.port;
|
||||
var socket = io(url, {
|
||||
'reconnection': true,
|
||||
'reconnectionDelay': 500,
|
||||
'reconnectionDelay': config.socket.reconnectDelay || 500,
|
||||
});
|
||||
|
||||
return {
|
||||
|
|
|
@ -103,15 +103,12 @@ describe("Unit: Controllers", function() {
|
|||
it('should check blockChainStatus', function() {
|
||||
$httpBackend.expectGET(GH);
|
||||
$httpBackend.flush();
|
||||
rootScope.blockChainStatus='error';
|
||||
rootScope.insightError=1;
|
||||
scope.$apply();
|
||||
expect(rootScope.insightError).equal(1);
|
||||
rootScope.blockChainStatus='ok';
|
||||
scope.$apply();
|
||||
expect(rootScope.insightError).equal(1);
|
||||
rootScope.blockChainStatus='restored';
|
||||
scope.$apply();
|
||||
expect(rootScope.insightError).equal(0);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue