2022-08-08 04:42:08 -04:00
|
|
|
import { Job } from 'bullmq'
|
2022-03-23 11:14:33 -04:00
|
|
|
import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from '@server/lib/activitypub/send'
|
2021-10-13 05:47:32 -04:00
|
|
|
import { ActorFollowHealthCache } from '@server/lib/actor-follow-health-cache'
|
2022-11-16 08:40:10 -05:00
|
|
|
import { parallelHTTPBroadcastFromWorker, sequentialHTTPBroadcastFromWorker } from '@server/lib/worker/parent-process'
|
2021-03-03 04:10:55 -05:00
|
|
|
import { ActivitypubHttpBroadcastPayload } from '@shared/models'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2022-09-08 06:26:46 -04:00
|
|
|
// 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 09:48:17 -04:00
|
|
|
logger.info('Processing ActivityPub broadcast in job %s.', job.id)
|
2018-01-25 09:05:18 -05:00
|
|
|
|
2022-09-08 06:26:46 -04:00
|
|
|
const requestOptions = await buildRequestOptions(job.data)
|
2017-11-17 05:35:10 -05:00
|
|
|
|
2022-09-08 06:26:46 -04:00
|
|
|
const { badUrls, goodUrls } = await sequentialHTTPBroadcastFromWorker({ uris: job.data.uris, requestOptions })
|
2017-11-17 05:35:10 -05:00
|
|
|
|
2022-09-08 06:26:46 -04: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 05:35:10 -05:00
|
|
|
|
2022-09-08 06:26:46 -04:00
|
|
|
const requestOptions = await buildRequestOptions(job.data)
|
2018-01-11 03:35:50 -05:00
|
|
|
|
2022-11-16 08:40:10 -05:00
|
|
|
const { badUrls, goodUrls } = await parallelHTTPBroadcastFromWorker({ uris: job.data.uris, requestOptions })
|
2018-01-11 03:35:50 -05:00
|
|
|
|
2021-10-13 05:47:32 -04:00
|
|
|
return ActorFollowHealthCache.Instance.updateActorFollowsHealth(goodUrls, badUrls)
|
2017-11-17 05:35:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2022-09-08 06:26:46 -04:00
|
|
|
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 05:35:10 -05:00
|
|
|
}
|