haproxy: properly close socket after query
This commit is contained in:
parent
0ec71ea8bb
commit
e62aafd172
2 changed files with 12 additions and 5 deletions
|
@ -70,7 +70,7 @@ defmodule HAHandler.Control do
|
||||||
{:secondary, "MAINT"} ->
|
{:secondary, "MAINT"} ->
|
||||||
:noop
|
:noop
|
||||||
unknown ->
|
unknown ->
|
||||||
Logger.warning("Unhandled PGSQL/HAProxy state: #{unknown}")
|
Logger.warning("Unhandled PGSQL/HAProxy state: #{inspect(unknown)}")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -26,9 +26,13 @@ defmodule HAHandler.HAProxy do
|
||||||
"""
|
"""
|
||||||
def execute(command) when is_binary(command) do
|
def execute(command) when is_binary(command) do
|
||||||
case open_socket(haproxy_socket()) do
|
case open_socket(haproxy_socket()) do
|
||||||
{:ok, socket} ->
|
{:ok, fd, port} ->
|
||||||
send(socket, {self(), {:command, command <> "\n"}})
|
send(port, {self(), {:command, command <> "\n"}})
|
||||||
read_from_socket(socket)
|
data = read_from_socket(port)
|
||||||
|
# We need to be careful and always close file descriptors, as they can
|
||||||
|
# run out - which will block all operations.
|
||||||
|
close_socket(fd)
|
||||||
|
data
|
||||||
{:error, err} ->
|
{:error, err} ->
|
||||||
{:error, err}
|
{:error, err}
|
||||||
end
|
end
|
||||||
|
@ -158,13 +162,16 @@ defmodule HAHandler.HAProxy do
|
||||||
{fdin, fdout} = {socket, socket}
|
{fdin, fdout} = {socket, socket}
|
||||||
port = Port.open({:fd, fdin, fdout}, [{:line, 10_000}, :binary])
|
port = Port.open({:fd, fdin, fdout}, [{:line, 10_000}, :binary])
|
||||||
|
|
||||||
{:ok, port}
|
{:ok, socket, port}
|
||||||
|
|
||||||
{:error, err} ->
|
{:error, err} ->
|
||||||
{:error, err}
|
{:error, err}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Close UNIX fd opened with `open_socket/1`.
|
||||||
|
defp close_socket(fd), do: Socket.close(fd)
|
||||||
|
|
||||||
# Messages may be split due to the `{:line, L}` option specific in
|
# Messages may be split due to the `{:line, L}` option specific in
|
||||||
# `open_socket/1`.
|
# `open_socket/1`.
|
||||||
defp read_from_socket(socket, acc \\ "") do
|
defp read_from_socket(socket, acc \\ "") do
|
||||||
|
|
Loading…
Reference in a new issue