3 const async = require('async')
4 const config = require('config')
5 const request = require('request')
6 const replay = require('request-replay')
8 const constants = require('../initializers/constants')
9 const logger = require('./logger')
10 const peertubeCrypto = require('./peertubeCrypto')
12 const http = config.get('webserver.https') ? 'https' : 'http'
13 const host = config.get('webserver.host')
14 const port = config.get('webserver.port')
17 makeMultipleRetryRequest: makeMultipleRetryRequest
20 function makeMultipleRetryRequest (allData, pods, callbackEach, callback) {
22 callback = callbackEach
26 const url = http + '://' + host + ':' + port
29 // Add signature if it is specified in the params
30 if (allData.method === 'POST' && allData.data && allData.sign === true) {
31 signature = peertubeCrypto.sign(url)
34 // Make a request for each pod
35 async.each(pods, function (pod, callbackEachAsync) {
36 function callbackEachRetryRequest (err, response, body, url, pod) {
37 if (callbackEach !== null) {
38 callbackEach(err, response, body, url, pod, function () {
47 url: pod.url + allData.path,
48 method: allData.method
51 // Add data with POST requst ?
52 if (allData.method === 'POST' && allData.data) {
54 if (allData.encrypt === true) {
55 peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(allData.data), function (err, encrypted) {
56 if (err) return callback(err)
63 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
66 params.json = { data: allData.data }
67 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
70 makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
75 // ---------------------------------------------------------------------------
77 module.exports = requests
79 // ---------------------------------------------------------------------------
81 function makeRetryRequest (params, fromUrl, toPod, signature, callbackEach) {
82 // Append the signature
84 params.json.signature = {
90 logger.debug('Make retry requests to %s.', toPod.url)
93 request.post(params, function (err, response, body) {
94 callbackEach(err, response, body, params.url, toPod)
97 retries: constants.REQUEST_RETRIES,
100 errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
102 ).on('replay', function (replay) {
103 logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
104 params.url, replay.error.code, replay.error.message, replay.number, replay.delay)