]>
Commit | Line | Data |
---|---|---|
9f10b292 | 1 | 'use strict' |
dac0a531 | 2 | |
9f10b292 C |
3 | var async = require('async') |
4 | var config = require('config') | |
5 | var request = require('request') | |
6 | var replay = require('request-replay') | |
dac0a531 | 7 | |
9f10b292 C |
8 | var constants = require('../initializers/constants') |
9 | var logger = require('./logger') | |
10 | var peertubeCrypto = require('./peertubeCrypto') | |
dac0a531 | 11 | |
9f10b292 C |
12 | var http = config.get('webserver.https') ? 'https' : 'http' |
13 | var host = config.get('webserver.host') | |
14 | var port = config.get('webserver.port') | |
dac0a531 | 15 | |
9f10b292 C |
16 | var requests = { |
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 | |
9f10b292 C |
26 | var url = http + '://' + host + ':' + port |
27 | var 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 | |
9f10b292 C |
46 | var params = { |
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) { | |
55 | // TODO: ES6 with let | |
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) | |
59 | ||
60 | copy_params.json = { | |
61 | data: encrypted.data, | |
62 | key: encrypted.key | |
63 | } | |
64 | ||
65 | makeRetryRequest(copy_params, copy_url, copy_pod, copy_signature, callbackEachRetryRequest) | |
66 | }) | |
67 | })(params, url, pod, signature) | |
dac0a531 | 68 | } else { |
9f10b292 | 69 | params.json = { data: all_data.data } |
dac0a531 C |
70 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) |
71 | } | |
9f10b292 C |
72 | } else { |
73 | makeRetryRequest(params, url, pod, signature, callbackEachRetryRequest) | |
74 | } | |
75 | }, callback) | |
76 | } | |
dac0a531 | 77 | |
9f10b292 | 78 | // --------------------------------------------------------------------------- |
dac0a531 | 79 | |
9f10b292 | 80 | module.exports = requests |
dac0a531 | 81 | |
9f10b292 | 82 | // --------------------------------------------------------------------------- |
dac0a531 | 83 | |
9f10b292 C |
84 | function makeRetryRequest (params, from_url, to_pod, signature, callbackEach) { |
85 | // Append the signature | |
86 | if (signature) { | |
87 | params.json.signature = { | |
88 | url: from_url, | |
89 | signature: signature | |
dac0a531 | 90 | } |
dac0a531 | 91 | } |
9f10b292 C |
92 | |
93 | logger.debug('Make retry requests to %s.', to_pod.url) | |
94 | ||
95 | replay( | |
96 | request.post(params, function (err, response, body) { | |
97 | callbackEach(err, response, body, params.url, to_pod) | |
98 | }), | |
99 | { | |
100 | retries: constants.REQUEST_RETRIES, | |
101 | factor: 3, | |
102 | maxTimeout: Infinity, | |
103 | errorCodes: [ 'EADDRINFO', 'ETIMEDOUT', 'ECONNRESET', 'ESOCKETTIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED' ] | |
104 | } | |
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) | |
108 | }) | |
109 | } |