Process broadcast requests in parallel
authorChocobozzz <me@florianbigard.com>
Wed, 18 Apr 2018 14:04:49 +0000 (16:04 +0200)
committerChocobozzz <me@florianbigard.com>
Wed, 18 Apr 2018 14:04:49 +0000 (16:04 +0200)
server.ts
server/initializers/constants.ts
server/lib/job-queue/handlers/activitypub-http-broadcast.ts

index 5323bae2b423677bf3322fdf510485bfdd18e212..8024655a35dfcbfebe7fe9f468adaf996ce2f225 100644 (file)
--- a/server.ts
+++ b/server.ts
@@ -215,7 +215,8 @@ async function startApplication () {
   Redis.Instance.init()
 
   // Make server listening
-  server.listen(port, hostname)
-  logger.info('Server listening on %s:%d', hostname, port)
-  logger.info('Web server: %s', CONFIG.WEBSERVER.URL)
+  server.listen(port, hostname, () => {
+    logger.info('Server listening on %s:%d', hostname, port)
+    logger.info('Web server: %s', CONFIG.WEBSERVER.URL)
+  })
 }
index ffcbe69b86ff2211f9d3623476b67deee2b16002..5ee13389d1e182d2e464ba540ed1b4b3d2392c29 100644 (file)
@@ -77,6 +77,7 @@ const JOB_CONCURRENCY: { [ id in JobType ]: number } = {
   'video-file': 1,
   'email': 5
 }
+const BROADCAST_CONCURRENCY = 5 // How many requests in parallel we do in activitypub-http-broadcast job
 // 2 days
 const JOB_COMPLETED_LIFETIME = 60000 * 60 * 24 * 2
 
@@ -463,6 +464,7 @@ export {
   LAST_MIGRATION_VERSION,
   OAUTH_LIFETIME,
   OPENGRAPH_AND_OEMBED_COMMENT,
+  BROADCAST_CONCURRENCY,
   PAGINATION_COUNT_DEFAULT,
   ACTOR_FOLLOW_SCORE,
   PREVIEWS_SIZE,
index 78878fc01a86556e301ceb3ab92960c86103f13e..38b8393f4527ba290db69594588df50ee688c9c9 100644 (file)
@@ -1,8 +1,10 @@
 import * as kue from 'kue'
+import * as Bluebird from 'bluebird'
 import { logger } from '../../../helpers/logger'
 import { doRequest } from '../../../helpers/requests'
 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
 import { buildSignedRequestOptions, computeBody } from './utils/activitypub-http-utils'
+import { BROADCAST_CONCURRENCY } from '../../../initializers'
 
 export type ActivitypubHttpBroadcastPayload = {
   uris: string[]
@@ -28,16 +30,11 @@ async function processActivityPubHttpBroadcast (job: kue.Job) {
   const badUrls: string[] = []
   const goodUrls: string[] = []
 
-  for (const uri of payload.uris) {
-    options.uri = uri
-
-    try {
-      await doRequest(options)
-      goodUrls.push(uri)
-    } catch (err) {
-      badUrls.push(uri)
-    }
-  }
+  await Bluebird.map(payload.uris, uri => {
+    return doRequest(Object.assign({}, options, { uri }))
+      .then(() => goodUrls.push(uri))
+      .catch(() => badUrls.push(uri))
+  }, { concurrency: BROADCAST_CONCURRENCY })
 
   return ActorFollowModel.updateActorFollowsScore(goodUrls, badUrls, undefined)
 }