3 var async = require('async')
4 var config = require('config')
5 var request = require('request')
6 var replay = require('request-replay')
8 var constants = require('../initializers/constants')
9 var logger = require('./logger')
10 var peertubeCrypto = require('./peertubeCrypto')
12 var http = config.get('webserver.https') ? 'https' : 'http'
13 var host = config.get('webserver.host')
14 var port = config.get('webserver.port')
17 makeMultipleRetryRequest: makeMultipleRetryRequest
20 function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) {
22 callback = callbackEach
26 var url = http + '://' + host + ':' + port
29 // Add signature if it is specified in the params
30 if (all_data.method === 'POST' && all_data.data && all_data.sign === true) {
31 signature = peertubeCrypto.sign(url)
34 // Make a request for each pod
35 async.each(pods, function (pod, callback_each_async) {
36 function callbackEachRetryRequest (err, response, body, url, pod) {
37 if (callbackEach !== null) {
38 callbackEach(err, response, body, url, pod, function () {
47 url: pod.url + all_data.path,
48 method: all_data.method
51 // Add data with POST requst ?
52 if (all_data.method === 'POST' && all_data.data) {
54 if (all_data.encrypt === true) {
56 ;(function (copy_params, copy_url, copy_pod, copy_signature) {
57 peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) {
58 if (err) return callback(err)
65 makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest)
67 })(params, url, pod, signature)
69 params.json = { data: all_data.data }
70 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
73 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
78 // ---------------------------------------------------------------------------
80 module.exports = requests
82 // ---------------------------------------------------------------------------
84 function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) {
85 // Append the signature
87 params.json.signature = {
93 logger.debug('Make retry requests to %s.', to_pod.url)
96 request.post(params, function (err, response, body) {
97 callbackEach(err, response, body, params.url, to_pod)
100 retries: constants.REQUEST_RETRIES,
102 maxTimeout: Infinity,
103 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
105 ).on('replay', function (replay) {
106 logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
107 params.url, replay.error.code, replay.error.message, replay.number, replay.delay)