aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/constants.js37
-rw-r--r--src/database.js3
-rw-r--r--src/pods.js9
-rw-r--r--src/poolRequests.js20
-rw-r--r--src/utils.js9
5 files changed, 52 insertions, 26 deletions
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 @@
1;(function () {
2 'use strict'
3
4 var constants = {}
5
6 function isTestInstance () {
7 return (process.env.NODE_ENV === 'test')
8 }
9
10 // API version of our pod
11 constants.API_VERSION = 'v1'
12
13 // Score a pod has when we create it as a friend
14 constants.FRIEND_BASE_SCORE = 100
15
16 // Time to wait between requests to the friends
17 constants.INTERVAL = 60000
18
19 // Number of points we add/remove from a friend after a successful/bad request
20 constants.PODS_SCORE = {
21 MALUS: -10,
22 BONUS: 10
23 }
24
25 // Number of retries we make for the make retry requests (to friends...)
26 constants.REQUEST_RETRIES = 10
27
28 // Special constants for a test instance
29 if (isTestInstance() === true) {
30 constants.FRIEND_BASE_SCORE = 20
31 constants.INTERVAL = 10000
32 constants.REQUEST_RETRIES = 2
33 }
34
35 // ----------- Export -----------
36 module.exports = constants
37})()
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 @@
4 var config = require('config') 4 var config = require('config')
5 var mongoose = require('mongoose') 5 var mongoose = require('mongoose')
6 6
7 var constants = require('./constants')
7 var logger = require('./logger') 8 var logger = require('./logger')
8 9
9 var dbname = 'peertube' + config.get('database.suffix') 10 var dbname = 'peertube' + config.get('database.suffix')
@@ -25,7 +26,7 @@
25 var podsSchema = mongoose.Schema({ 26 var podsSchema = mongoose.Schema({
26 url: String, 27 url: String,
27 publicKey: String, 28 publicKey: String,
28 score: { type: Number, max: global.FRIEND_BASE_SCORE } 29 score: { type: Number, max: constants.FRIEND_BASE_SCORE }
29 }) 30 })
30 31
31 var PodsDB = mongoose.model('pods', podsSchema) 32 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 @@
6 var fs = require('fs') 6 var fs = require('fs')
7 var request = require('request') 7 var request = require('request')
8 8
9 var constants = require('./constants')
9 var logger = require('./logger') 10 var logger = require('./logger')
10 var PodsDB = require('./database').PodsDB 11 var PodsDB = require('./database').PodsDB
11 var poolRequests = require('./poolRequests') 12 var poolRequests = require('./poolRequests')
@@ -20,7 +21,7 @@
20 // ----------- Private functions ----------- 21 // ----------- Private functions -----------
21 22
22 function getForeignPodsList (url, callback) { 23 function getForeignPodsList (url, callback) {
23 var path = '/api/' + global.API_VERSION + '/pods' 24 var path = '/api/' + constants.API_VERSION + '/pods'
24 25
25 request.get(url + path, function (err, response, body) { 26 request.get(url + path, function (err, response, body) {
26 if (err) throw err 27 if (err) throw err
@@ -48,7 +49,7 @@
48 var params = { 49 var params = {
49 url: data.url, 50 url: data.url,
50 publicKey: data.publicKey, 51 publicKey: data.publicKey,
51 score: global.FRIEND_BASE_SCORE 52 score: constants.FRIEND_BASE_SCORE
52 } 53 }
53 54
54 PodsDB.create(params, function (err, pod) { 55 PodsDB.create(params, function (err, pod) {
@@ -142,14 +143,14 @@
142 } 143 }
143 144
144 utils.makeMultipleRetryRequest( 145 utils.makeMultipleRetryRequest(
145 { method: 'POST', path: '/api/' + global.API_VERSION + '/pods/', data: data }, 146 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
146 147
147 pods_list, 148 pods_list,
148 149
149 function eachRequest (err, response, body, url, pod, callback_each_request) { 150 function eachRequest (err, response, body, url, pod, callback_each_request) {
150 // We add the pod if it responded correctly with its public certificate 151 // We add the pod if it responded correctly with its public certificate
151 if (!err && response.statusCode === 200) { 152 if (!err && response.statusCode === 200) {
152 pods.add({ url: pod.url, publicKey: body.cert, score: global.FRIEND_BASE_SCORE }, function (err) { 153 pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
153 if (err) { 154 if (err) {
154 logger.error('Error with adding %s pod.', pod.url, { error: err }) 155 logger.error('Error with adding %s pod.', pod.url, { error: err })
155 } 156 }
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 @@
3 3
4 var async = require('async') 4 var async = require('async')
5 5
6 var constants = require('./constants')
6 var logger = require('./logger') 7 var logger = require('./logger')
7 var database = require('./database') 8 var database = require('./database')
8 var PoolRequestsDB = database.PoolRequestsDB 9 var PoolRequestsDB = database.PoolRequestsDB
@@ -11,15 +12,6 @@
11 12
12 var poolRequests = {} 13 var poolRequests = {}
13 14
14 // ----------- Constants -----------
15
16 // Time to wait between requests to the friends
17 var INTERVAL = utils.isTestInstance() ? 10000 : 60000
18 var PODS_SCORE = {
19 MALUS: -10,
20 BONUS: 10
21 }
22
23 // ----------- Private ----------- 15 // ----------- Private -----------
24 var timer = null 16 var timer = null
25 17
@@ -90,8 +82,8 @@
90 function updatePodsScore (good_pods, bad_pods) { 82 function updatePodsScore (good_pods, bad_pods) {
91 logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) 83 logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length)
92 84
93 PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: PODS_SCORE.BONUS } }, { multi: true }).exec() 85 PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec()
94 PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: PODS_SCORE.MALUS } }, { multi: true }, function (err) { 86 PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) {
95 if (err) throw err 87 if (err) throw err
96 removeBadPods() 88 removeBadPods()
97 }) 89 })
@@ -121,9 +113,9 @@
121 } 113 }
122 114
123 if (type === 'add') { 115 if (type === 'add') {
124 params.path = '/api/' + global.API_VERSION + '/remotevideos/add' 116 params.path = '/api/' + constants.API_VERSION + '/remotevideos/add'
125 } else if (type === 'remove') { 117 } else if (type === 'remove') {
126 params.path = '/api/' + global.API_VERSION + '/remotevideos/remove' 118 params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove'
127 } else { 119 } else {
128 throw new Error('Unkown pool request type.') 120 throw new Error('Unkown pool request type.')
129 } 121 }
@@ -156,7 +148,7 @@
156 // ----------- Public ----------- 148 // ----------- Public -----------
157 poolRequests.activate = function () { 149 poolRequests.activate = function () {
158 logger.info('Pool requests activated.') 150 logger.info('Pool requests activated.')
159 timer = setInterval(makePoolRequests, INTERVAL) 151 timer = setInterval(makePoolRequests, constants.INTERVAL)
160 } 152 }
161 153
162 poolRequests.addToPoolRequests = function (id, type, request) { 154 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 @@
10 var replay = require('request-replay') 10 var replay = require('request-replay')
11 var ursa = require('ursa') 11 var ursa = require('ursa')
12 12
13 var constants = require('./constants')
13 var logger = require('./logger') 14 var logger = require('./logger')
14 15
15 var utils = {} 16 var utils = {}
@@ -31,15 +32,13 @@
31 } 32 }
32 33
33 logger.debug('Make retry requests to %s.', to_pod.url) 34 logger.debug('Make retry requests to %s.', to_pod.url)
34 // Default 10 but in tests we want to be faster
35 var retries = utils.isTestInstance() ? 2 : 10
36 35
37 replay( 36 replay(
38 request.post(params, function (err, response, body) { 37 request.post(params, function (err, response, body) {
39 callbackEach(err, response, body, params.url, to_pod) 38 callbackEach(err, response, body, params.url, to_pod)
40 }), 39 }),
41 { 40 {
42 retries: retries, 41 retries: constants.REQUEST_RETRIES,
43 factor: 3, 42 factor: 3,
44 maxTimeout: Infinity, 43 maxTimeout: Infinity,
45 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] 44 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
@@ -195,9 +194,5 @@
195 process.kill(-webtorrent_process.pid) 194 process.kill(-webtorrent_process.pid)
196 } 195 }
197 196
198 utils.isTestInstance = function () {
199 return (process.env.NODE_ENV === 'test')
200 }
201
202 module.exports = utils 197 module.exports = utils
203})() 198})()