Skip to content

Commit

Permalink
Fix warnings on Elixir v1.19+
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Feb 7, 2025
1 parent d740ec2 commit 3a2e78a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 59 deletions.
8 changes: 4 additions & 4 deletions lib/flame/code_sync.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ defmodule FLAME.CodeSync do
apps
end

%CodeSync{code | apps_to_start: apps_to_start}
%{code | apps_to_start: apps_to_start}
end

def compute_sync_beams(%CodeSync{} = code) do
Expand All @@ -93,7 +93,7 @@ defmodule FLAME.CodeSync do
into: %{},
do: {path, :erlang.md5(File.read!(path))}

%CodeSync{
%{
code
| sync_beam_hashes: beam_hashes,
changed_paths: Enum.uniq(code.changed_paths ++ sync_beams_files)
Expand Down Expand Up @@ -124,7 +124,7 @@ defmodule FLAME.CodeSync do
lookup_apps_files(code)
end

%CodeSync{code | changed_paths: Enum.uniq(code.changed_paths ++ changed_paths)}
%{code | changed_paths: Enum.uniq(code.changed_paths ++ changed_paths)}
end

def changed?(%CodeSync{} = code) do
Expand Down Expand Up @@ -153,7 +153,7 @@ defmodule FLAME.CodeSync do
for path <- deleted_paths,
do: path |> Path.basename(".beam") |> String.to_atom()

%CodeSync{
%{
current
| changed_paths: changed,
deleted_paths: deleted_paths,
Expand Down
21 changes: 10 additions & 11 deletions lib/flame/fly_backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ defmodule FLAME.FlyBackend do
end
end

state = %FlyBackend{state | runner_node_base: "#{state.app}-flame-#{rand_id(20)}"}
state = %{state | runner_node_base: "#{state.app}-flame-#{rand_id(20)}"}
parent_ref = make_ref()

encoded_parent =
Expand All @@ -217,8 +217,7 @@ defmodule FLAME.FlyBackend do
end
end)

new_state =
%FlyBackend{state | env: new_env, parent_ref: parent_ref, local_ip: ip}
new_state = %{state | env: new_env, parent_ref: parent_ref, local_ip: ip}

{:ok, new_state}
end
Expand Down Expand Up @@ -286,19 +285,19 @@ defmodule FLAME.FlyBackend do
)
end)

if state.log,
do:
Logger.log(
state.log,
"#{inspect(__MODULE__)} #{inspect(node())} machine create #{req_connect_time}ms"
)
if state.log do
Logger.log(
state.log,
"#{inspect(__MODULE__)} #{inspect(node())} machine create #{req_connect_time}ms"
)
end

remaining_connect_window = state.boot_timeout - req_connect_time

case resp do
%{"id" => id, "instance_id" => instance_id, "private_ip" => ip} ->
new_state =
%FlyBackend{
%{
state
| runner_id: id,
runner_instance_id: instance_id,
Expand All @@ -315,7 +314,7 @@ defmodule FLAME.FlyBackend do
exit(:timeout)
end

new_state = %FlyBackend{
new_state = %{
new_state
| remote_terminator_pid: remote_terminator_pid,
runner_node_name: node(remote_terminator_pid)
Expand Down
44 changes: 21 additions & 23 deletions lib/flame/pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ defmodule FLAME.Pool do
end

def handle_info(:async_boot_continue, %Pool{} = state) do
{:noreply, async_boot_runner(%Pool{state | async_boot_timer: nil})}
{:noreply, async_boot_runner(%{state | async_boot_timer: nil})}
end

def handle_info({:cancel, ref, caller_pid, reason}, state) do
Expand Down Expand Up @@ -567,7 +567,7 @@ defmodule FLAME.Pool do
|> Map.delete(caller_pid)
|> Map.put(child_pid, new_caller)

%Pool{state | callers: new_callers}
%{state | callers: new_callers}
end

defp checkin_runner(state, ref, caller_pid, reason)
Expand Down Expand Up @@ -630,15 +630,15 @@ defmodule FLAME.Pool do
runner_ref: runner.monitor_ref
}

new_state = %Pool{state | callers: Map.put(state.callers, from_pid, new_caller)}
new_state = %{state | callers: Map.put(state.callers, from_pid, new_caller)}

inc_runner_count(new_state, runner.monitor_ref)
end

defp waiting_in(%Pool{} = state, deadline, {pid, _tag} = from) do
ref = Process.monitor(pid)
waiting = %WaitingState{from: from, monitor_ref: ref, deadline: deadline}
%Pool{state | waiting: Queue.insert(state.waiting, waiting, pid)}
%{state | waiting: Queue.insert(state.waiting, waiting, pid)}
end

defp boot_runners(%Pool{} = state) do
Expand Down Expand Up @@ -667,11 +667,8 @@ defmodule FLAME.Pool do

defp schedule_async_boot_runner(%Pool{} = state) do
if state.async_boot_timer, do: Process.cancel_timer(state.async_boot_timer)

%Pool{
state
| async_boot_timer: Process.send_after(self(), :async_boot_continue, @async_boot_debounce)
}
timer = Process.send_after(self(), :async_boot_continue, @async_boot_debounce)
%{state | async_boot_timer: timer}
end

defp async_boot_runner(%Pool{on_grow_start: on_grow_start, name: name} = state) do
Expand All @@ -685,7 +682,7 @@ defmodule FLAME.Pool do
end)

new_pending = Map.put(state.pending_runners, task.ref, task.pid)
%Pool{state | pending_runners: new_pending}
%{state | pending_runners: new_pending}
end

defp start_child_runner(%Pool{} = state, runner_opts \\ []) do
Expand Down Expand Up @@ -713,56 +710,57 @@ defmodule FLAME.Pool do
defp put_runner(%Pool{} = state, pid) when is_pid(pid) do
ref = Process.monitor(pid)
runner = %RunnerState{count: 0, pid: pid, monitor_ref: ref}
new_state = %Pool{state | runners: Map.put(state.runners, runner.monitor_ref, runner)}
new_state = %{state | runners: Map.put(state.runners, runner.monitor_ref, runner)}
{runner, new_state}
end

defp inc_runner_count(%Pool{} = state, ref) do
new_runners =
Map.update!(state.runners, ref, fn %RunnerState{} = runner ->
%RunnerState{runner | count: runner.count + 1}
%{runner | count: runner.count + 1}
end)

%Pool{state | runners: new_runners}
%{state | runners: new_runners}
end

defp dec_runner_count(%Pool{} = state, ref) do
new_runners =
Map.update!(state.runners, ref, fn %RunnerState{} = runner ->
%RunnerState{runner | count: runner.count - 1}
%{runner | count: runner.count - 1}
end)

%Pool{state | runners: new_runners}
%{state | runners: new_runners}
end

defp drop_child_runner(%Pool{} = state, runner_ref) when is_reference(runner_ref) do
%{^runner_ref => %RunnerState{}} = state.runners
Process.demonitor(runner_ref, [:flush])

# kill all callers that still had a checkout for this runner
new_state =
Enum.reduce(state.callers, state, fn
{caller_pid, %Caller{monitor_ref: ref, runner_ref: ^runner_ref}}, acc ->
Process.demonitor(ref, [:flush])
Process.exit(caller_pid, :kill)
%Pool{acc | callers: Map.delete(acc.callers, caller_pid)}
%{acc | callers: Map.delete(acc.callers, caller_pid)}

{_caller_pid, %Caller{}}, acc ->
acc
end)

maybe_on_shrink(%Pool{new_state | runners: Map.delete(new_state.runners, runner_ref)})
maybe_on_shrink(%{new_state | runners: Map.delete(new_state.runners, runner_ref)})
end

defp drop_caller(%Pool{} = state, caller_pid, %Caller{} = caller) when is_pid(caller_pid) do
new_state = %Pool{state | callers: Map.delete(state.callers, caller_pid)}
new_state = %{state | callers: Map.delete(state.callers, caller_pid)}

new_state
|> dec_runner_count(caller.runner_ref)
|> call_next_waiting_caller()
end

defp maybe_drop_waiting(%Pool{} = state, caller_pid) when is_pid(caller_pid) do
%Pool{state | waiting: Queue.delete_by_key(state.waiting, caller_pid)}
%{state | waiting: Queue.delete_by_key(state.waiting, caller_pid)}
end

defp pop_next_waiting_caller(%Pool{} = state) do
Expand All @@ -780,8 +778,8 @@ defmodule FLAME.Pool do
end)

case result do
{nil, %Queue{} = new_waiting} -> {nil, %Pool{state | waiting: new_waiting}}
{{_pid, %WaitingState{} = first}, %Queue{} = rest} -> {first, %Pool{state | waiting: rest}}
{nil, %Queue{} = new_waiting} -> {nil, %{state | waiting: new_waiting}}
{{_pid, %WaitingState{} = first}, %Queue{} = rest} -> {first, %{state | waiting: rest}}
end
end

Expand Down Expand Up @@ -816,7 +814,7 @@ defmodule FLAME.Pool do

case state.pending_runners do
%{^ref => _} ->
state = %Pool{state | pending_runners: Map.delete(state.pending_runners, ref)}
state = %{state | pending_runners: Map.delete(state.pending_runners, ref)}
# we rate limit this to avoid many failed async boot attempts
if has_unmet_servicable_demand?(state) do
state
Expand Down Expand Up @@ -859,7 +857,7 @@ defmodule FLAME.Pool do
%{^ref => task_pid} = state.pending_runners
Process.demonitor(ref, [:flush])

new_state = %Pool{state | pending_runners: Map.delete(state.pending_runners, ref)}
new_state = %{state | pending_runners: Map.delete(state.pending_runners, ref)}
{runner, new_state} = put_runner(new_state, pid)
new_state = maybe_on_grow_end(new_state, task_pid, :ok)

Expand Down
6 changes: 3 additions & 3 deletions lib/flame/queue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule FLAME.Queue do
def insert(%Queue{idx: idx} = queue, item, key) do
new_tree = :gb_trees.insert(idx, {key, item}, queue.tree)
new_keys = Map.put(queue.keys, key, idx)
%Queue{queue | tree: new_tree, keys: new_keys, idx: idx + 1}
%{queue | tree: new_tree, keys: new_keys, idx: idx + 1}
end

@doc """
Expand All @@ -41,7 +41,7 @@ defmodule FLAME.Queue do
{_smallest_idx, {key, val}, new_tree} = :gb_trees.take_smallest(tree)
new_keys = Map.delete(keys, key)
new_idx = if :gb_trees.is_empty(new_tree), do: 0, else: idx
{{key, val}, %Queue{queue | tree: new_tree, keys: new_keys, idx: new_idx}}
{{key, val}, %{queue | tree: new_tree, keys: new_keys, idx: new_idx}}
else
{nil, queue}
end
Expand Down Expand Up @@ -103,7 +103,7 @@ defmodule FLAME.Queue do
new_tree = :gb_trees.delete_any(index, tree)
new_keys = Map.delete(keys, key)
new_idx = if :gb_trees.is_empty(new_tree), do: 0, else: queue.idx
%Queue{queue | tree: new_tree, keys: new_keys, idx: new_idx}
%{queue | tree: new_tree, keys: new_keys, idx: new_idx}

%{} ->
queue
Expand Down
8 changes: 4 additions & 4 deletions lib/flame/runner.ex
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ defmodule FLAME.Runner do
case runner.backend.remote_boot(backend_state) do
{:ok, remote_terminator_pid, new_backend_state} when is_pid(remote_terminator_pid) ->
Process.monitor(remote_terminator_pid)
new_runner = %Runner{runner | terminator: remote_terminator_pid, status: :booted}
new_runner = %{runner | terminator: remote_terminator_pid, status: :booted}
new_state = %{state | runner: new_runner, backend_state: new_backend_state}
{new_state, beams_stream} = maybe_stream_code_paths(new_state)

Expand Down Expand Up @@ -403,7 +403,7 @@ defmodule FLAME.Runner do
{backend, backend.init(Keyword.merge(base_backend_opts, backend_opts))}
end

%Runner{runner | backend: backend, backend_init: backend_init}
%{runner | backend: backend, backend_init: backend_init}
end

defp time(%Runner{log: false} = _runner, _label, func) do
Expand Down Expand Up @@ -525,7 +525,7 @@ defmodule FLAME.Runner do
|> CodeSync.compute_sync_beams()

%CodeSync.PackagedStream{} = parent_stream = CodeSync.package_to_stream(code_sync)
new_runner = %Runner{runner | code_sync: code_sync}
new_runner = %{runner | code_sync: code_sync}
{%{state | runner: new_runner}, parent_stream}
else
{state, nil}
Expand All @@ -535,7 +535,7 @@ defmodule FLAME.Runner do
defp maybe_diff_code_paths(%{runner: %Runner{} = runner} = state) do
if runner.code_sync do
diffed_code = CodeSync.diff(runner.code_sync)
new_runner = %Runner{runner | code_sync: diffed_code}
new_runner = %{runner | code_sync: diffed_code}
new_state = %{state | runner: new_runner}

if CodeSync.changed?(diffed_code) do
Expand Down
Loading

0 comments on commit 3a2e78a

Please sign in to comment.