]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/commitdiff
Use worker thread to send HTTP requests
authorChocobozzz <me@florianbigard.com>
Thu, 8 Sep 2022 10:26:46 +0000 (12:26 +0200)
committerChocobozzz <me@florianbigard.com>
Thu, 8 Sep 2022 10:27:22 +0000 (12:27 +0200)
Compute HTTP signature could be CPU intensive

server/helpers/requests.ts
server/initializers/constants.ts
server/lib/job-queue/handlers/activitypub-http-broadcast.ts
server/lib/job-queue/job-queue.ts
server/lib/worker/parent-process.ts
server/lib/worker/workers/http-broadcast.ts [new file with mode: 0644]

index 0756beb16fb1c226f4f860a83a6dd8ff2f35fe2b..1a3cc1b5b6f38492aeae086b1e42d0d01315f7bf 100644 (file)
@@ -192,6 +192,8 @@ async function findLatestRedirection (url: string, options: PeerTubeRequestOptio
 // ---------------------------------------------------------------------------
 
 export {
+  PeerTubeRequestOptions,
+
   doRequest,
   doJSONRequest,
   doRequestAndSaveToFile,
index c2289ef360cb19b394437b8c913e1c27357f43ab..f7d9a41da52a4ff6be18bb06024ed8f31c2aab58 100644 (file)
@@ -786,6 +786,14 @@ const WORKER_THREADS = {
   PROCESS_IMAGE: {
     CONCURRENCY: 1,
     MAX_THREADS: 5
+  },
+  SEQUENTIAL_HTTP_BROADCAST: {
+    CONCURRENCY: 1,
+    MAX_THREADS: 1
+  },
+  PARALLEL_HTTP_BROADCAST: {
+    CONCURRENCY: JOB_CONCURRENCY['activitypub-http-broadcast-parallel'],
+    MAX_THREADS: 1
   }
 }
 
index 13eff52111025d288e7152d2f75230e3f533be20..733c1378a7af3a3b33041c749047e0db924595b1 100644 (file)
@@ -1,39 +1,28 @@
-import { map } from 'bluebird'
 import { Job } from 'bullmq'
 import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from '@server/lib/activitypub/send'
 import { ActorFollowHealthCache } from '@server/lib/actor-follow-health-cache'
+import { sequentialHTTPBroadcastFromWorker } from '@server/lib/worker/parent-process'
 import { ActivitypubHttpBroadcastPayload } from '@shared/models'
 import { logger } from '../../../helpers/logger'
-import { doRequest } from '../../../helpers/requests'
-import { BROADCAST_CONCURRENCY } from '../../../initializers/constants'
 
-async function processActivityPubHttpBroadcast (job: Job) {
+// Prefer using a worker thread for HTTP requests because on high load we may have to sign many requests, which can be CPU intensive
+
+async function processActivityPubHttpSequentialBroadcast (job: Job<ActivitypubHttpBroadcastPayload>) {
   logger.info('Processing ActivityPub broadcast in job %s.', job.id)
 
-  const payload = job.data as ActivitypubHttpBroadcastPayload
+  const requestOptions = await buildRequestOptions(job.data)
 
-  const body = await computeBody(payload)
-  const httpSignatureOptions = await buildSignedRequestOptions(payload)
+  const { badUrls, goodUrls } = await sequentialHTTPBroadcastFromWorker({ uris: job.data.uris, requestOptions })
 
-  const options = {
-    method: 'POST' as 'POST',
-    json: body,
-    httpSignature: httpSignatureOptions,
-    headers: buildGlobalHeaders(body)
-  }
+  return ActorFollowHealthCache.Instance.updateActorFollowsHealth(goodUrls, badUrls)
+}
+
+async function processActivityPubParallelHttpBroadcast (job: Job<ActivitypubHttpBroadcastPayload>) {
+  logger.info('Processing ActivityPub parallel broadcast in job %s.', job.id)
 
-  const badUrls: string[] = []
-  const goodUrls: string[] = []
+  const requestOptions = await buildRequestOptions(job.data)
 
-  await map(payload.uris, async uri => {
-    try {
-      await doRequest(uri, options)
-      goodUrls.push(uri)
-    } catch (err) {
-      logger.debug('HTTP broadcast to %s failed.', uri, { err })
-      badUrls.push(uri)
-    }
-  }, { concurrency: BROADCAST_CONCURRENCY })
+  const { badUrls, goodUrls } = await sequentialHTTPBroadcastFromWorker({ uris: job.data.uris, requestOptions })
 
   return ActorFollowHealthCache.Instance.updateActorFollowsHealth(goodUrls, badUrls)
 }
@@ -41,5 +30,20 @@ async function processActivityPubHttpBroadcast (job: Job) {
 // ---------------------------------------------------------------------------
 
 export {
-  processActivityPubHttpBroadcast
+  processActivityPubHttpSequentialBroadcast,
+  processActivityPubParallelHttpBroadcast
+}
+
+// ---------------------------------------------------------------------------
+
+async function buildRequestOptions (payload: ActivitypubHttpBroadcastPayload) {
+  const body = await computeBody(payload)
+  const httpSignatureOptions = await buildSignedRequestOptions(payload)
+
+  return {
+    method: 'POST' as 'POST',
+    json: body,
+    httpSignature: httpSignatureOptions,
+    headers: buildGlobalHeaders(body)
+  }
 }
index 0fcaaf4662c876a1e48d775f8941db3095bc7eca..e54d12acd2f82274fcfda7735fa4133c4610d78b 100644 (file)
@@ -45,7 +45,7 @@ import { JOB_ATTEMPTS, JOB_COMPLETED_LIFETIME, JOB_CONCURRENCY, JOB_TTL, REPEAT_
 import { Hooks } from '../plugins/hooks'
 import { processActivityPubCleaner } from './handlers/activitypub-cleaner'
 import { processActivityPubFollow } from './handlers/activitypub-follow'
-import { processActivityPubHttpBroadcast } from './handlers/activitypub-http-broadcast'
+import { processActivityPubHttpSequentialBroadcast, processActivityPubParallelHttpBroadcast } from './handlers/activitypub-http-broadcast'
 import { processActivityPubHttpFetcher } from './handlers/activitypub-http-fetcher'
 import { processActivityPubHttpUnicast } from './handlers/activitypub-http-unicast'
 import { refreshAPObject } from './handlers/activitypub-refresher'
@@ -96,8 +96,8 @@ export type CreateJobOptions = {
 }
 
 const handlers: { [id in JobType]: (job: Job) => Promise<any> } = {
-  'activitypub-http-broadcast': processActivityPubHttpBroadcast,
-  'activitypub-http-broadcast-parallel': processActivityPubHttpBroadcast,
+  'activitypub-http-broadcast': processActivityPubHttpSequentialBroadcast,
+  'activitypub-http-broadcast-parallel': processActivityPubParallelHttpBroadcast,
   'activitypub-http-unicast': processActivityPubHttpUnicast,
   'activitypub-http-fetcher': processActivityPubHttpFetcher,
   'activitypub-cleaner': processActivityPubCleaner,
index 4bc7f26207f97a44852f03bc1e2c54d78a546534..7d410204786ab5fca790b6e198af13d370cfc978 100644 (file)
@@ -2,6 +2,7 @@ import { join } from 'path'
 import Piscina from 'piscina'
 import { processImage } from '@server/helpers/image-utils'
 import { WORKER_THREADS } from '@server/initializers/constants'
+import { httpBroadcast } from './workers/http-broadcast'
 import { downloadImage } from './workers/image-downloader'
 
 let downloadImageWorker: Piscina
@@ -34,7 +35,41 @@ function processImageFromWorker (options: Parameters<typeof processImage>[0]): P
   return processImageWorker.run(options)
 }
 
+// ---------------------------------------------------------------------------
+
+let parallelHTTPBroadcastWorker: Piscina
+
+function parallelHTTPBroadcastFromWorker (options: Parameters<typeof httpBroadcast>[0]): Promise<ReturnType<typeof httpBroadcast>> {
+  if (!parallelHTTPBroadcastWorker) {
+    parallelHTTPBroadcastWorker = new Piscina({
+      filename: join(__dirname, 'workers', 'http-broadcast.js'),
+      concurrentTasksPerWorker: WORKER_THREADS.PARALLEL_HTTP_BROADCAST.CONCURRENCY,
+      maxThreads: WORKER_THREADS.PARALLEL_HTTP_BROADCAST.MAX_THREADS
+    })
+  }
+
+  return parallelHTTPBroadcastWorker.run(options)
+}
+
+// ---------------------------------------------------------------------------
+
+let sequentialHTTPBroadcastWorker: Piscina
+
+function sequentialHTTPBroadcastFromWorker (options: Parameters<typeof httpBroadcast>[0]): Promise<ReturnType<typeof httpBroadcast>> {
+  if (!sequentialHTTPBroadcastWorker) {
+    sequentialHTTPBroadcastWorker = new Piscina({
+      filename: join(__dirname, 'workers', 'http-broadcast.js'),
+      concurrentTasksPerWorker: WORKER_THREADS.SEQUENTIAL_HTTP_BROADCAST.CONCURRENCY,
+      maxThreads: WORKER_THREADS.SEQUENTIAL_HTTP_BROADCAST.MAX_THREADS
+    })
+  }
+
+  return sequentialHTTPBroadcastWorker.run(options)
+}
+
 export {
   downloadImageFromWorker,
-  processImageFromWorker
+  processImageFromWorker,
+  parallelHTTPBroadcastFromWorker,
+  sequentialHTTPBroadcastFromWorker
 }
diff --git a/server/lib/worker/workers/http-broadcast.ts b/server/lib/worker/workers/http-broadcast.ts
new file mode 100644 (file)
index 0000000..8c9c6b8
--- /dev/null
@@ -0,0 +1,32 @@
+import { map } from 'bluebird'
+import { logger } from '@server/helpers/logger'
+import { doRequest, PeerTubeRequestOptions } from '@server/helpers/requests'
+import { BROADCAST_CONCURRENCY } from '@server/initializers/constants'
+
+async function httpBroadcast (payload: {
+  uris: string[]
+  requestOptions: PeerTubeRequestOptions
+}) {
+  const { uris, requestOptions } = payload
+
+  const badUrls: string[] = []
+  const goodUrls: string[] = []
+
+  await map(uris, async uri => {
+    try {
+      await doRequest(uri, requestOptions)
+      goodUrls.push(uri)
+    } catch (err) {
+      logger.debug('HTTP broadcast to %s failed.', uri, { err })
+      badUrls.push(uri)
+    }
+  }, { concurrency: BROADCAST_CONCURRENCY })
+
+  return { goodUrls, badUrls }
+}
+
+module.exports = httpBroadcast
+
+export {
+  httpBroadcast
+}