Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions apps/unlock/lib/aggregate_processor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ defmodule Unlock.AggregateProcessor do
:include_origin
])

comp_fn = fn _key ->
{:commit, fetch_feeds(item, options), ttl: :timer.seconds(item.ttl)}
end

cache_name = Unlock.Shared.cache_name()
cache_key = item.identifier <> ":#{:erlang.phash2(options)}"

case Cachex.fetch(cache_name, cache_key, comp_fn) do
{:ok, result} ->
Logger.info("Proxy response for #{item.identifier} served from cache")
result

{:commit, result, _options} ->
result

{:error, _error} ->
Logger.error("Error while fetching key #{cache_key}")
%Unlock.HTTP.Response{status: 502, body: "", headers: []}
end
end

def fetch_feeds(%Unlock.Config.Item.Aggregate{} = item, options) do
headers = @schema_fields
headers = if options[:include_origin], do: headers ++ ["origin", "slug"], else: headers

Expand Down
6 changes: 3 additions & 3 deletions apps/unlock/test/controllers/unlock_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ defmodule Unlock.ControllerTest do
def setup_aggregate_proxy_config(slug) do
setup_proxy_config(%{
slug => %Unlock.Config.Item.Aggregate{
ttl: 30,
identifier: slug,
feeds: [
%Unlock.Config.Item.Generic.HTTP{
Expand Down Expand Up @@ -589,7 +590,7 @@ defmodule Unlock.ControllerTest do

verify!(Unlock.HTTP.Client.Mock)

# more calls should not result in any real query
# more calls should not result in any HTTP calls to sub-feeds
{resp, logs} =
with_log(fn ->
build_conn()
Expand All @@ -599,8 +600,7 @@ defmodule Unlock.ControllerTest do
assert resp.status == 200
assert resp.resp_body == Helper.data_as_csv(@expected_headers, [first_data_row, second_data_row], "\r\n")

assert logs =~ ~r|Proxy response for an-existing-aggregate-identifier:first-uuid served from cache|
assert logs =~ ~r|Proxy response for an-existing-aggregate-identifier:second-uuid served from cache|
assert logs =~ ~r|Proxy response for an-existing-aggregate-identifier served from cache|

assert_received {:telemetry_event, [:proxy, :request, :external], %{},
%{target: "proxy:an-existing-aggregate-identifier"}}
Expand Down