From 656ea8f70e848b03c0b668584dd533d72b376498 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 14 Dec 2015 21:09:25 +0100 Subject: Create a constants module to easily modify some constants in a test instance for example. --- src/constants.js | 37 +++++++++++++++++++++++++++++++++++++ src/database.js | 3 ++- src/pods.js | 9 +++++---- src/poolRequests.js | 20 ++++++-------------- src/utils.js | 9 ++------- 5 files changed, 52 insertions(+), 26 deletions(-) create mode 100644 src/constants.js (limited to 'src') diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 000000000..00b713961 --- /dev/null +++ b/src/constants.js @@ -0,0 +1,37 @@ +;(function () { + 'use strict' + + var constants = {} + + function isTestInstance () { + return (process.env.NODE_ENV === 'test') + } + + // API version of our pod + constants.API_VERSION = 'v1' + + // Score a pod has when we create it as a friend + constants.FRIEND_BASE_SCORE = 100 + + // Time to wait between requests to the friends + constants.INTERVAL = 60000 + + // Number of points we add/remove from a friend after a successful/bad request + constants.PODS_SCORE = { + MALUS: -10, + BONUS: 10 + } + + // Number of retries we make for the make retry requests (to friends...) + constants.REQUEST_RETRIES = 10 + + // Special constants for a test instance + if (isTestInstance() === true) { + constants.FRIEND_BASE_SCORE = 20 + constants.INTERVAL = 10000 + constants.REQUEST_RETRIES = 2 + } + + // ----------- Export ----------- + module.exports = constants +})() diff --git a/src/database.js b/src/database.js index 514a622dc..e03176b31 100644 --- a/src/database.js +++ b/src/database.js @@ -4,6 +4,7 @@ var config = require('config') var mongoose = require('mongoose') + var constants = require('./constants') var logger = require('./logger') var dbname = 'peertube' + config.get('database.suffix') @@ -25,7 +26,7 @@ var podsSchema = mongoose.Schema({ url: String, publicKey: String, - score: { type: Number, max: global.FRIEND_BASE_SCORE } + score: { type: Number, max: constants.FRIEND_BASE_SCORE } }) var PodsDB = mongoose.model('pods', podsSchema) diff --git a/src/pods.js b/src/pods.js index a0dbb6b68..8da216a55 100644 --- a/src/pods.js +++ b/src/pods.js @@ -6,6 +6,7 @@ var fs = require('fs') var request = require('request') + var constants = require('./constants') var logger = require('./logger') var PodsDB = require('./database').PodsDB var poolRequests = require('./poolRequests') @@ -20,7 +21,7 @@ // ----------- Private functions ----------- function getForeignPodsList (url, callback) { - var path = '/api/' + global.API_VERSION + '/pods' + var path = '/api/' + constants.API_VERSION + '/pods' request.get(url + path, function (err, response, body) { if (err) throw err @@ -48,7 +49,7 @@ var params = { url: data.url, publicKey: data.publicKey, - score: global.FRIEND_BASE_SCORE + score: constants.FRIEND_BASE_SCORE } PodsDB.create(params, function (err, pod) { @@ -142,14 +143,14 @@ } utils.makeMultipleRetryRequest( - { method: 'POST', path: '/api/' + global.API_VERSION + '/pods/', data: data }, + { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, pods_list, function eachRequest (err, response, body, url, pod, callback_each_request) { // We add the pod if it responded correctly with its public certificate if (!err && response.statusCode === 200) { - pods.add({ url: pod.url, publicKey: body.cert, score: global.FRIEND_BASE_SCORE }, function (err) { + pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { if (err) { logger.error('Error with adding %s pod.', pod.url, { error: err }) } diff --git a/src/poolRequests.js b/src/poolRequests.js index 190fb3659..edb12b1e8 100644 --- a/src/poolRequests.js +++ b/src/poolRequests.js @@ -3,6 +3,7 @@ var async = require('async') + var constants = require('./constants') var logger = require('./logger') var database = require('./database') var PoolRequestsDB = database.PoolRequestsDB @@ -11,15 +12,6 @@ var poolRequests = {} - // ----------- Constants ----------- - - // Time to wait between requests to the friends - var INTERVAL = utils.isTestInstance() ? 10000 : 60000 - var PODS_SCORE = { - MALUS: -10, - BONUS: 10 - } - // ----------- Private ----------- var timer = null @@ -90,8 +82,8 @@ function updatePodsScore (good_pods, bad_pods) { logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) - PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: PODS_SCORE.BONUS } }, { multi: true }).exec() - PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: PODS_SCORE.MALUS } }, { multi: true }, function (err) { + PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec() + PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) { if (err) throw err removeBadPods() }) @@ -121,9 +113,9 @@ } if (type === 'add') { - params.path = '/api/' + global.API_VERSION + '/remotevideos/add' + params.path = '/api/' + constants.API_VERSION + '/remotevideos/add' } else if (type === 'remove') { - params.path = '/api/' + global.API_VERSION + '/remotevideos/remove' + params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove' } else { throw new Error('Unkown pool request type.') } @@ -156,7 +148,7 @@ // ----------- Public ----------- poolRequests.activate = function () { logger.info('Pool requests activated.') - timer = setInterval(makePoolRequests, INTERVAL) + timer = setInterval(makePoolRequests, constants.INTERVAL) } poolRequests.addToPoolRequests = function (id, type, request) { diff --git a/src/utils.js b/src/utils.js index 30edcd0e9..5880c6c90 100644 --- a/src/utils.js +++ b/src/utils.js @@ -10,6 +10,7 @@ var replay = require('request-replay') var ursa = require('ursa') + var constants = require('./constants') var logger = require('./logger') var utils = {} @@ -31,15 +32,13 @@ } logger.debug('Make retry requests to %s.', to_pod.url) - // Default 10 but in tests we want to be faster - var retries = utils.isTestInstance() ? 2 : 10 replay( request.post(params, function (err, response, body) { callbackEach(err, response, body, params.url, to_pod) }), { - retries: retries, + retries: constants.REQUEST_RETRIES, factor: 3, maxTimeout: Infinity, errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] @@ -195,9 +194,5 @@ process.kill(-webtorrent_process.pid) } - utils.isTestInstance = function () { - return (process.env.NODE_ENV === 'test') - } - module.exports = utils })() -- cgit v1.2.3