Skip to content

Commit

Permalink
remove useless else statement
Browse files Browse the repository at this point in the history
  • Loading branch information
arnesetzer committed Jul 26, 2024
1 parent 1b113f0 commit 9d67b39
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions lib/request_urls.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
var http = require('http');
var https = require('https');
var request = require('request');
var apiKey = require( '../lib/apiKey' );
var apiKey = require('../lib/apiKey');

var ExponentialBackoff = require( '../lib/ExponentialBackoff');
var ExponentialBackoff = require('../lib/ExponentialBackoff');

var retries = 0;

function printProgress(done, total) {
process.stderr.write(
'\rTests completed: '+ done + '/' + total + ' (retries: ' + retries + ')'
'\rTests completed: ' + done + '/' + total + ' (retries: ' + retries + ')'
);
if (done === total) {
console.log(); //print a newline
Expand All @@ -18,10 +18,10 @@ function printProgress(done, total) {

function shouldRetryRequest(res) {
var retry_codes = [408, 413, 429];
if( retry_codes.indexOf(res.statusCode) !== -1 ){
if (retry_codes.indexOf(res.statusCode) !== -1) {
return true;
} else if( res.body && res.body.geocoding && Array.isArray( res.body.geocoding.errors ) &&
res.body.geocoding.errors[0].indexOf('Request Timeout') !== -1 ){
} else if (res.body && res.body.geocoding && Array.isArray(res.body.geocoding.errors) &&
res.body.geocoding.errors[0].indexOf('Request Timeout') !== -1) {
return true;
}
return false;
Expand All @@ -31,22 +31,22 @@ function request_urls(config, urls, callback) {
const maxSockets = config.maxSockets || 1;
var total_length = urls.length;
var responses = {};
var httpAgent = new http.Agent({keepAlive: true, maxSockets: maxSockets});
var httpsAgent = new https.Agent({keepAlive: true, maxSockets: maxSockets});
var httpAgent = new http.Agent({ keepAlive: true, maxSockets: maxSockets });
var httpsAgent = new https.Agent({ keepAlive: true, maxSockets: maxSockets });
var intervalId;
const interval = 1000 / config.rate; // the number of miliseconds to delay between requests
var test_interval = new ExponentialBackoff(interval, 5, interval, 20000);
var delay = test_interval.getBackoff();

var apiUrl = config.endpoint.url;
var key = apiKey( apiUrl );
var key = apiKey(apiUrl);

var getOneUrl = function (){
var getOneUrl = function () {
// check if all responses have been recieved and call the next step in
// processing via the `callback` function
if( Object.keys(responses).length === total_length ){
if (Object.keys(responses).length === total_length) {
clearInterval(intervalId);
return callback( responses );
return callback(responses);
}

var url = urls.pop();
Expand All @@ -61,7 +61,7 @@ function request_urls(config, urls, callback) {
const agent = (url.startsWith('https:')) ? httpsAgent : httpAgent;

var requestOpts = {
headers: {'user-agent': 'pelias-fuzzy-tester'},
headers: { 'user-agent': 'pelias-fuzzy-tester' },
url: url,
json: true,
agent: agent,
Expand All @@ -76,17 +76,16 @@ function request_urls(config, urls, callback) {
requestOpts.url = url + `&${key.keyName}=${key.value}`;
}

request(requestOpts, function (err, res) {
if (err) {
console.error(err);
request( requestOpts, function ( err, res ) {
if ( err ) {
console.error( err );
//Check if hostname is valid. Abort execution if not
if (err.code === 'ENOTFOUND') {
throw new Error('Invalid hostname, exiting');
if ( err.code === 'ENOTFOUND' ) {
throw new Error( 'Invalid hostname, exiting' );
}
else {
urls.push(url);
urls.push(url);
}
else if( shouldRetryRequest(res) ){
else if ( shouldRetryRequest(res) ) {
urls.push(url);
retries++;
printProgress(Object.keys(responses).length, total_length);
Expand All @@ -99,9 +98,9 @@ function request_urls(config, urls, callback) {
printProgress(Object.keys(responses).length, total_length);
}

if( Object.keys(responses).length === total_length ){
if (Object.keys(responses).length === total_length) {
clearInterval(intervalId);
callback( responses );
callback(responses);
} else {
if (delay !== test_interval.getBackoff()) {
delay = test_interval.getBackoff();
Expand Down

0 comments on commit 9d67b39

Please sign in to comment.