Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cache for user query, server_name_indication, change log level #179

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.10
0.9.11
2 changes: 1 addition & 1 deletion lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ defmodule Supavisor.ClientHandler do
def handle_event(:internal, {:hello, {user, external_id}}, :exchange, %{sock: sock} = data) do
sni_hostname = try_get_sni(sock)

case Tenants.get_user(user, external_id, sni_hostname) do
case Tenants.get_user_cache(user, external_id, sni_hostname) do
{:ok, info} ->
id = Supavisor.id(info.tenant.external_id, user, data.mode, info.user.mode_type)
Registry.register(Supavisor.Registry.TenantClients, id, [])
Expand Down
5 changes: 3 additions & 2 deletions lib/supavisor/db_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@

case try_ssl_handshake({:gen_tcp, sock}, auth) do
{:ok, sock} ->
case send_startup(sock, auth) do

Check warning on line 75 in lib/supavisor/db_handler.ex

View workflow job for this annotation

GitHub Actions / Formatting Checks

Function body is nested too deep (max depth is 2, was 3).
:ok ->
:ok = activate(sock)
{:next_state, :authentication, %{data | sock: sock}}
Expand Down Expand Up @@ -225,7 +225,7 @@

def handle_event(:internal, :check_buffer, :idle, %{buffer: buff} = data) do
if buff != [] do
Logger.warning("Buffer is not empty, try to send #{IO.iodata_length(buff)} bytes")
Logger.info("Buffer is not empty, try to send #{IO.iodata_length(buff)} bytes")
abc3 marked this conversation as resolved.
Show resolved Hide resolved
buff = Enum.reverse(buff)
:ok = sock_send(data.sock, buff)
end
Expand All @@ -252,7 +252,7 @@
end

def handle_event({:call, {pid, _} = from}, {:db_call, bin}, state, %{buffer: buff} = data) do
Logger.warning(
Logger.info(
abc3 marked this conversation as resolved.
Show resolved Hide resolved
"state #{state} <-- <-- bin #{inspect(byte_size(bin))} bytes, caller: #{inspect(pid)}"
)

Expand Down Expand Up @@ -329,6 +329,7 @@
[
verify: :verify_peer,
cacerts: [auth.upstream_tls_ca],
server_name_indication: String.to_charlist(auth.host),
customize_hostname_check: [{:match_fun, fn _, _ -> true end}]
]

Expand Down
1 change: 1 addition & 0 deletions lib/supavisor/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
[
{:verify, :verify_peer},
{:cacerts, [upstream_cert(params["upstream_tls_ca"])]},
{:server_name_indication, String.to_charlist(params["db_host"])},
{:customize_hostname_check, [{:match_fun, fn _, _ -> true end}]}
]
end
Expand All @@ -51,7 +52,7 @@
Postgrex.query(conn, "select version()", [])
|> case do
{:ok, %{rows: [[version]]}} ->
if !params["require_user"] do

Check warning on line 55 in lib/supavisor/helpers.ex

View workflow job for this annotation

GitHub Actions / Formatting Checks

Function body is nested too deep (max depth is 2, was 3).
case get_user_secret(conn, params["auth_query"], user["db_user"]) do
{:ok, _} ->
{:halt, {:ok, version}}
Expand Down
13 changes: 13 additions & 0 deletions lib/supavisor/tenants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ defmodule Supavisor.Tenants do
Tenant |> Repo.get_by(external_id: external_id) |> Repo.preload(:users)
end

@spec get_user_cache(String.t(), String.t() | nil, String.t() | nil) ::
{:ok, map()} | {:error, any()}
def get_user_cache(user, external_id, sni_hostname) do
cache_key = {:user_cache, user, external_id, sni_hostname}

case Cachex.fetch(Supavisor.Cache, cache_key, fn _key ->
{:commit, {:cached, get_user(user, external_id, sni_hostname)}, ttl: 5_000}
end) do
{_, {:cached, value}} -> value
{_, {:cached, value}, _} -> value
end
end

@spec get_user(String.t(), String.t() | nil, String.t() | nil) ::
{:ok, map()} | {:error, any()}
def get_user(_, nil, nil) do
Expand Down
Loading