Merge branch 'issue/2069' into 'develop'
[akkoma] / lib / pleroma / web / fed_sockets / supervisor.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.FedSockets.Supervisor do
6 use Supervisor
7 import Cachex.Spec
8
9 def start_link(opts) do
10 Supervisor.start_link(__MODULE__, opts, name: __MODULE__)
11 end
12
13 def init(args) do
14 children = [
15 build_cache(:fed_socket_fetches, args),
16 build_cache(:fed_socket_rejections, args),
17 {Registry, keys: :unique, name: FedSockets.Registry, meta: [rejected: %{}]}
18 ]
19
20 opts = [strategy: :one_for_all, name: Pleroma.Web.Streamer.Supervisor]
21 Supervisor.init(children, opts)
22 end
23
24 defp build_cache(name, args) do
25 opts = get_opts(name, args)
26
27 %{
28 id: String.to_atom("#{name}_cache"),
29 start: {Cachex, :start_link, [name, opts]},
30 type: :worker
31 }
32 end
33
34 defp get_opts(cache_name, args)
35 when cache_name in [:fed_socket_fetches, :fed_socket_rejections] do
36 default = get_opts_or_config(args, cache_name, :default, 15_000)
37 interval = get_opts_or_config(args, cache_name, :interval, 3_000)
38 lazy = get_opts_or_config(args, cache_name, :lazy, false)
39
40 [expiration: expiration(default: default, interval: interval, lazy: lazy)]
41 end
42
43 defp get_opts(name, args) do
44 Keyword.get(args, name, [])
45 end
46
47 defp get_opts_or_config(args, name, key, default) do
48 args
49 |> Keyword.get(name, [])
50 |> Keyword.get(key)
51 |> case do
52 nil ->
53 Pleroma.Config.get([:fed_sockets, name, key], default)
54
55 value ->
56 value
57 end
58 end
59 end