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

feat: add new transaction handler #402

Open
wants to merge 17 commits into
base: v2
Choose a base branch
from
Open

feat: add new transaction handler #402

wants to merge 17 commits into from

Conversation

abc3
Copy link
Member

@abc3 abc3 commented Jul 18, 2024

No description provided.

@abc3 abc3 marked this pull request as ready for review July 24, 2024 11:36
@abc3 abc3 requested a review from a team as a code owner July 24, 2024 11:36
lib/supavisor.ex Outdated
opts =
%{
max_connections: args.max_clients,
num_acceptors: if(acceptors < 10, do: 10, else: acceptors),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
num_acceptors: if(acceptors < 10, do: 10, else: acceptors),
num_acceptors: max(acceptors, 10),

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh, cool, didn't know that

alias Supavisor.Helpers, as: H
alias Supavisor.HandlerHelpers, as: HH

alias Supavisor.{
alias S.{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
alias S.{
alias Supavisor.{

I am not a fan of alias Supavisor, as: S, and this make it even worse.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, let's make it more readable

Comment on lines 298 to 340
with {:ok, sup} <-
Supavisor.start_dist(data.id, data.auth_secrets, log_level: data.log_level),
true <- if(node(sup) == node(), do: true, else: :proxy),
{:ok, opts} <- Supavisor.subscribe(sup, data.id) do
Process.monitor(opts.workers.manager)
data = Map.merge(data, opts.workers)
data = %{data | idle_timeout: opts.idle_timeout}

next =
if opts.ps == [] do
{:timeout, 10_000, :wait_ps}
else
{:next_event, :internal, {:client, {:greetings, opts.ps}}}
end

{:keep_state, data, next}
else
{:error, :max_clients_reached} ->
msg = "Max client connections reached"
Logger.error("ClientHandler: #{msg}")
:ok = HH.send_error(data.sock, "XX000", msg)
Telem.client_join(:fail, data.id)
{:stop, {:shutdown, :max_clients_reached}}

:proxy ->
{:ok, %{port: port, host: host}} = S.get_pool_ranch(data.id)

auth =
Map.merge(data.auth, %{
port: port,
host: host,
ip_version: :v4,
upstream_ssl: false,
upstream_tls_ca: nil,
upstream_verify: nil
})

data = Map.merge(data, %{auth: auth, mode: :session, proxy: true})
{:keep_state, data, {:next_event, :internal, {:client, :connect_db}}}

error ->
Logger.error("ClientHandler: Subscribe error: #{inspect(error)}")
{:keep_state_and_data, {:timeout, 1000, :subscribe}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That with looks super heavy, but I do not have immediate plan how to make it cleaner.

case ProxyDb.send_startup(sock, auth) do
tenant = if(data.proxy, do: S.tenant(data.id))

case Db.send_startup(sock, auth, tenant) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deep case nesting.

clients when clients == [{self(), []}] or clients == [] -> PromEx.remove_metrics(data.id)
_ -> :ok
if data.id != nil do
with clients <- Registry.lookup(Supavisor.Registry.TenantClients, data.id),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use with for this clause? It cannot fail.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

@abc3 abc3 requested a review from hauleth July 26, 2024 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants