Add a cache for publishing to reduce db load

This commit is contained in:
Mark Felder 2024-08-04 21:27:54 -04:00
parent b389b85d77
commit 99dbde56f4
3 changed files with 18 additions and 3 deletions

View file

@ -168,7 +168,8 @@ defmodule Pleroma.Application do
limit: 500_000
),
build_cachex("rel_me", limit: 2500),
build_cachex("host_meta", default_ttl: :timer.minutes(120), limit: 5000)
build_cachex("host_meta", default_ttl: :timer.minutes(120), limit: 5000),
build_cachex("publisher", default_ttl: :timer.minutes(30), limit: 2500)
]
end

View file

@ -21,6 +21,8 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
require Logger
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
@moduledoc """
ActivityPub outgoing federation module.
"""
@ -84,7 +86,15 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
* `cc`: the cc recipients relevant to this inbox (optional)
"""
def publish_one(%{inbox: inbox, activity_id: activity_id} = params) do
activity = Activity.get_by_id_with_user_actor(activity_id)
activity =
with {:ok, nil} <- @cachex.get(:publisher_cache, activity_id),
%Activity{} = activity <- Activity.get_by_id_with_user_actor(activity_id),
{:ok, _} <- @cachex.put(:publisher_cache, activity_id, activity) do
activity
else
{:ok, %Activity{} = activity} -> activity
end
actor = activity.user_actor
ap_id = activity.data["id"]

View file

@ -8,13 +8,17 @@ defmodule Pleroma.Workers.PublisherWorker do
use Pleroma.Workers.WorkerHelper, queue: "federator_outgoing"
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
def backoff(%Job{attempt: attempt}) when is_integer(attempt) do
Pleroma.Workers.WorkerHelper.sidekiq_backoff(attempt, 5)
end
@impl Oban.Worker
def perform(%Job{args: %{"op" => "publish", "activity_id" => activity_id}}) do
activity = Activity.get_by_id(activity_id)
activity = Activity.get_by_id_with_user_actor(activity_id)
{:ok, true} = @cachex.put(:publisher_cache, activity_id, activity)
Federator.perform(:publish, activity)
end