From 4ead40e7766f5964ecd9a11766ff56b95090fe1c Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 9 Jun 2021 13:34:40 +0200 Subject: Cache refresh actor promise --- server/lib/activitypub/actors/get.ts | 2 +- server/lib/activitypub/actors/refresh.ts | 34 +++++++++++++++------- .../job-queue/handlers/activitypub-refresher.ts | 2 +- 3 files changed, 26 insertions(+), 12 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/actors/get.ts b/server/lib/activitypub/actors/get.ts index de93aa964..501ff74e3 100644 --- a/server/lib/activitypub/actors/get.ts +++ b/server/lib/activitypub/actors/get.ts @@ -56,7 +56,7 @@ async function getOrCreateAPActor ( if (actor.Account) (actor as MActorAccountChannelIdActor).Account.Actor = actor if (actor.VideoChannel) (actor as MActorAccountChannelIdActor).VideoChannel.Actor = actor - const { actor: actorRefreshed, refreshed } = await refreshActorIfNeeded(actor, fetchType) + const { actor: actorRefreshed, refreshed } = await refreshActorIfNeeded({ actor, fetchedType: fetchType }) if (!actorRefreshed) throw new Error('Actor ' + actor.url + ' does not exist anymore.') await scheduleOutboxFetchIfNeeded(actor, created, refreshed, updateCollections) diff --git a/server/lib/activitypub/actors/refresh.ts b/server/lib/activitypub/actors/refresh.ts index 1f7cfcd8f..b2fe3932f 100644 --- a/server/lib/activitypub/actors/refresh.ts +++ b/server/lib/activitypub/actors/refresh.ts @@ -1,4 +1,5 @@ import { logger, loggerTagsFactory } from '@server/helpers/logger' +import { PromiseCache } from '@server/helpers/promise-cache' import { PeerTubeRequestError } from '@server/helpers/requests' import { ActorLoadByUrlType } from '@server/lib/model-loaders' import { ActorModel } from '@server/models/actor/actor' @@ -8,11 +9,30 @@ import { fetchRemoteActor } from './shared' import { APActorUpdater } from './updater' import { getUrlFromWebfinger } from './webfinger' -async function refreshActorIfNeeded ( - actorArg: T, +type RefreshResult = Promise<{ actor: T | MActorFull, refreshed: boolean }> + +type RefreshOptions = { + actor: T fetchedType: ActorLoadByUrlType -): Promise<{ actor: T | MActorFull, refreshed: boolean }> { - if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false } +} + +const promiseCache = new PromiseCache(doRefresh, (options: RefreshOptions) => options.actor.url) + +function refreshActorIfNeeded (options: RefreshOptions): RefreshResult { + const actorArg = options.actor + if (!actorArg.isOutdated()) return Promise.resolve({ actor: actorArg, refreshed: false }) + + return promiseCache.run(options) +} + +export { + refreshActorIfNeeded +} + +// --------------------------------------------------------------------------- + +async function doRefresh (options: RefreshOptions): RefreshResult { + const { actor: actorArg, fetchedType } = options // We need more attributes const actor = fetchedType === 'all' @@ -52,12 +72,6 @@ async function refreshActorIfNeeded { diff --git a/server/lib/job-queue/handlers/activitypub-refresher.ts b/server/lib/job-queue/handlers/activitypub-refresher.ts index f1c7d01b6..d97e50ebc 100644 --- a/server/lib/job-queue/handlers/activitypub-refresher.ts +++ b/server/lib/job-queue/handlers/activitypub-refresher.ts @@ -47,7 +47,7 @@ async function refreshActor (actorUrl: string) { const actor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorUrl) if (actor) { - await refreshActorIfNeeded(actor, fetchType) + await refreshActorIfNeeded({ actor, fetchedType: fetchType }) } } -- cgit v1.2.3