Skip to content

Commit 89c6b51

Browse files
authored
Refactor CustomData to use ETS table directly on get (#86)
Before this change the CustomData process could become a bottleneck when fetching custom data to store as part of ExAudit change tracking Having a named ETS table makes it easy to directly fetch data without going through the table owner
1 parent abb125e commit 89c6b51

File tree

1 file changed

+4
-18
lines changed

1 file changed

+4
-18
lines changed

lib/tracking/custom_data.ex

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule ExAudit.CustomData do
1010
end
1111

1212
def init(nil) do
13-
ets = :ets.new(:track_by_pid, [:public])
13+
ets = :ets.new(__MODULE__, [:protected, :named_table])
1414
{:ok, ets}
1515
end
1616

@@ -24,23 +24,9 @@ defmodule ExAudit.CustomData do
2424
{:reply, :ok, ets}
2525
end
2626

27-
def handle_call({:get, pid}, _, ets) do
28-
case :ets.lookup(ets, pid) do
29-
[] ->
30-
{:reply, [], ets}
31-
32-
list ->
33-
values = Enum.flat_map(list, &elem(&1, 1))
34-
{:reply, values, ets}
35-
end
36-
end
37-
38-
def get() do
39-
GenServer.call(__MODULE__, {:get, self()})
40-
end
41-
42-
def get(pid) do
43-
GenServer.call(__MODULE__, {:get, pid})
27+
def get(pid \\ self()) do
28+
:ets.lookup(__MODULE__, pid)
29+
|> Enum.flat_map(&elem(&1, 1))
4430
end
4531

4632
def handle_info({:DOWN, _, :process, pid, _}, ets) do

0 commit comments

Comments
 (0)