Skip to content

Commit

Permalink
Do not use persistent term
Browse files Browse the repository at this point in the history
If a repository may be shut on and off, cleaning up persistent
term can be too expensive.
  • Loading branch information
josevalim committed Nov 9, 2024
1 parent 4a99afd commit 8054c22
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions lib/ecto/repo/registry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ defmodule Ecto.Repo.Registry do
end

def lookup(repo) when is_atom(repo) do
case Process.whereis(repo) do
nil ->
raise "could not lookup Ecto repo #{inspect(repo)} because it was not started or it does not exist"

_ ->
:persistent_term.get(repo, nil)
end
GenServer.whereis(repo)
|> Kernel.||(raise "could not lookup Ecto repo #{inspect repo} because it was not started or it does not exist")
|> lookup()
end

def lookup(pid) when is_pid(pid) do
Expand All @@ -42,19 +38,13 @@ defmodule Ecto.Repo.Registry do
@impl true
def handle_call({:associate, pid, name, value}, _from, table) do
ref = Process.monitor(pid)
name && :persistent_term.put(name, value)
true = :ets.insert(table, {pid, ref, name, value})
{:reply, :ok, table}
end

@impl true
def handle_info({:DOWN, ref, _type, pid, _reason}, table) do
[{^pid, ^ref, name, _}] = :ets.lookup(table, pid)
# We don't delete from persistent term on purpose. Since the process is
# named, we can assume it does not start dynamically, so it will either
# restart or the amount of memory it uses is negligibla to justify the
# process purging done by persistent_term. If the repo is restarted and
# stores the same metadata, then no purging happens either.
[{^pid, ^ref, _, _}] = :ets.lookup(table, pid)
:ets.delete(table, pid)
{:noreply, table}
end
Expand Down

0 comments on commit 8054c22

Please sign in to comment.