aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/friends.js
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-02-18 10:29:36 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-02-18 10:29:36 +0100
commitc1a7ab7f04fdb1601cf1e41c4e372dbd3c81f3de (patch)
tree5d721432dcc25f43ebd72b12f675d4e208faf616 /server/lib/friends.js
parent361b7df2a257d1c44ec1d79128a9065b563090d8 (diff)
downloadPeerTube-c1a7ab7f04fdb1601cf1e41c4e372dbd3c81f3de.tar.gz
PeerTube-c1a7ab7f04fdb1601cf1e41c4e372dbd3c81f3de.tar.zst
PeerTube-c1a7ab7f04fdb1601cf1e41c4e372dbd3c81f3de.zip
Server: use a request scheduler object instance for friends
communication
Diffstat (limited to 'server/lib/friends.js')
-rw-r--r--server/lib/friends.js59
1 files changed, 15 insertions, 44 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js
index 9b38693c7..7dfa62a2a 100644
--- a/server/lib/friends.js
+++ b/server/lib/friends.js
@@ -11,10 +11,13 @@ const db = require('../initializers/database')
11const logger = require('../helpers/logger') 11const logger = require('../helpers/logger')
12const peertubeCrypto = require('../helpers/peertube-crypto') 12const peertubeCrypto = require('../helpers/peertube-crypto')
13const requests = require('../helpers/requests') 13const requests = require('../helpers/requests')
14const RequestScheduler = require('./request-scheduler')
14 15
15const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS] 16const ENDPOINT_ACTIONS = constants.REQUEST_ENDPOINT_ACTIONS[constants.REQUEST_ENDPOINTS.VIDEOS]
17const requestScheduler = new RequestScheduler('')
16 18
17const friends = { 19const friends = {
20 activate,
18 addVideoToFriends, 21 addVideoToFriends,
19 updateVideoToFriends, 22 updateVideoToFriends,
20 reportAbuseVideoToFriend, 23 reportAbuseVideoToFriend,
@@ -25,6 +28,10 @@ const friends = {
25 sendOwnedVideosToPod 28 sendOwnedVideosToPod
26} 29}
27 30
31function activate () {
32 requestScheduler.activate()
33}
34
28function addVideoToFriends (videoData, transaction, callback) { 35function addVideoToFriends (videoData, transaction, callback) {
29 const options = { 36 const options = {
30 type: ENDPOINT_ACTIONS.ADD, 37 type: ENDPOINT_ACTIONS.ADD,
@@ -99,11 +106,11 @@ function makeFriends (hosts, callback) {
99 106
100function quitFriends (callback) { 107function quitFriends (callback) {
101 // Stop pool requests 108 // Stop pool requests
102 db.Request.deactivate() 109 requestScheduler.deactivate()
103 110
104 waterfall([ 111 waterfall([
105 function flushRequests (callbackAsync) { 112 function flushRequests (callbackAsync) {
106 db.Request.flush(callbackAsync) 113 requestScheduler.flush(callbackAsync)
107 }, 114 },
108 115
109 function getPodsList (callbackAsync) { 116 function getPodsList (callbackAsync) {
@@ -140,7 +147,7 @@ function quitFriends (callback) {
140 } 147 }
141 ], function (err) { 148 ], function (err) {
142 // Don't forget to re activate the scheduler, even if there was an error 149 // Don't forget to re activate the scheduler, even if there was an error
143 db.Request.activate() 150 requestScheduler.activate()
144 151
145 if (err) return callback(err) 152 if (err) return callback(err)
146 153
@@ -235,9 +242,9 @@ function getForeignPodsList (host, callback) {
235 242
236function makeRequestsToWinningPods (cert, podsList, callback) { 243function makeRequestsToWinningPods (cert, podsList, callback) {
237 // Stop pool requests 244 // Stop pool requests
238 db.Request.deactivate() 245 requestScheduler.deactivate()
239 // Flush pool requests 246 // Flush pool requests
240 db.Request.forceSend() 247 requestScheduler.forceSend()
241 248
242 eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) { 249 eachLimit(podsList, constants.REQUESTS_IN_PARALLEL, function (pod, callbackEach) {
243 const params = { 250 const params = {
@@ -278,7 +285,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
278 }, function endRequests () { 285 }, function endRequests () {
279 // Final callback, we've ended all the requests 286 // Final callback, we've ended all the requests
280 // Now we made new friends, we can re activate the pool of requests 287 // Now we made new friends, we can re activate the pool of requests
281 db.Request.activate() 288 requestScheduler.activate()
282 289
283 logger.debug('makeRequestsToWinningPods finished.') 290 logger.debug('makeRequestsToWinningPods finished.')
284 return callback() 291 return callback()
@@ -289,7 +296,7 @@ function makeRequestsToWinningPods (cert, podsList, callback) {
289// { type, endpoint, data, toIds, transaction } 296// { type, endpoint, data, toIds, transaction }
290function createRequest (options, callback) { 297function createRequest (options, callback) {
291 if (!callback) callback = function () {} 298 if (!callback) callback = function () {}
292 if (options.toIds) return _createRequest(options, callback) 299 if (options.toIds) return requestScheduler.createRequest(options, callback)
293 300
294 // If the "toIds" pods is not specified, we send the request to all our friends 301 // If the "toIds" pods is not specified, we send the request to all our friends
295 db.Pod.listAllIds(options.transaction, function (err, podIds) { 302 db.Pod.listAllIds(options.transaction, function (err, podIds) {
@@ -299,43 +306,7 @@ function createRequest (options, callback) {
299 } 306 }
300 307
301 const newOptions = Object.assign(options, { toIds: podIds }) 308 const newOptions = Object.assign(options, { toIds: podIds })
302 return _createRequest(newOptions, callback) 309 return requestScheduler.createRequest(newOptions, callback)
303 })
304}
305
306// { type, endpoint, data, toIds, transaction }
307function _createRequest (options, callback) {
308 const type = options.type
309 const endpoint = options.endpoint
310 const data = options.data
311 const toIds = options.toIds
312 const transaction = options.transaction
313
314 const pods = []
315
316 // If there are no destination pods abort
317 if (toIds.length === 0) return callback(null)
318
319 toIds.forEach(function (toPod) {
320 pods.push(db.Pod.build({ id: toPod }))
321 })
322
323 const createQuery = {
324 endpoint,
325 request: {
326 type: type,
327 data: data
328 }
329 }
330
331 const dbRequestOptions = {
332 transaction
333 }
334
335 return db.Request.create(createQuery, dbRequestOptions).asCallback(function (err, request) {
336 if (err) return callback(err)
337
338 return request.setPods(pods, dbRequestOptions).asCallback(callback)
339 }) 310 })
340} 311}
341 312