1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
'use strict'
const async = require('async')
const config = require('config')
const request = require('request')
const replay = require('request-replay')
const constants = require('../initializers/constants')
const logger = require('./logger')
const peertubeCrypto = require('./peertubeCrypto')
const http = config.get('webserver.https') ? 'https' : 'http'
const host = config.get('webserver.host')
const port = config.get('webserver.port')
const requests = {
makeMultipleRetryRequest: makeMultipleRetryRequest
}
function makeMultipleRetryRequest (allData, pods, callbackEach, callback) {
if (!callback) {
callback = callbackEach
callbackEach = null
}
const url = http + '://' + host + ':' + port
let signature
// Add signature if it is specified in the params
if (allData.method === 'POST' && allData.data && allData.sign === true) {
signature = peertubeCrypto.sign(url)
}
// Make a request for each pod
async.each(pods, function (pod, callbackEachAsync) {
function callbackEachRetryRequest (err, response, body, url, pod) {
if (callbackEach !== null) {
callbackEach(err, response, body, url, pod, function () {
callbackEachAsync()
})
} else {
callbackEachAsync()
}
}
const params = {
url: pod.url + allData.path,
method: allData.method
}
// Add data with POST requst ?
if (allData.method === 'POST' && allData.data) {
// Encrypt data ?
if (allData.encrypt === true) {
peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(allData.data), function (err, encrypted) {
if (err) return callback(err)
params.json = {
data: encrypted.data,
key: encrypted.key
}
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
})
} else {
params.json = { data: allData.data }
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
}
} else {
makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest)
}
}, callback)
}
// ---------------------------------------------------------------------------
module.exports = requests
// ---------------------------------------------------------------------------
function makeRetryRequest (params, fromUrl, toPod, signature, callbackEach) {
// Append the signature
if (signature) {
params.json.signature = {
url: fromUrl,
signature: signature
}
}
logger.debug('Make retry requests to %s.', toPod.url)
replay(
request.post(params, function (err, response, body) {
callbackEach(err, response, body, params.url, toPod)
}),
{
retries: constants.REQUEST_RETRIES,
factor: 3,
maxTimeout: Infinity,
errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ]
}
).on('replay', function (replay) {
logger.info('Replaying request to %s. Request failed: %d %s. Replay number: #%d. Will retry in: %d ms.',
params.url, replay.error.code, replay.error.message, replay.number, replay.delay)
})
}
|