Skip to content

Commit

Permalink
add default hostname to unix socket pools
Browse files Browse the repository at this point in the history
  • Loading branch information
cgerling committed Oct 15, 2021
1 parent a9710ee commit 6247f99
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
14 changes: 14 additions & 0 deletions lib/finch/pool_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ defmodule Finch.PoolManager do
:versions
]

@default_conn_hostname "localhost"

def start_link(config) do
GenServer.start_link(__MODULE__, config, name: config.manager_name)
end
Expand Down Expand Up @@ -78,6 +80,7 @@ defmodule Finch.PoolManager do
config
|> Map.get(shp, default)
|> maybe_drop_tls_options(shp)
|> maybe_add_hostname(shp)
end

# Drop TLS options from :conn_opts for default pools with :http scheme,
Expand All @@ -95,6 +98,17 @@ defmodule Finch.PoolManager do

defp maybe_drop_tls_options(config, _), do: config

# Hostname is required when the address is not a url (binary) so we need to specify
# a default value in case the configuration does not specify one.
defp maybe_add_hostname(config, {_scheme, {:local, _path}, _port} = _shp) when is_map(config) do
conn_opts =
config |> Map.get(:conn_opts, []) |> Keyword.put_new(:hostname, @default_conn_hostname)

Map.put(config, :conn_opts, conn_opts)
end

defp maybe_add_hostname(config, _), do: config

defp pool_mod(:http1), do: Finch.HTTP1.Pool
defp pool_mod(:http2), do: Finch.HTTP2.Pool
end
16 changes: 4 additions & 12 deletions test/finch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ defmodule FinchTest do
pools: %{
endpoint(bypass, "/some-path") => [count: 5, size: 5],
endpoint(other_bypass, "/some-other-path") => [count: 10, size: 10],
{:http, unix_socket} => [count: 5, size: 5, conn_opts: [hostname: "localhost"]],
{:https, unix_socket} => [count: 10, size: 10, conn_opts: [hostname: "localhost"]]
{:http, unix_socket} => [count: 5, size: 5],
{:https, unix_socket} => [count: 10, size: 10]
}}
)

Expand Down Expand Up @@ -262,17 +262,9 @@ defmodule FinchTest do
end

test "successful get request when host is a unix socket" do
{:ok, socket_address} = MockSocketServer.start()

start_supervised!(
{Finch,
name: MyFinch,
pools: %{
{:http, socket_address} => [conn_opts: [hostname: "localhost"]]
}}
)
start_supervised!({Finch, name: MyFinch})

{:local, socket_path} = socket_address
{:ok, {:local, socket_path}} = MockSocketServer.start()

Finch.build(:get, "http://localhost/", [], nil, unix_socket: socket_path)
|> Finch.request(MyFinch)
Expand Down

0 comments on commit 6247f99

Please sign in to comment.