From 645e56baf093aa097629962366df86311613b2c7 Mon Sep 17 00:00:00 2001 From: Stas Date: Thu, 12 Oct 2023 20:11:24 +0200 Subject: [PATCH 1/4] fix: add naive cache for user query, pass server_name_indication, change log level --- lib/supavisor/client_handler.ex | 2 +- lib/supavisor/db_handler.ex | 5 +++-- lib/supavisor/helpers.ex | 1 + lib/supavisor/tenants.ex | 13 +++++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/supavisor/client_handler.ex b/lib/supavisor/client_handler.ex index 44da1a65..e4036ab2 100644 --- a/lib/supavisor/client_handler.ex +++ b/lib/supavisor/client_handler.ex @@ -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, []) diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index 07e01a7c..ce9e4854 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -225,7 +225,7 @@ defmodule Supavisor.DbHandler do 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") buff = Enum.reverse(buff) :ok = sock_send(data.sock, buff) end @@ -252,7 +252,7 @@ defmodule Supavisor.DbHandler do end def handle_event({:call, {pid, _} = from}, {:db_call, bin}, state, %{buffer: buff} = data) do - Logger.warning( + Logger.info( "state #{state} <-- <-- bin #{inspect(byte_size(bin))} bytes, caller: #{inspect(pid)}" ) @@ -329,6 +329,7 @@ defmodule Supavisor.DbHandler do [ verify: :verify_peer, cacerts: [auth.upstream_tls_ca], + server_name_indication: String.to_charlist(auth.host), customize_hostname_check: [{:match_fun, fn _, _ -> true end}] ] diff --git a/lib/supavisor/helpers.ex b/lib/supavisor/helpers.ex index 55ceb400..e33b556a 100644 --- a/lib/supavisor/helpers.ex +++ b/lib/supavisor/helpers.ex @@ -29,6 +29,7 @@ defmodule Supavisor.Helpers do [ {: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 diff --git a/lib/supavisor/tenants.ex b/lib/supavisor/tenants.ex index f4653fbc..6a7e0cb9 100644 --- a/lib/supavisor/tenants.ex +++ b/lib/supavisor/tenants.ex @@ -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 From 7730c52e9d5361260c04db3a9e2c3e95914089de Mon Sep 17 00:00:00 2001 From: Stas Date: Thu, 12 Oct 2023 20:13:58 +0200 Subject: [PATCH 2/4] bump version --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 56f31511..8225a4ba 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.9.10 +0.9.11 From cdbeef5e2948abebd06ecaaf73cbc7e7818f265e Mon Sep 17 00:00:00 2001 From: Stas Date: Fri, 13 Oct 2023 11:55:07 +0300 Subject: [PATCH 3/4] Update lib/supavisor/db_handler.ex Co-authored-by: Chase Granberry --- lib/supavisor/db_handler.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index ce9e4854..237048cc 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -225,7 +225,7 @@ defmodule Supavisor.DbHandler do def handle_event(:internal, :check_buffer, :idle, %{buffer: buff} = data) do if buff != [] do - Logger.info("Buffer is not empty, try to send #{IO.iodata_length(buff)} bytes") + Logger.debug("Buffer is not empty, try to send #{IO.iodata_length(buff)} bytes") buff = Enum.reverse(buff) :ok = sock_send(data.sock, buff) end From 95df63e0cae90f3ac47aecaf7cb392eb475278da Mon Sep 17 00:00:00 2001 From: Stas Date: Fri, 13 Oct 2023 11:55:14 +0300 Subject: [PATCH 4/4] Update lib/supavisor/db_handler.ex Co-authored-by: Chase Granberry --- lib/supavisor/db_handler.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/supavisor/db_handler.ex b/lib/supavisor/db_handler.ex index 237048cc..0158c1e9 100644 --- a/lib/supavisor/db_handler.ex +++ b/lib/supavisor/db_handler.ex @@ -252,7 +252,7 @@ defmodule Supavisor.DbHandler do end def handle_event({:call, {pid, _} = from}, {:db_call, bin}, state, %{buffer: buff} = data) do - Logger.info( + Logger.debug( "state #{state} <-- <-- bin #{inspect(byte_size(bin))} bytes, caller: #{inspect(pid)}" )