Skip to content

Commit

Permalink
Merge pull request #16 from Someguy123/auto-node-switch
Browse files Browse the repository at this point in the history
Alternate nodes
  • Loading branch information
Rishi556 authored Nov 9, 2020
2 parents ec30bf9 + 0c07f2d commit 8810d08
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
12 changes: 11 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ console.log('-------------');
log(`Loaded configuration:
Username: ${config.name}
Bias: ${config.peg ? config.peg_multi : 'Disabled'}
RPC Node: ${config.node}`);
RPC Node: ${config.node}
Alternate Nodes: ${config.alternate_nodes.join(", ")}`);
console.log('-------------');

global.verbose = false;
Expand Down Expand Up @@ -80,6 +81,7 @@ class HiveAcc {
var msg = ('message' in err) ? err.message : err;
console.error('The error returned was:', msg);
if(tries < retry_conf.login_attempts) {
switchNode()
console.error(`Will retry in ${retry_conf.login_delay} seconds`);
return delay(retry_conf.login_delay * 1000)
.then(() => resolve(this.loadAccount(reload, tries+1)))
Expand Down Expand Up @@ -150,6 +152,7 @@ class HiveAcc {
var msg = ('message' in err) ? err.message : err;
console.error(`reason: ${msg}`);
if(tries < retry_conf.feed_attempts) {
switchNode()
console.error(`Will retry in ${retry_conf.feed_delay} seconds`);
return delay(retry_conf.feed_delay * 1000)
.then(() => this.publish_feed(rate, tries+1))
Expand Down Expand Up @@ -203,6 +206,13 @@ function main() {
});
}

function switchNode(){
let currentNode = config.alternate_nodes.shift()
hive.api.setOptions({ url: currentNode })
config.alternate_nodes.push(currentNode)
console.log(`Switched node to ${currentNode}.`)
}

log('Attempting to login into account', config.name);
accountmgr.login().then((user_data) => {
var {username} = user_data;
Expand Down
1 change: 1 addition & 0 deletions config.advanced.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"peg": false,
"peg_multi": 1.0,
"node": "https://hivded.privex.io",
"alternate_nodes" : ["https://api.deathwing.me", "https://api.hive.blog"],
"ex_symbol": "hive",
"ex_compare": "usd",
"base_symbol": "HBD",
Expand Down
6 changes: 6 additions & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var settings = {
var defaults = {
hive: {
node: 'https://hived.privex.io/',
alternate_nodes : ["https://api.deathwing.me", "https://api.hive.blog"],
ex_symbol: 'hive', ex_compare: 'usd',
base_symbol: 'HBD', quote_symbol: 'HIVE',
}
Expand All @@ -29,6 +30,7 @@ if(!('network' in config)) { config.network = 'hive'; }
var ndef = defaults[config.network];

if(!('node' in config)) { config.node = ndef.node; }
if(!('alternate_nodes' in config)) { config.alternate_nodes = ndef.alternate_nodes; }
// disable peg by default. 0% peg (bias)
if(!('peg' in config)) { config.peg = false; }
if(!('peg_multi' in config)) { config.peg_multi = 1; }
Expand Down Expand Up @@ -57,6 +59,10 @@ for(var t_arg of process.argv) {
if (t_arg == 'publishonce') settings.publishOnce = true;
}

if (!config.alternate_nodes.includes(config.node)){
config.alternate_nodes.push(config.node)
}

settings.config = config;

module.exports = settings;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hivefeed-js",
"version": "4.0.1",
"version": "4.1.0",
"description": "Hive Price Feed in JS with automatic retry for down nodes",
"repository": "https://github.com/someguy123/hivefeed-js",
"main": "app.js",
Expand Down

0 comments on commit 8810d08

Please sign in to comment.