Merge pull request #83 from lbryio/retry-search-server

Retry search on a different server if connection to Lighthouse fails (WIP)
This commit is contained in:
alexliebowitz 2016-11-14 12:01:31 -05:00 committed by GitHub
commit a1554cba5c
5 changed files with 41 additions and 18 deletions

View file

@ -10,8 +10,20 @@ var lbry = {
}
};
lbry.jsonrpc_call = function (connectionString, method, params, callback, errorCallback, connectFailedCallback) {
lbry.jsonrpc_call = function (connectionString, method, params, callback, errorCallback, connectFailedCallback, timeout) {
var xhr = new XMLHttpRequest;
if (typeof connectFailedCallback !== 'undefined') {
if (timeout) {
xhr.timeout = timeout;
}
xhr.addEventListener('error', function (e) {
connectFailedCallback(e);
});
xhr.addEventListener('timeout', function() {
connectFailedCallback(new Error('XMLHttpRequest connection timed out'));
})
}
xhr.addEventListener('load', function() {
var response = JSON.parse(xhr.responseText);
@ -24,12 +36,6 @@ lbry.jsonrpc_call = function (connectionString, method, params, callback, errorC
}
});
if (connectFailedCallback) {
xhr.addEventListener('error', function (e) {
connectFailedCallback(e);
});
}
xhr.open('POST', connectionString, true);
xhr.send(JSON.stringify({
'jsonrpc': '2.0',
@ -122,11 +128,6 @@ lbry.sendToAddress = function(amount, address, callback, errorCallback)
lbry.call("send_amount_to_address", { "amount" : amount, "address": address }, callback, errorCallback);
}
lbry.search = function(query, callback)
{
lbry.lighthouse.call('search', [query], callback);
}
lbry.resolveName = function(name, callback) {
lbry.call('resolve_name', { 'name': name }, callback, () => {
// For now, assume any error means the name was not resolved

View file

@ -1,4 +1,7 @@
lbry.lighthouse = {
_search_timeout: 5000,
_max_search_tries: 5,
servers: [
'http://lighthouse1.lbry.io:50005',
'http://lighthouse2.lbry.io:50005',
@ -6,9 +9,28 @@ lbry.lighthouse = {
],
path: '/',
call: function(method, params, callback, errorCallback, connectFailedCallback) {
lbry.jsonrpc_call(this.server + this.path, method, params, callback, errorCallback, connectFailedCallback);
call: function(method, params, callback, errorCallback, connectFailedCallback, timeout) {
lbry.jsonrpc_call(this.server + this.path, method, params, callback, errorCallback, connectFailedCallback, timeout);
},
search: function(query, callback) {
let handleSearchFailed = function(tryNum=0) {
if (tryNum > lbry.lighthouse._max_search_tries) {
throw new Error(`Could not connect to Lighthouse server. Last server attempted: ${lbry.lighthouse.server}`);
} else {
// Randomly choose one of the other search servers to switch to
let otherServers = lbry.lighthouse.servers.slice();
otherServers.splice(otherServers.indexOf(lbry.lighthouse.server), 1);
lbry.lighthouse.server = otherServers[Math.round(Math.random() * (otherServers.length - 1))];
lbry.lighthouse.call('search', [query], callback, undefined, function() {
handleSearchFailed(tryNum + 1);
}, lbry.lighthouse._search_timeout);
}
}
lbry.lighthouse.call('search', [query], callback, undefined, function() { handleSearchFailed() }, lbry.lighthouse._search_timeout);
}
};
lbry.lighthouse.server = lbry.lighthouse.servers[Math.round(Math.random() * (lbry.lighthouse.servers.length - 1))];

View file

@ -179,7 +179,7 @@ var FeaturedContentItem = React.createClass({
componentDidMount: function() {
this.resolveSearch = true;
lbry.search(this.props.name, function(results) {
lbry.lighthouse.search(this.props.name, function(results) {
var result = results[0];
var metadata = result.value;
if (this.resolveSearch)
@ -257,7 +257,7 @@ var DiscoverPage = React.createClass({
query: this.props.query,
});
lbry.search(this.props.query, this.searchCallback);
lbry.lighthouse.search(this.props.query, this.searchCallback);
},
componentDidMount: function() {

View file

@ -225,7 +225,7 @@ var MyFilesPage = React.createClass({
for (let fileInfo of filesInfo) {
let name = fileInfo.lbry_uri;
lbry.search(name, (results) => {
lbry.lighthouse.search(name, (results) => {
var result = results[0];
var available = result.name == name && result.available;

View file

@ -117,7 +117,7 @@ var DetailPage = React.createClass({
componentWillMount: function() {
document.title = 'lbry://' + this.props.name;
lbry.search(this.props.name, (results) => {
lbry.lighthouse.search(this.props.name, (results) => {
var result = results[0];
if (result.name != this.props.name) {