]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Server: requests refractoring
authorChocobozzz <florian.bigard@gmail.com>
Tue, 17 Jan 2017 20:17:07 +0000 (21:17 +0100)
committerChocobozzz <florian.bigard@gmail.com>
Tue, 17 Jan 2017 20:17:07 +0000 (21:17 +0100)
server/lib/friends.js
server/models/request.js

index f634aedbb4c0b80150b17733cf75d631554a124f..1e8037c3748652ea1435a6bf833a2dc7b09de00b 100644 (file)
@@ -203,7 +203,7 @@ function computeForeignPodsList (host, podsScore, callback) {
       else podsScore[foreignPodHost] = 1
     })
 
-    callback()
+    return callback()
   })
 }
 
@@ -212,6 +212,7 @@ function computeWinningPods (hosts, podsScore) {
   // Only add a pod if it exists in more than a half base pods
   const podsList = []
   const baseScore = hosts.length / 2
+
   Object.keys(podsScore).forEach(function (podHost) {
     // If the pod is not me and with a good score we add it
     if (isMe(podHost) === false && podsScore[podHost] > baseScore) {
index cd52ea767b703db139d57f247d13ffe314794ded..baa26fc1b2576f7919df03291007cc7bd72a5a2d 100644 (file)
@@ -118,13 +118,8 @@ function makeRequest (toPod, requestEndpoint, requestsToMake, callback) {
   // The function fire some useful callbacks
   requests.makeSecureRequest(params, function (err, res) {
     if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
-      logger.error(
-        'Error sending secure request to %s pod.',
-        toPod.host,
-        {
-          error: err ? err.message : 'Status code not 20x : ' + res.statusCode
-        }
-      )
+      err = err ? err.message : 'Status code not 20x : ' + res.statusCode
+      logger.error('Error sending secure request to %s pod.', toPod.host, { error: err })
 
       return callback(false)
     }
@@ -153,26 +148,7 @@ function makeRequests () {
     }
 
     // We want to group requests by destinations pod and endpoint
-    const requestsToMakeGrouped = {}
-    Object.keys(requests).forEach(function (toPodId) {
-      requests[toPodId].forEach(function (data) {
-        const request = data.request
-        const pod = data.pod
-        const hashKey = toPodId + request.endpoint
-
-        if (!requestsToMakeGrouped[hashKey]) {
-          requestsToMakeGrouped[hashKey] = {
-            toPod: pod,
-            endpoint: request.endpoint,
-            ids: [], // request ids, to delete them from the DB in the future
-            datas: [] // requests data,
-          }
-        }
-
-        requestsToMakeGrouped[hashKey].ids.push(request.id)
-        requestsToMakeGrouped[hashKey].datas.push(request.request)
-      })
-    })
+    const requestsToMakeGrouped = buildRequestObjects(requests)
 
     logger.info('Making requests to friends.')
 
@@ -188,22 +164,20 @@ function makeRequests () {
         const requestIdsToDelete = requestToMake.ids
 
         logger.info('Removing %d requests of unexisting pod %s.', requestIdsToDelete.length, requestToMake.toPod.id)
-        RequestToPod.removePodOf.call(self, requestIdsToDelete, requestToMake.toPod.id)
-        return callbackEach()
+        return RequestToPod.removePodOf(requestIdsToDelete, requestToMake.toPod.id, callbackEach)
       }
 
       makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, function (success) {
-        if (success === true) {
-          logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
-
-          goodPods.push(requestToMake.toPod.id)
-
-          // Remove the pod id of these request ids
-          RequestToPod.removePodOf(requestToMake.ids, requestToMake.toPod.id, callbackEach)
-        } else {
+        if (success === false) {
           badPods.push(requestToMake.toPod.id)
-          callbackEach()
+          return callbackEach()
         }
+
+        logger.debug('Removing requests for pod %s.', requestToMake.toPod.id, { requestsIds: requestToMake.ids })
+        goodPods.push(requestToMake.toPod.id)
+
+        // Remove the pod id of these request ids
+        RequestToPod.removePodOf(requestToMake.ids, requestToMake.toPod.id, callbackEach)
       })
     }, function () {
       // All the requests were made, we update the pods score
@@ -216,6 +190,32 @@ function makeRequests () {
   })
 }
 
+function buildRequestObjects (requests) {
+  const requestsToMakeGrouped = {}
+
+  Object.keys(requests).forEach(function (toPodId) {
+    requests[toPodId].forEach(function (data) {
+      const request = data.request
+      const pod = data.pod
+      const hashKey = toPodId + request.endpoint
+
+      if (!requestsToMakeGrouped[hashKey]) {
+        requestsToMakeGrouped[hashKey] = {
+          toPod: pod,
+          endpoint: request.endpoint,
+          ids: [], // request ids, to delete them from the DB in the future
+          datas: [] // requests data,
+        }
+      }
+
+      requestsToMakeGrouped[hashKey].ids.push(request.id)
+      requestsToMakeGrouped[hashKey].datas.push(request.request)
+    })
+  })
+
+  return requestsToMakeGrouped
+}
+
 // Remove pods with a score of 0 (too many requests where they were unreachable)
 function removeBadPods () {
   const self = this