logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

supervisor.ex (1602B)


  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. defmodule Pleroma.Web.FedSockets.Supervisor do
  5. use Supervisor
  6. import Cachex.Spec
  7. def start_link(opts) do
  8. Supervisor.start_link(__MODULE__, opts, name: __MODULE__)
  9. end
  10. def init(args) do
  11. children = [
  12. build_cache(:fed_socket_fetches, args),
  13. build_cache(:fed_socket_rejections, args),
  14. {Registry, keys: :unique, name: FedSockets.Registry, meta: [rejected: %{}]}
  15. ]
  16. opts = [strategy: :one_for_all, name: Pleroma.Web.Streamer.Supervisor]
  17. Supervisor.init(children, opts)
  18. end
  19. defp build_cache(name, args) do
  20. opts = get_opts(name, args)
  21. %{
  22. id: String.to_atom("#{name}_cache"),
  23. start: {Cachex, :start_link, [name, opts]},
  24. type: :worker
  25. }
  26. end
  27. defp get_opts(cache_name, args)
  28. when cache_name in [:fed_socket_fetches, :fed_socket_rejections] do
  29. default = get_opts_or_config(args, cache_name, :default, 15_000)
  30. interval = get_opts_or_config(args, cache_name, :interval, 3_000)
  31. lazy = get_opts_or_config(args, cache_name, :lazy, false)
  32. [expiration: expiration(default: default, interval: interval, lazy: lazy)]
  33. end
  34. defp get_opts(name, args) do
  35. Keyword.get(args, name, [])
  36. end
  37. defp get_opts_or_config(args, name, key, default) do
  38. args
  39. |> Keyword.get(name, [])
  40. |> Keyword.get(key)
  41. |> case do
  42. nil ->
  43. Pleroma.Config.get([:fed_sockets, name, key], default)
  44. value ->
  45. value
  46. end
  47. end
  48. end