Skip to content

Commit

Permalink
fix: cache for user query, server_name_indication, change log level (#…
Browse files Browse the repository at this point in the history
…179)

* fix: add naive cache for user query, pass server_name_indication, change log level
  • Loading branch information
abc3 authored Oct 13, 2023
1 parent 9a19132 commit 0d5c692
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
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 @@ -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.debug("Buffer is not empty, try to send #{IO.iodata_length(buff)} bytes")
buff = Enum.reverse(buff)
:ok = sock_send(data.sock, buff)
end
Expand All @@ -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.debug(
"state #{state} <-- <-- bin #{inspect(byte_size(bin))} bytes, caller: #{inspect(pid)}"
)

Expand Down Expand Up @@ -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}]
]

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 @@ 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
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

0 comments on commit 0d5c692

Please sign in to comment.