PeerTube/server/lib/job-queue/handlers/activitypub-http-broadcast.ts

50 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-08-08 03:42:08 -05:00
import { Job } from 'bullmq'
2022-03-23 10:14:33 -05:00
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'
2021-03-03 03:10:55 -06:00
import { ActivitypubHttpBroadcastPayload } from '@shared/models'
2017-12-28 04:16:08 -06:00
import { logger } from '../../../helpers/logger'
// 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>) {
2022-08-08 08:48:17 -05:00
logger.info('Processing ActivityPub broadcast in job %s.', job.id)
const requestOptions = await buildRequestOptions(job.data)
2017-11-17 04:35:10 -06:00
const { badUrls, goodUrls } = await sequentialHTTPBroadcastFromWorker({ uris: job.data.uris, requestOptions })
2017-11-17 04:35:10 -06:00
return ActorFollowHealthCache.Instance.updateActorFollowsHealth(goodUrls, badUrls)
}
async function processActivityPubParallelHttpBroadcast (job: Job<ActivitypubHttpBroadcastPayload>) {
logger.info('Processing ActivityPub parallel broadcast in job %s.', job.id)
2017-11-17 04:35:10 -06:00
const requestOptions = await buildRequestOptions(job.data)
const { badUrls, goodUrls } = await sequentialHTTPBroadcastFromWorker({ uris: job.data.uris, requestOptions })
return ActorFollowHealthCache.Instance.updateActorFollowsHealth(goodUrls, badUrls)
2017-11-17 04:35:10 -06:00
}
// ---------------------------------------------------------------------------
export {
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)
}
2017-11-17 04:35:10 -06:00
}