Move lbry.search() to lbry.lighthouse and add connection retry logic

This commit is contained in:
Alex Liebowitz 2016-11-11 08:17:43 -05:00
parent 14565ec586
commit e0d68a9619
5 changed files with 28 additions and 11 deletions

View file

@ -122,11 +122,6 @@ lbry.sendToAddress = function(amount, address, callback, errorCallback)
lbry.call("send_amount_to_address", { "amount" : amount, "address": 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.resolveName = function(name, callback) {
lbry.call('resolve_name', { 'name': name }, callback, () => { lbry.call('resolve_name', { 'name': name }, callback, () => {
// For now, assume any error means the name was not resolved // For now, assume any error means the name was not resolved

View file

@ -1,4 +1,7 @@
lbry.lighthouse = { lbry.lighthouse = {
_search_timeout: 5000,
_max_search_tries: 5,
servers: [ servers: [
'http://lighthouse1.lbry.io:50005', 'http://lighthouse1.lbry.io:50005',
'http://lighthouse2.lbry.io:50005', 'http://lighthouse2.lbry.io:50005',
@ -6,9 +9,28 @@ lbry.lighthouse = {
], ],
path: '/', path: '/',
call: function(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); 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))]; 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() { componentDidMount: function() {
this.resolveSearch = true; this.resolveSearch = true;
lbry.search(this.props.name, function(results) { lbry.lighthouse.search(this.props.name, function(results) {
var result = results[0]; var result = results[0];
var metadata = result.value; var metadata = result.value;
if (this.resolveSearch) if (this.resolveSearch)
@ -257,7 +257,7 @@ var DiscoverPage = React.createClass({
query: this.props.query, query: this.props.query,
}); });
lbry.search(this.props.query, this.searchCallback); lbry.lighthouse.search(this.props.query, this.searchCallback);
}, },
componentDidMount: function() { componentDidMount: function() {

View file

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

View file

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