Skip to content

Commit

Permalink
Handle domain socket host names (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmkarlsson authored Nov 13, 2024
1 parent 19f030d commit 6333127
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/pgo_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,17 @@ close(#conn{socket=Socket}) ->
open(Pool, PoolConfig) ->
Host = maps:get(host, PoolConfig, ?DEFAULT_HOST),
Port = maps:get(port, PoolConfig, ?DEFAULT_PORT),
ConnectHost = get_connect_address(Host, Port),
ConnectPort = case ConnectHost of
{local, _} -> 0;
_ -> Port
end,
SockOpts = maps:get(socket_options, PoolConfig, []),
TraceDefault = maps:get(trace, PoolConfig, true),
IncludeStatementDefault = maps:get(include_statement_span_attribute, PoolConfig, true),
QueueDefault = maps:get(queue, PoolConfig, true),
DefaultDecodeOpts = maps:get(decode_opts, PoolConfig, []),
case gen_tcp:connect(Host, Port, SockOpts ++ [binary, {packet, raw}, {active, false}]) of
case gen_tcp:connect(ConnectHost, ConnectPort, SockOpts ++ [binary, {packet, raw}, {active, false}]) of
{ok, Socket} ->
Conn = #conn{pool=Pool,
owner=self(),
Expand Down Expand Up @@ -683,3 +688,22 @@ decode_ready_for_query_message(<<$E>>) ->
{ok, #ready_for_query{transaction_status=error}};
decode_ready_for_query_message(Payload) ->
{error, {unknown_message, ready_for_query, Payload}}.

get_connect_address(Host, Port) ->
case Host of
"/" ++ _Path ->
% Host which looks like an absolute path
% is treated like a unix domain socket
{local, domain_socket_path(Host, Port)};
"@" ++ Path ->
% Host beginning with @ is treated like
% an abstract domain socket. The path
% to the socket should begin with \0.
{local, [0] ++ domain_socket_path(Path, Port)};
_ ->
Host
end.

domain_socket_path(Dir, Port) ->
Socket = ".s.PGSQL." ++ integer_to_list(Port),
filename:join([Dir, Socket]).

0 comments on commit 6333127

Please sign in to comment.