]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
dac0a531 | 2 | |
f0f5567b C |
3 | const async = require('async') |
4 | const config = require('config') | |
5 | const request = require('request') | |
6 | const replay = require('request-replay') | |
dac0a531 | 7 | |
f0f5567b C |
8 | const constants = require('../initializers/constants') |
9 | const logger = require('./logger') | |
10 | const peertubeCrypto = require('./peertubeCrypto') | |
dac0a531 | 11 | |
f0f5567b C |
12 | const http = config.get('webserver.https') ? 'https' : 'http' |
13 | const host = config.get('webserver.host') | |
14 | const port = config.get('webserver.port') | |
dac0a531 | 15 | |
f0f5567b | 16 | const requests = { |
9f10b292 C |
17 | makeMultipleRetryRequest: makeMultipleRetryRequest |
18 | } | |
dac0a531 | 19 | |
9f10b292 C |
20 | function makeMultipleRetryRequest (all_data, pods, callbackEach, callback) { |
21 | if (!callback) { | |
22 | callback = callbackEach | |
23 | callbackEach = null | |
24 | } | |
dac0a531 | 25 | |
f0f5567b C |
26 | const url = http + '://' + host + ':' + port |
27 | let signature | |
dac0a531 | 28 | |
9f10b292 C |
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) | |
32 | } | |
dac0a531 | 33 | |
9f10b292 C |
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 () { | |
dac0a531 | 39 | callback_each_async() |
9f10b292 C |
40 | }) |
41 | } else { | |
42 | callback_each_async() | |
dac0a531 | 43 | } |
9f10b292 | 44 | } |
dac0a531 | 45 | |
f0f5567b | 46 | const params = { |
9f10b292 C |
47 | url: pod.url + all_data.path, |
48 | method: all_data.method | |
49 | } | |
dac0a531 | 50 | |
9f10b292 C |
51 | // Add data with POST requst ? |
52 | if (all_data.method === 'POST' && all_data.data) { | |
53 | // Encrypt data ? | |
54 | if (all_data.encrypt === true) { | |
f0f5567b C |
55 | peertubeCrypto.encrypt(pod.publicKey, JSON.stringify(all_data.data), function (err, encrypted) { |
56 | if (err) return callback(err) | |
9f10b292 | 57 | |
f0f5567b C |
58 | params.json = { |
59 | data: encrypted.data, | |
60 | key: encrypted.key | |
61 | } | |
9f10b292 | 62 | |
f0f5567b C |
63 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) |
64 | }) | |
dac0a531 | 65 | } else { |
9f10b292 | 66 | params.json = { data: all_data.data } |
dac0a531 C |
67 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) |
68 | } | |
9f10b292 C |
69 | } else { |
70 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) | |
71 | } | |
72 | }, callback) | |
73 | } | |
dac0a531 | 74 | |
9f10b292 | 75 | // --------------------------------------------------------------------------- |
dac0a531 | 76 | |
9f10b292 | 77 | module.exports = requests |
dac0a531 | 78 | |
9f10b292 | 79 | // --------------------------------------------------------------------------- |
dac0a531 | 80 | |
9f10b292 C |
81 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { |
82 | // Append the signature | |
83 | if (signature) { | |
84 | params.json.signature = { | |
85 | url: from_url, | |
86 | signature: signature | |
dac0a531 | 87 | } |
dac0a531 | 88 | } |
9f10b292 C |
89 | |
90 | logger.debug('Make retry requests to %s.', to_pod.url) | |
91 | ||
92 | replay( | |
93 | request.post(params, function (err, response, body) { | |
94 | callbackEach(err, response, body, params.url, to_pod) | |
95 | }), | |
96 | { | |
97 | retries: constants.REQUEST_RETRIES, | |
98 | factor: 3, | |
99 | maxTimeout: Infinity, | |
100 | errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] | |
101 | } | |
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) | |
105 | }) | |
106 | } |