Skip to content

Commit 0977c2b

Browse files
committed
Improve readability based on pr comments
1 parent 412070c commit 0977c2b

3 files changed

Lines changed: 26 additions & 16 deletions

File tree

lib/remote_persistent_term.ex

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,15 @@ defmodule RemotePersistentTerm do
281281
start_meta,
282282
fn ->
283283
{status, version} =
284-
with {:ok, current_version, updated_fetcher_state} <- state.fetcher_mod.current_version(state.fetcher_state),
284+
with {:ok, current_version, updated_fetcher_state} <-
285+
state.fetcher_mod.current_version(state.fetcher_state),
285286
true <- state.current_version != current_version,
286-
:ok <- download_and_store_term(%{state | fetcher_state: updated_fetcher_state}, deserialize_fun, put_fun) do
287+
:ok <-
288+
download_and_store_term(
289+
%{state | fetcher_state: updated_fetcher_state},
290+
deserialize_fun,
291+
put_fun
292+
) do
287293
{:updated, current_version}
288294
else
289295
false ->
@@ -330,28 +336,32 @@ defmodule RemotePersistentTerm do
330336
{:ok, deserialized} ->
331337
put_fun.(deserialized)
332338

333-
{:error, _reason} = error when state.version_fallback? ->
339+
{:error, _reason} when state.version_fallback? ->
334340
Logger.error(
335341
"#{state.name} - failed to deserialize remote term, falling back to previous version"
336342
)
337343

338-
case state.fetcher_mod.previous_version(state.fetcher_state) do
339-
{:ok, previous_state} ->
340-
download_and_store_term(
341-
%{state | fetcher_state: previous_state},
342-
deserialize_fun,
343-
put_fun
344-
)
345-
346-
{:error, _} ->
347-
error
348-
end
344+
try_previous_version(state, deserialize_fun, put_fun)
349345

350346
error ->
351347
error
352348
end
353349
end
354350

351+
defp try_previous_version(state, deserialize_fun, put_fun) do
352+
case state.fetcher_mod.previous_version(state.fetcher_state) do
353+
{:ok, previous_state} ->
354+
download_and_store_term(
355+
%{state | fetcher_state: previous_state},
356+
deserialize_fun,
357+
put_fun
358+
)
359+
360+
{:error, _} = error ->
361+
error
362+
end
363+
end
364+
355365
defp maybe_decompress(%__MODULE__{auto_decompress?: true}, body) do
356366
case body do
357367
<<0x1F, 0x8B, _rest::binary>> = gzipped ->

lib/remote_persistent_term/fetcher/http.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ defmodule RemotePersistentTerm.Fetcher.Http do
8282
end
8383

8484
@impl true
85-
def previous_version(_state), do: {:error, :no_previous_version}
85+
def previous_version(_state), do: {:error, :not_supported}
8686

8787
defp response_status(url, status) do
8888
if status < 300 do

lib/remote_persistent_term/fetcher/static.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule RemotePersistentTerm.Fetcher.Static do
2828
def download(state), do: {:ok, unquote(Macro.escape(Keyword.fetch!(opts, :data)))}
2929

3030
@impl true
31-
def previous_version(_), do: {:error, :no_previous_version}
31+
def previous_version(_), do: {:error, :not_supported}
3232
end
3333
end
3434
end

0 commit comments

Comments
 (0)