diff --git a/js/lbry.js b/js/lbry.js index d1216b6f1..eec987bed 100644 --- a/js/lbry.js +++ b/js/lbry.js @@ -122,11 +122,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 diff --git a/js/lighthouse.js b/js/lighthouse.js index e72322486..93cea4ceb 100644 --- a/js/lighthouse.js +++ b/js/lighthouse.js @@ -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))]; diff --git a/js/page/discover.js b/js/page/discover.js index 8445414af..239525b0e 100644 --- a/js/page/discover.js +++ b/js/page/discover.js @@ -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() { diff --git a/js/page/my_files.js b/js/page/my_files.js index 26d5fed42..ea9ca10e4 100644 --- a/js/page/my_files.js +++ b/js/page/my_files.js @@ -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; diff --git a/js/page/show.js b/js/page/show.js index a7c28030c..939095575 100644 --- a/js/page/show.js +++ b/js/page/show.js @@ -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) {