Skip to content

Commit 412070c

Browse files
committed
Cache the versions and sort date chronologically
1 parent 55bfd44 commit 412070c

File tree

6 files changed

+17
-20
lines changed

6 files changed

+17
-20
lines changed

lib/remote_persistent_term.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ defmodule RemotePersistentTerm do
281281
start_meta,
282282
fn ->
283283
{status, version} =
284-
with {:ok, current_version} <- state.fetcher_mod.current_version(state.fetcher_state),
284+
with {:ok, current_version, updated_fetcher_state} <- state.fetcher_mod.current_version(state.fetcher_state),
285285
true <- state.current_version != current_version,
286-
:ok <- download_and_store_term(state, deserialize_fun, put_fun) do
286+
:ok <- download_and_store_term(%{state | fetcher_state: updated_fetcher_state}, deserialize_fun, put_fun) do
287287
{:updated, current_version}
288288
else
289289
false ->

lib/remote_persistent_term/fetcher.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule RemotePersistentTerm.Fetcher do
2020
Check the current version of the remote term. Used to avoid downloading the
2121
same term multiple times.
2222
"""
23-
@callback current_version(state()) :: {:ok, version()} | {:error, term()}
23+
@callback current_version(state()) :: {:ok, version(), state()} | {:error, term()}
2424

2525
@doc """
2626
Download the term from the remote source.

lib/remote_persistent_term/fetcher/http.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ defmodule RemotePersistentTerm.Fetcher.Http do
6666
end
6767

6868
@impl true
69-
def current_version(_state) do
70-
{:ok, DateTime.utc_now() |> DateTime.to_string()}
69+
def current_version(state) do
70+
{:ok, DateTime.utc_now() |> DateTime.to_string(), state}
7171
end
7272

7373
@impl true

lib/remote_persistent_term/fetcher/s3.ex

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ defmodule RemotePersistentTerm.Fetcher.S3 do
1515
key: String.t(),
1616
region: region,
1717
failover_buckets: [failover_bucket] | nil,
18-
version_id: String.t() | nil
18+
version_id: String.t() | nil,
19+
versions: [map()] | nil
1920
}
20-
defstruct [:bucket, :key, :region, :failover_buckets, :version_id]
21+
defstruct [:bucket, :key, :region, :failover_buckets, :version_id, :versions]
2122

2223
@failover_bucket_schema [
2324
bucket: [
@@ -92,7 +93,7 @@ defmodule RemotePersistentTerm.Fetcher.S3 do
9293
message: "Found latest version of object"
9394
)
9495

95-
{:ok, etag}
96+
{:ok, etag, %{state | versions: versions}}
9697
else
9798
{:error, {:unexpected_response, %{body: reason}}} ->
9899
{:error, reason}
@@ -176,9 +177,11 @@ defmodule RemotePersistentTerm.Fetcher.S3 do
176177
version_id: state.version_id
177178
)
178179

179-
with {:ok, versions} <- list_object_versions(state),
180+
versions = if state.versions, do: {:ok, state.versions}, else: list_object_versions(state)
181+
182+
with {:ok, versions} <- versions,
180183
{:ok, previous_version} <- find_previous_version(versions, state.version_id) do
181-
{:ok, %{state | version_id: previous_version.version_id}}
184+
{:ok, %{state | version_id: previous_version.version_id, versions: versions}}
182185
else
183186
{:error, reason} ->
184187
Logger.error(%{
@@ -194,13 +197,7 @@ defmodule RemotePersistentTerm.Fetcher.S3 do
194197

195198
defp find_previous_version(versions, current_version_id) do
196199
versions
197-
|> Enum.sort_by(
198-
fn version ->
199-
{:ok, datetime, _} = DateTime.from_iso8601(version.last_modified)
200-
datetime
201-
end,
202-
{:desc, DateTime}
203-
)
200+
|> Enum.sort_by(& &1.last_modified, :desc)
204201
|> Enum.find(fn version ->
205202
version.version_id != current_version_id
206203
end)

lib/remote_persistent_term/fetcher/static.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ defmodule RemotePersistentTerm.Fetcher.Static do
2222
def init(_), do: {:ok, []}
2323

2424
@impl true
25-
def current_version(_), do: {:ok, unquote(Keyword.get(opts, :version, "1"))}
25+
def current_version(state), do: {:ok, unquote(Keyword.get(opts, :version, "1")), state}
2626

2727
@impl true
28-
def download(_), do: {:ok, unquote(Macro.escape(Keyword.fetch!(opts, :data)))}
28+
def download(state), do: {:ok, unquote(Macro.escape(Keyword.fetch!(opts, :data)))}
2929

3030
@impl true
3131
def previous_version(_), do: {:error, :no_previous_version}

test/remote_persistent_term/fetcher/s3_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ defmodule RemotePersistentTerm.Fetcher.S3Test do
9999
log =
100100
capture_log(fn ->
101101
result = S3.current_version(state)
102-
assert {:ok, "current-etag"} = result
102+
assert {:ok, "current-etag", _updated_state} = result
103103
end)
104104

105105
assert log =~ "bucket: \"#{@bucket}\""

0 commit comments

Comments
 (0)