aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-01-17 21:17:07 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-01-17 21:17:07 +0100
commit1e4b0080ff1b4e802f71ec1f4cbf8cbcc70cdcbd (patch)
treef9c8ed5404ce905af5b22847fca6100f31b7f9b5 /server
parentda691c46df2f3c542d4fffaaa99ef18b6ef71f53 (diff)
downloadPeerTube-1e4b0080ff1b4e802f71ec1f4cbf8cbcc70cdcbd.tar.gz
PeerTube-1e4b0080ff1b4e802f71ec1f4cbf8cbcc70cdcbd.tar.zst
PeerTube-1e4b0080ff1b4e802f71ec1f4cbf8cbcc70cdcbd.zip
Server: requests refractoring
Diffstat (limited to 'server')
-rw-r--r--server/lib/friends.js3
-rw-r--r--server/models/request.js76
2 files changed, 40 insertions, 39 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index f634aedbb..1e8037c37 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -203,7 +203,7 @@ function computeForeignPodsList (host, podsScore, callback) {
203 else podsScore[foreignPodHost] = 1 203 else podsScore[foreignPodHost] = 1
204 }) 204 })
205 205
206 callback() 206 return callback()
207 }) 207 })
208} 208}
209 209
@@ -212,6 +212,7 @@ function computeWinningPods (hosts, podsScore) {
212 // Only add a pod if it exists in more than a half base pods 212 // Only add a pod if it exists in more than a half base pods
213 const podsList = [] 213 const podsList = []
214 const baseScore = hosts.length / 2 214 const baseScore = hosts.length / 2
215
215 Object.keys(podsScore).forEach(function (podHost) { 216 Object.keys(podsScore).forEach(function (podHost) {
216 // If the pod is not me and with a good score we add it 217 // If the pod is not me and with a good score we add it
217 if (isMe(podHost) === false && podsScore[podHost] > baseScore) { 218 if (isMe(podHost) === false && podsScore[podHost] > baseScore) {
diff --git a/server/models/request.js b/server/models/request.js
index cd52ea767..baa26fc1b 100644
--- a/server/models/request.js
+++ b/server/models/request.js
@@ -118,13 +118,8 @@ function makeRequest (toPod, requestEndpoint, requestsToMake, callback) {
118 // The function fire some useful callbacks 118 // The function fire some useful callbacks
119 requests.makeSecureRequest(params, function (err, res) { 119 requests.makeSecureRequest(params, function (err, res) {
120 if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) { 120 if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
121 logger.error( 121 err = err ? err.message : 'Status code not 20x : ' + res.statusCode
122 'Error sending secure request to %s pod.', 122 logger.error('Error sending secure request to %s pod.', toPod.host, { error: err })
123 toPod.host,
124 {
125 error: err ? err.message : 'Status code not 20x : ' + res.statusCode
126 }
127 )
128 123
129 return callback(false) 124 return callback(false)
130 } 125 }
@@ -153,26 +148,7 @@ function makeRequests () {
153 } 148 }
154 149
155 // We want to group requests by destinations pod and endpoint 150 // We want to group requests by destinations pod and endpoint
156 const requestsToMakeGrouped = {} 151 const requestsToMakeGrouped = buildRequestObjects(requests)
157 Object.keys(requests).forEach(function (toPodId) {
158 requests[toPodId].forEach(function (data) {
159 const request = data.request
160 const pod = data.pod
161 const hashKey = toPodId + request.endpoint
162
163 if (!requestsToMakeGrouped[hashKey]) {
164 requestsToMakeGrouped[hashKey] = {
165 toPod: pod,
166 endpoint: request.endpoint,
167 ids: [], // request ids, to delete them from the DB in the future
168 datas: [] // requests data,
169 }
170 }
171
172 requestsToMakeGrouped[hashKey].ids.push(request.id)
173 requestsToMakeGrouped[hashKey].datas.push(request.request)
174 })
175 })
176 152
177 logger.info('Making requests to friends.') 153 logger.info('Making requests to friends.')
178 154
@@ -188,22 +164,20 @@ function makeRequests () {
188 const requestIdsToDelete = requestToMake.ids 164 const requestIdsToDelete = requestToMake.ids
189 165
190 logger.info('Removing %d requests of unexisting pod %s.', requestIdsToDelete.length, requestToMake.toPod.id) 166 logger.info('Removing %d requests of unexisting pod %s.', requestIdsToDelete.length, requestToMake.toPod.id)
191 RequestToPod.removePodOf.call(self, requestIdsToDelete, requestToMake.toPod.id) 167 return RequestToPod.removePodOf(requestIdsToDelete, requestToMake.toPod.id, callbackEach)
192 return callbackEach()
193 } 168 }
194 169
195 makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, function (success) { 170 makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, function (success) {
196 if (success === true) { 171 if (success === false) {
197 logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
198
199 goodPods.push(requestToMake.toPod.id)
200
201 // Remove the pod id of these request ids
202 RequestToPod.removePodOf(requestToMake.ids, requestToMake.toPod.id, callbackEach)
203 } else {
204 badPods.push(requestToMake.toPod.id) 172 badPods.push(requestToMake.toPod.id)
205 callbackEach() 173 return callbackEach()
206 } 174 }
175
176 logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
177 goodPods.push(requestToMake.toPod.id)
178
179 // Remove the pod id of these request ids
180 RequestToPod.removePodOf(requestToMake.ids, requestToMake.toPod.id, callbackEach)
207 }) 181 })
208 }, function () { 182 }, function () {
209 // All the requests were made, we update the pods score 183 // All the requests were made, we update the pods score
@@ -216,6 +190,32 @@ function makeRequests () {
216 }) 190 })
217} 191}
218 192
193function buildRequestObjects (requests) {
194 const requestsToMakeGrouped = {}
195
196 Object.keys(requests).forEach(function (toPodId) {
197 requests[toPodId].forEach(function (data) {
198 const request = data.request
199 const pod = data.pod
200 const hashKey = toPodId + request.endpoint
201
202 if (!requestsToMakeGrouped[hashKey]) {
203 requestsToMakeGrouped[hashKey] = {
204 toPod: pod,
205 endpoint: request.endpoint,
206 ids: [], // request ids, to delete them from the DB in the future
207 datas: [] // requests data,
208 }
209 }
210
211 requestsToMakeGrouped[hashKey].ids.push(request.id)
212 requestsToMakeGrouped[hashKey].datas.push(request.request)
213 })
214 })
215
216 return requestsToMakeGrouped
217}
218
219// Remove pods with a score of 0 (too many requests where they were unreachable) 219// Remove pods with a score of 0 (too many requests where they were unreachable)
220function removeBadPods () { 220function removeBadPods () {
221 const self = this 221 const self = this