Server: requests refractoring
This commit is contained in:
parent
da691c46df
commit
1e4b0080ff
|
@ -203,7 +203,7 @@ function computeForeignPodsList (host, podsScore, callback) {
|
||||||
else podsScore[foreignPodHost] = 1
|
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
|
// Only add a pod if it exists in more than a half base pods
|
||||||
const podsList = []
|
const podsList = []
|
||||||
const baseScore = hosts.length / 2
|
const baseScore = hosts.length / 2
|
||||||
|
|
||||||
Object.keys(podsScore).forEach(function (podHost) {
|
Object.keys(podsScore).forEach(function (podHost) {
|
||||||
// If the pod is not me and with a good score we add it
|
// If the pod is not me and with a good score we add it
|
||||||
if (isMe(podHost) === false && podsScore[podHost] > baseScore) {
|
if (isMe(podHost) === false && podsScore[podHost] > baseScore) {
|
||||||
|
|
|
@ -118,13 +118,8 @@ function makeRequest (toPod, requestEndpoint, requestsToMake, callback) {
|
||||||
// The function fire some useful callbacks
|
// The function fire some useful callbacks
|
||||||
requests.makeSecureRequest(params, function (err, res) {
|
requests.makeSecureRequest(params, function (err, res) {
|
||||||
if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
|
if (err || (res.statusCode !== 200 && res.statusCode !== 201 && res.statusCode !== 204)) {
|
||||||
logger.error(
|
err = err ? err.message : 'Status code not 20x : ' + res.statusCode
|
||||||
'Error sending secure request to %s pod.',
|
logger.error('Error sending secure request to %s pod.', toPod.host, { error: err })
|
||||||
toPod.host,
|
|
||||||
{
|
|
||||||
error: err ? err.message : 'Status code not 20x : ' + res.statusCode
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return callback(false)
|
return callback(false)
|
||||||
}
|
}
|
||||||
|
@ -153,7 +148,51 @@ function makeRequests () {
|
||||||
}
|
}
|
||||||
|
|
||||||
// We want to group requests by destinations pod and endpoint
|
// We want to group requests by destinations pod and endpoint
|
||||||
|
const requestsToMakeGrouped = buildRequestObjects(requests)
|
||||||
|
|
||||||
|
logger.info('Making requests to friends.')
|
||||||
|
|
||||||
|
const goodPods = []
|
||||||
|
const badPods = []
|
||||||
|
|
||||||
|
eachLimit(Object.keys(requestsToMakeGrouped), constants.REQUESTS_IN_PARALLEL, function (hashKey, callbackEach) {
|
||||||
|
const requestToMake = requestsToMakeGrouped[hashKey]
|
||||||
|
const toPod = requestToMake.toPod
|
||||||
|
|
||||||
|
// Maybe the pod is not our friend anymore so simply remove it
|
||||||
|
if (!toPod) {
|
||||||
|
const requestIdsToDelete = requestToMake.ids
|
||||||
|
|
||||||
|
logger.info('Removing %d requests of unexisting pod %s.', requestIdsToDelete.length, requestToMake.toPod.id)
|
||||||
|
return RequestToPod.removePodOf(requestIdsToDelete, requestToMake.toPod.id, callbackEach)
|
||||||
|
}
|
||||||
|
|
||||||
|
makeRequest(toPod, requestToMake.endpoint, requestToMake.datas, function (success) {
|
||||||
|
if (success === false) {
|
||||||
|
badPods.push(requestToMake.toPod.id)
|
||||||
|
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
|
||||||
|
updatePodsScore.call(self, goodPods, badPods)
|
||||||
|
// Flush requests with no pod
|
||||||
|
removeWithEmptyTo.call(self, function (err) {
|
||||||
|
if (err) logger.error('Error when removing requests with no pods.', { error: err })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildRequestObjects (requests) {
|
||||||
const requestsToMakeGrouped = {}
|
const requestsToMakeGrouped = {}
|
||||||
|
|
||||||
Object.keys(requests).forEach(function (toPodId) {
|
Object.keys(requests).forEach(function (toPodId) {
|
||||||
requests[toPodId].forEach(function (data) {
|
requests[toPodId].forEach(function (data) {
|
||||||
const request = data.request
|
const request = data.request
|
||||||
|
@ -174,46 +213,7 @@ function makeRequests () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
logger.info('Making requests to friends.')
|
return requestsToMakeGrouped
|
||||||
|
|
||||||
const goodPods = []
|
|
||||||
const badPods = []
|
|
||||||
|
|
||||||
eachLimit(Object.keys(requestsToMakeGrouped), constants.REQUESTS_IN_PARALLEL, function (hashKey, callbackEach) {
|
|
||||||
const requestToMake = requestsToMakeGrouped[hashKey]
|
|
||||||
const toPod = requestToMake.toPod
|
|
||||||
|
|
||||||
// Maybe the pod is not our friend anymore so simply remove it
|
|
||||||
if (!toPod) {
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
|
|
||||||
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 {
|
|
||||||
badPods.push(requestToMake.toPod.id)
|
|
||||||
callbackEach()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}, function () {
|
|
||||||
// All the requests were made, we update the pods score
|
|
||||||
updatePodsScore.call(self, goodPods, badPods)
|
|
||||||
// Flush requests with no pod
|
|
||||||
removeWithEmptyTo.call(self, function (err) {
|
|
||||||
if (err) logger.error('Error when removing requests with no pods.', { error: err })
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove pods with a score of 0 (too many requests where they were unreachable)
|
// Remove pods with a score of 0 (too many requests where they were unreachable)
|
||||||
|
|
Loading…
Reference in New Issue