From 5bcf09560073ed31700d64451b3c584badc9c2e4 Mon Sep 17 00:00:00 2001 From: Alex Liebowitz Date: Thu, 29 Dec 2016 04:57:07 -0500 Subject: [PATCH] Refactor lighthouse.js and add support for custom servers - Adds support for custom servers - Some light refactoring; mainly moving the logic for retrying queries into lbry.call() instead of leaving it up to the individual methods --- js/lighthouse.js | 57 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/js/lighthouse.js b/js/lighthouse.js index 0a5de3152..6d349a3a8 100644 --- a/js/lighthouse.js +++ b/js/lighthouse.js @@ -1,8 +1,8 @@ import lbry from './lbry.js'; var lighthouse = { - _search_timeout: 5000, - _max_search_tries: 5, + _query_timeout: 5000, + _max_query_tries: 5, servers: [ 'http://lighthouse4.lbry.io:50005', @@ -12,37 +12,56 @@ var lighthouse = { path: '/', 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, errorCallback, connectFailedCallback, timeout) { - let handleSearchFailed = function(tryNum=0) { - if (tryNum > lighthouse._max_search_tries) { + const handleConnectFailed = function(tryNum=0) { + if (tryNum > lighthouse._max_query_tries) { if (connectFailedCallback) { connectFailedCallback(); } else { throw new Error(`Could not connect to Lighthouse server. Last server attempted: ${lighthouse.server}`); } } else { - // Randomly choose one of the other search servers to switch to - let otherServers = lighthouse.servers.slice(); - otherServers.splice(otherServers.indexOf(lighthouse.server), 1); - lighthouse.server = otherServers[Math.round(Math.random() * (otherServers.length - 1))]; - - lighthouse.call('search', [query], callback, errorCallback, function() { - handleSearchFailed(tryNum + 1); - }, lighthouse._search_timeout); + lbry.call(method, params, callback, errorCallback, () => { handleConnectFailed(tryNum + 1) }, timeout); } } - lighthouse.call('search', [query], callback, errorCallback, function() { handleSearchFailed() }, lighthouse._search_timeout); + // Set the Lighthouse server if it hasn't been set yet, or if the current server is not in + // the current set of servers (most likely because of a settings change). + if (typeof lighthouse.server === 'undefined' || lighthouse.getServers().indexOf(lighthouse.server) == -1) { + lighthouse.chooseNewServer(); + } + + lbry.jsonrpc_call(this.server + this.path, method, params, callback, errorCallback, + () => { handleConnectFailed() }, timeout || lighthouse.query_timeout); + }, + + getServers: function() { + return lbry.getClientSetting('useCustomLighthouseServers') + ? lbry.getClientSetting('customLighthouseServers') + : lighthouse.servers; + }, + + chooseNewServer: function() { + // Randomly choose a new Lighthouse server and switch to it. If a server is already set, this + // will choose a different one (used for switching servers after a failed query). + const servers = lighthouse.getServers(); + let newServerChoices; + if (!lighthouse.server) { + newServerChoices = servers; + } else { + newServerChoices = lighthouse.getServers().slice(); + newServerChoices.splice(newServerChoices.indexOf(lighthouse.server), 1); + } + lighthouse.server = newServerChoices[Math.round(Math.random() * (newServerChoices.length - 1))]; + }, + + search: function(query, callback, errorCallback, connectFailedCallback, timeout) { + lighthouse.call('search', [query], callback, errorCallback, connectFailedCallback, timeout); }, getSizeForName: function(name, callback, errorCallback, connectFailedCallback, timeout) { - return lighthouse.call('get_size_for_name', [name], callback, errorCallback, connectFailedCallback, timeout); + lighthouse.call('get_size_for_name', [name], callback, errorCallback, connectFailedCallback, timeout); } }; -lighthouse.server = lighthouse.servers[Math.round(Math.random() * (lighthouse.servers.length - 1))]; export default lighthouse;