Skip to content

Commit e5a7c29

Browse files
authored
perf: increase expiration time for auth context (#2268)
* perf: increase expiration time for auth context * perf: increase auth cache ttl, add oauth_access_tokens to publication_tables * chore: fix invalid expiration error * chore: use alter publication * chore: correct alter publication syntax
1 parent ca28b5d commit e5a7c29

File tree

6 files changed

+59
-1
lines changed

6 files changed

+59
-1
lines changed

lib/logflare/auth/cache.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ defmodule Logflare.Auth.Cache do
2525
Utils.cache_limit(100_000)
2626
]
2727
|> Enum.filter(& &1),
28-
expiration: Utils.cache_expiration_sec(30, 15)
28+
expiration: Utils.cache_expiration_min(5, 2)
2929
]
3030
]}
3131
}

lib/logflare/context_cache.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ defmodule Logflare.ContextCache do
118118
end
119119

120120
defp select_key(%_{id: id}), do: id
121+
defp select_key({:ok, %_{id: id}}), do: id
121122
defp select_key(true), do: "true"
122123
defp select_key(nil), do: :not_found
123124
defp select_key(_), do: :unknown

lib/logflare/context_cache/cache_buster.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@ defmodule Logflare.ContextCache.CacheBuster do
116116
{Logflare.TeamUsers, String.to_integer(id)}
117117
end
118118

119+
defp handle_record(%UpdatedRecord{
120+
relation: {_schema, "oauth_access_tokens"},
121+
record: %{"id" => id}
122+
})
123+
when is_binary(id) do
124+
{Logflare.Auth, String.to_integer(id)}
125+
end
126+
119127
defp handle_record(%NewRecord{
120128
relation: {_schema, "billing_accounts"},
121129
record: %{"id" => _id}
@@ -175,6 +183,14 @@ defmodule Logflare.ContextCache.CacheBuster do
175183
{Logflare.TeamUsers, :not_found}
176184
end
177185

186+
defp handle_record(%NewRecord{
187+
relation: {_schema, "oauth_access_tokens"},
188+
record: %{"id" => _id}
189+
}) do
190+
# When new records are created they were previously cached as `nil` so we need to bust the :not_found keys
191+
{Logflare.Auth, :not_found}
192+
end
193+
178194
defp handle_record(%DeletedRecord{
179195
relation: {_schema, "billing_accounts"},
180196
old_record: %{"id" => id}
@@ -233,6 +249,15 @@ defmodule Logflare.ContextCache.CacheBuster do
233249
{Logflare.TeamUsers, String.to_integer(id)}
234250
end
235251

252+
defp handle_record(%DeletedRecord{
253+
relation: {_schema, "oauth_access_tokens"},
254+
old_record: %{"id" => id}
255+
})
256+
when is_binary(id) do
257+
# Must do `alter table rules replica identity full` to get full records on deletes otherwise all fields are null
258+
{Logflare.Auth, String.to_integer(id)}
259+
end
260+
236261
defp handle_record(_record) do
237262
:noop
238263
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
defmodule Logflare.Repo.Migrations.RecreateTablesPublication do
2+
use Ecto.Migration
3+
use Ecto.Migration
4+
5+
@publications Application.get_env(:logflare, Logflare.ContextCache.CacheBuster)[:publications]
6+
@table "oauth_access_tokens"
7+
8+
def up do
9+
for p <- @publications do
10+
execute("ALTER PUBLICATION #{p} ADD TABLE #{@table};")
11+
end
12+
end
13+
14+
def down do
15+
for p <- @publications do
16+
execute("ALTER PUBLICATION #{p} DROP TABLE #{@table};")
17+
end
18+
end
19+
end

test/logflare/auth/cache_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Logflare.Auth.CacheTest do
33
alias Logflare.Auth
44
alias Logflare.Factory
55
alias Logflare.Partners.Partner
6+
alias Logflare.ContextCache
67

78
setup do
89
user = Factory.insert(:user)
@@ -47,6 +48,17 @@ defmodule Logflare.Auth.CacheTest do
4748
assert {:error, :unauthorized} = Auth.Cache.verify_access_token(key)
4849
end
4950

51+
test "cache busting", %{user: user} do
52+
{:ok, key} = Auth.create_access_token(user)
53+
54+
Auth
55+
|> expect(:verify_access_token, 2, fn _ -> {:ok, key} end)
56+
57+
assert {:ok, _} = Auth.Cache.verify_access_token(key.token)
58+
ContextCache.bust_keys([{Auth, key.id}])
59+
assert {:ok, _} = Auth.Cache.verify_access_token(key.token)
60+
end
61+
5062
defp access_token_fixture(user_or_team_or_partner) do
5163
{:ok, key} = Auth.create_access_token(user_or_team_or_partner)
5264
key

test/test_helper.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Mimic.copy(Logflare.Source.RateCounterServer)
1717
Mimic.copy(Logflare.Source.BigQuery.Schema)
1818
Mimic.copy(Logflare.SystemMetrics.AllLogsLogged)
1919
Mimic.copy(Logflare.Sources.Cache)
20+
Mimic.copy(Logflare.Auth)
2021
Mimic.copy(Logflare.SingleTenant)
2122
Mimic.copy(Logflare.Backends.Adaptor.WebhookAdaptor)
2223
Mimic.copy(Logflare.Backends.Adaptor.WebhookAdaptor.Client)

0 commit comments

Comments
 (0)