@@ -44,7 +44,7 @@ defmodule Realtime.Database do
4444 @ doc """
4545 Creates a database connection struct from the given tenant.
4646 """
47- @ spec from_tenant ( Tenant . t ( ) , binary ( ) , :stop | :exp | :rand | :rand_exp ) :: t ( )
47+ @ spec from_tenant ( Tenant . t ( ) , binary ( ) , :stop | :exp | :rand | :rand_exp ) :: { :ok , t ( ) } | { :error , :nxdomain }
4848 def from_tenant ( % Tenant { } = tenant , application_name , backoff \\ :rand_exp ) do
4949 tenant
5050 |> then ( & Realtime.PostgresCdc . filter_settings ( @ cdc , & 1 . extensions ) )
@@ -54,7 +54,7 @@ defmodule Realtime.Database do
5454 @ doc """
5555 Creates a database connection struct from the given settings.
5656 """
57- @ spec from_settings ( map ( ) , binary ( ) , :stop | :exp | :rand | :rand_exp ) :: t ( )
57+ @ spec from_settings ( map ( ) , binary ( ) , :stop | :exp | :rand | :rand_exp ) :: { :ok , t ( ) } | { :error , :nxdomain }
5858 def from_settings ( settings , application_name , backoff \\ :rand_exp ) do
5959 pool = pool_size_by_application_name ( application_name , settings )
6060
@@ -65,22 +65,24 @@ defmodule Realtime.Database do
6565 |> Map . new ( )
6666 |> then ( & Map . merge ( settings , & 1 ) )
6767
68- { :ok , addrtype } = detect_ip_version ( settings [ "db_host" ] )
69- ssl = if default_ssl_param ( settings ) , do: [ verify: :verify_none ] , else: false
70-
71- % __MODULE__ {
72- hostname: settings [ "db_host" ] ,
73- port: String . to_integer ( settings [ "db_port" ] ) ,
74- database: settings [ "db_name" ] ,
75- username: settings [ "db_user" ] ,
76- password: settings [ "db_password" ] ,
77- pool_size: pool ,
78- queue_target: settings [ "db_queue_target" ] || 5_000 ,
79- application_name: application_name ,
80- backoff_type: backoff ,
81- socket_options: [ addrtype ] ,
82- ssl: ssl
83- }
68+ with { :ok , addrtype } <- detect_ip_version ( settings [ "db_host" ] ) do
69+ ssl = if default_ssl_param ( settings ) , do: [ verify: :verify_none ] , else: false
70+
71+ { :ok ,
72+ % __MODULE__ {
73+ hostname: settings [ "db_host" ] ,
74+ port: String . to_integer ( settings [ "db_port" ] ) ,
75+ database: settings [ "db_name" ] ,
76+ username: settings [ "db_user" ] ,
77+ password: settings [ "db_password" ] ,
78+ pool_size: pool ,
79+ queue_target: settings [ "db_queue_target" ] || 5_000 ,
80+ application_name: application_name ,
81+ backoff_type: backoff ,
82+ socket_options: [ addrtype ] ,
83+ ssl: ssl
84+ } }
85+ end
8486 end
8587
8688 @ available_connection_factor 0.95
@@ -97,10 +99,10 @@ defmodule Realtime.Database do
9799 |> then ( & PostgresCdc . filter_settings ( @ cdc , & 1 . extensions ) )
98100 |> then ( fn settings ->
99101 required_pool = tenant_pool_requirements ( settings )
100- check_settings = from_settings ( settings , "realtime_connect" , :stop )
101- check_settings = Map . put ( check_settings , :max_restarts , 0 )
102102
103- with { :ok , conn } <- connect_db ( check_settings ) ,
103+ with { :ok , base_settings } <- from_settings ( settings , "realtime_connect" , :stop ) ,
104+ check_settings = % { base_settings | max_restarts: 0 } ,
105+ { :ok , conn } <- connect_db ( check_settings ) ,
104106 { :ok , [ available_connections , migrations_ran ] } <- query_connection_info ( conn ) do
105107 requirement = ceil ( required_pool * @ available_connection_factor )
106108
@@ -154,9 +156,9 @@ defmodule Realtime.Database do
154156 @ spec connect ( Tenant . t ( ) , binary ( ) , :stop | :exp | :rand | :rand_exp ) ::
155157 { :ok , pid ( ) } | { :error , any ( ) }
156158 def connect ( tenant , application_name , backoff \\ :stop ) do
157- tenant
158- |> from_tenant ( application_name , backoff )
159- |> connect_db ( )
159+ with { :ok , settings } <- from_tenant ( tenant , application_name , backoff ) do
160+ connect_db ( settings )
161+ end
160162 end
161163
162164 @ doc """
0 commit comments