feat: first supervision tree

main
Moritz Böhme 2024-08-07 10:03:31 +02:00
parent 846d0fb16d
commit 1c197381b7
Signed by: moritz
GPG Key ID: 970C6E89EB0547A9
8 changed files with 85 additions and 74 deletions

2
.gitignore vendored
View File

@ -14,4 +14,4 @@ erl_crash.dump
/config/*.secret.exs /config/*.secret.exs
.elixir_ls/ .elixir_ls/
### Elixir Patch ### persist

View File

@ -5,8 +5,6 @@ defmodule Todo.Cache do
def init(_init_args) do def init(_init_args) do
IO.puts("Starting #{__MODULE__}") IO.puts("Starting #{__MODULE__}")
Todo.Database.start()
{:ok, %{}} {:ok, %{}}
end end
@ -17,7 +15,7 @@ defmodule Todo.Cache do
{:reply, process, state} {:reply, process, state}
:error -> :error ->
{:ok, new_process} = Todo.Server.start(name) {:ok, new_process} = Todo.Server.start_link(name)
new_state = Map.put(state, name, new_process) new_state = Map.put(state, name, new_process)

View File

@ -1,68 +1,50 @@
defmodule Todo.Database do defmodule Todo.Database do
alias Todo.DatabaseWorker use Supervisor
use GenServer
@db_folder "./persist" @db_folder "./persist"
@num_of_workers 3 @pool_size 3
def start do def start_link do
GenServer.start(__MODULE__, nil, name: __MODULE__) Supervisor.start_link(__MODULE__, nil)
end
@impl Supervisor
def init(_) do
IO.puts("Starting #{__MODULE__}.")
File.mkdir_p!(@db_folder)
children = Enum.map(1..@pool_size, &worker_spec/1)
Supervisor.init(children, strategy: :one_for_one)
end
defp worker_spec(worker_id) do
default_worker_spec = {Todo.DatabaseWorker, {@db_folder, worker_id}}
Supervisor.child_spec(default_worker_spec, id: worker_id)
end
def child_spec(_) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, []},
type: :supervisor
}
end end
def store(key, data) do def store(key, data) do
GenServer.cast(__MODULE__, {:store, key, data}) key
|> choose_worker()
|> Todo.DatabaseWorker.store(key, data)
end end
def get(key) do def get(key) do
GenServer.call(__MODULE__, {:get, key}) key
|> choose_worker()
|> Todo.DatabaseWorker.get(key)
end end
@impl GenServer defp choose_worker(key) do
def init(_) do :erlang.phash2(key, @pool_size)
IO.puts("Starting #{__MODULE__}")
File.mkdir_p!(@db_folder)
{:ok, nil, {:continue, :init}}
end
@impl GenServer
def handle_continue(:init, nil) do
File.mkdir_p!(@db_folder)
workers =
0..(@num_of_workers - 1)
|> Enum.map(fn i ->
{:ok, pid} = Todo.DatabaseWorker.start(@db_folder)
{i, pid}
end)
|> Map.new()
{:noreply, workers}
end
@impl GenServer
def handle_cast({:store, key, data}, workers) do
workers
|> choose_worker(key)
|> DatabaseWorker.store(key, data)
{:noreply, workers}
end
@impl GenServer
def handle_call({:get, key}, _, workers) do
data =
workers
|> choose_worker(key)
|> DatabaseWorker.get(key)
{:reply, data, workers}
end
defp choose_worker(workers, key) do
id = :erlang.phash2(key, @num_of_workers)
Map.fetch!(workers, id)
end end
end end

View File

@ -1,36 +1,44 @@
defmodule Todo.DatabaseWorker do defmodule Todo.DatabaseWorker do
use GenServer use GenServer
def start(db_folder) do def start_link({_db_folder, worker_id} = state) do
GenServer.start(__MODULE__, db_folder) GenServer.start_link(__MODULE__, state, name: via_tuple(worker_id))
end end
def store(pid, key, data) do def store(worker_id, key, data) do
GenServer.cast(pid, {:store, key, data}) worker_id
|> via_tuple()
|> GenServer.cast({:store, key, data})
end end
def get(pid, key) do def get(worker_id, key) do
GenServer.call(pid, {:get, key}) worker_id
|> via_tuple()
|> GenServer.call({:get, key})
end
defp via_tuple(worker_id) do
Todo.ProcessRegistry.via_tuple({__MODULE__, worker_id})
end end
@impl GenServer @impl GenServer
def init(db_folder) do def init({db_folder, worker_id} = state) do
IO.puts("Starting #{__MODULE__} with db folder #{db_folder}.") IO.puts("Starting #{__MODULE__} #{worker_id} with db folder #{db_folder}.")
{:ok, db_folder} {:ok, state}
end end
@impl GenServer @impl GenServer
def handle_cast({:store, key, data}, db_folder) do def handle_cast({:store, key, data}, {db_folder, _worker_id} = state) do
{db_folder, key} {db_folder, key}
|> file_name() |> file_name()
|> File.write!(:erlang.term_to_binary(data)) |> File.write!(:erlang.term_to_binary(data))
{:noreply, db_folder} {:noreply, state}
end end
@impl GenServer @impl GenServer
def handle_call({:get, key}, _, db_folder) do def handle_call({:get, key}, _, {db_folder, _worker_id} = state) do
data = data =
case File.read(file_name({db_folder, key})) do case File.read(file_name({db_folder, key})) do
{:ok, contents} -> {:ok, contents} ->
@ -40,7 +48,7 @@ defmodule Todo.DatabaseWorker do
nil nil
end end
{:reply, data, db_folder} {:reply, data, state}
end end
def file_name({db_folder, key}) do def file_name({db_folder, key}) do

View File

@ -0,0 +1,18 @@
defmodule Todo.ProcessRegistry do
def start_link do
IO.puts("Starting #{__MODULE__}.")
Registry.start_link(keys: :unique, name: __MODULE__)
end
def via_tuple(key) do
{:via, Registry, {__MODULE__, key}}
end
def child_spec(_) do
Supervisor.child_spec(
Registry,
id: __MODULE__,
start: {__MODULE__, :start_link, []}
)
end
end

View File

@ -1,8 +1,8 @@
defmodule Todo.Server do defmodule Todo.Server do
use GenServer use GenServer
def start(name) do def start_link(name) do
GenServer.start(__MODULE__, name) GenServer.start_link(__MODULE__, name)
end end
def add(pid, entry) do def add(pid, entry) do

View File

@ -5,9 +5,14 @@ defmodule Todo.System do
Supervisor.start_link(__MODULE__, nil) Supervisor.start_link(__MODULE__, nil)
end end
@impl Supervisor
def init(_) do def init(_) do
Supervisor.init( Supervisor.init(
[Todo.Cache], [
Todo.ProcessRegistry,
Todo.Database,
Todo.Cache
],
strategy: :one_for_one strategy: :one_for_one
) )
end end

BIN
persist/a

Binary file not shown.