projects
/
akkoma
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge remote-tracking branch 'pleroma/develop' into object-tombstone-visibility
[akkoma]
/
lib
/
pleroma
/
web
/
activity_pub
/
mrf
/
media_proxy_warming_policy.ex
diff --git
a/lib/pleroma/web/activity_pub/mrf/media_proxy_warming_policy.ex
b/lib/pleroma/web/activity_pub/mrf/media_proxy_warming_policy.ex
index 6c63fe15c451b3675b8240ac6245302b477612bb..25289d3a48cdb9787b16dea73d0d834e1aecadd3 100644
(file)
--- a/
lib/pleroma/web/activity_pub/mrf/media_proxy_warming_policy.ex
+++ b/
lib/pleroma/web/activity_pub/mrf/media_proxy_warming_policy.ex
@@
-1,30
+1,22
@@
# Pleroma: A lightweight social networking server
# Pleroma: A lightweight social networking server
-# Copyright © 2017-202
0
Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-202
1
Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy do
@moduledoc "Preloads any attachments in the MediaProxy cache by prefetching them"
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy do
@moduledoc "Preloads any attachments in the MediaProxy cache by prefetching them"
- @behaviour Pleroma.Web.ActivityPub.MRF
+ @behaviour Pleroma.Web.ActivityPub.MRF
.Policy
alias Pleroma.HTTP
alias Pleroma.Web.MediaProxy
alias Pleroma.HTTP
alias Pleroma.Web.MediaProxy
- alias Pleroma.Workers.BackgroundWorker
require Logger
@adapter_options [
require Logger
@adapter_options [
- pool: :media
+ pool: :media,
+ recv_timeout: 10_000
]
]
- defp adapter_options do
- if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Hackney do
- Keyword.put(@adapter_options, :recv_timeout, 10_000)
- else
- @adapter_options
- end
- end
-
- def perform(:prefetch, url) do
+ defp prefetch(url) do
# Fetching only proxiable resources
if MediaProxy.enabled?() and MediaProxy.url_proxiable?(url) do
# If preview proxy is enabled, it'll also hit media proxy (so we're caching both requests)
# Fetching only proxiable resources
if MediaProxy.enabled?() and MediaProxy.url_proxiable?(url) do
# If preview proxy is enabled, it'll also hit media proxy (so we're caching both requests)
@@
-32,17
+24,25
@@
defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy do
Logger.debug("Prefetching #{inspect(url)} as #{inspect(prefetch_url)}")
Logger.debug("Prefetching #{inspect(url)} as #{inspect(prefetch_url)}")
- HTTP.get(prefetch_url, [], adapter: adapter_options())
+ if Pleroma.Config.get(:env) == :test do
+ fetch(prefetch_url)
+ else
+ ConcurrentLimiter.limit(__MODULE__, fn ->
+ Task.start(fn -> fetch(prefetch_url) end)
+ end)
+ end
end
end
end
end
- def perform(:preload, %{"object" => %{"attachment" => attachments}} = _message) do
+ defp fetch(url), do: HTTP.get(url, [], @adapter_options)
+
+ defp preload(%{"object" => %{"attachment" => attachments}} = _message) do
Enum.each(attachments, fn
%{"url" => url} when is_list(url) ->
url
|> Enum.each(fn
%{"href" => href} ->
Enum.each(attachments, fn
%{"url" => url} when is_list(url) ->
url
|> Enum.each(fn
%{"href" => href} ->
-
BackgroundWorker.enqueue("media_proxy_prefetch", %{"url" => href}
)
+
prefetch(href
)
x ->
Logger.debug("Unhandled attachment URL object #{inspect(x)}")
x ->
Logger.debug("Unhandled attachment URL object #{inspect(x)}")
@@
-58,7
+58,7
@@
defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy do
%{"type" => "Create", "object" => %{"attachment" => attachments} = _object} = message
)
when is_list(attachments) and length(attachments) > 0 do
%{"type" => "Create", "object" => %{"attachment" => attachments} = _object} = message
)
when is_list(attachments) and length(attachments) > 0 do
-
BackgroundWorker.enqueue("media_proxy_preload", %{"message" => message}
)
+
preload(message
)
{:ok, message}
end
{:ok, message}
end