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

Deprecate :protocol in favour of :protocols #251

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions lib/finch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,19 @@ defmodule Finch do
@pool_config_schema [
protocol: [
type: {:in, [:http2, :http1]},
doc: "The type of connection and pool to use.",
default: :http1
deprecated: "Use :protocols instead."
],
protocols: [
type: {:list, {:in, [:http1, :http2]}},
doc: """
The type of connections to support.

If using `:http1` only, an HTTP1 pool without multiplexing is used. \
If using `:http2` only, an HTTP2 pool with multiplexing is used. \
If both are listed, then both HTTP1/HTTP2 connections are \
supported (via ALPN), but there is no multiplexing.
""",
default: [:http1]
],
size: [
type: :pos_integer,
Expand Down Expand Up @@ -55,10 +66,7 @@ defmodule Finch do
doc: """
These options are passed to `Mint.HTTP.connect/4` whenever a new connection is established. \
`:mode` is not configurable as Finch must control this setting. Typically these options are \
used to configure proxying, https settings, or connect timeouts. \

For HTTP1 pools, the pool will force HTTP1 connections by default but you can perform ALPN \
over HTTP1 pools by setting the `:protocols` option.
used to configure proxying, https settings, or connect timeouts.
""",
default: []
],
Expand Down Expand Up @@ -238,12 +246,30 @@ defmodule Finch do
conn_opts
|> Keyword.put(:ssl_key_log_file_device, ssl_key_log_file_device)
|> Keyword.put(:transport_opts, transport_opts)
|> Keyword.put(:protocols, valid[:protocols])

# TODO: Remove :protocol on v0.18
mod =
case valid[:protocol] do
wojtekmach marked this conversation as resolved.
Show resolved Hide resolved
:http1 ->
Finch.HTTP1.Pool

:http2 ->
Finch.HTTP2.Pool

nil ->
if :http1 in valid[:protocols] do
Finch.HTTP1.Pool
else
Finch.HTTP2.Pool
end
end

%{
mod: mod,
size: valid[:size],
count: valid[:count],
conn_opts: conn_opts,
protocol: valid[:protocol],
conn_max_idle_time: to_native(valid[:max_idle_time] || valid[:conn_max_idle_time]),
pool_max_idle_time: valid[:pool_max_idle_time]
}
Expand Down
3 changes: 2 additions & 1 deletion lib/finch/http1/conn.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Finch.Conn do
defmodule Finch.HTTP1.Conn do
@moduledoc false

alias Finch.SSL
Expand Down Expand Up @@ -41,6 +41,7 @@ defmodule Finch.Conn do
# custom protocols in case they don't know if a connection
# is HTTP1/HTTP2, but they are fine as treating HTTP2
# connections has HTTP2.

conn_opts =
conn.opts
|> Keyword.put(:mode, :passive)
Expand Down
2 changes: 1 addition & 1 deletion lib/finch/http1/pool.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Finch.HTTP1.Pool do
@behaviour NimblePool
@behaviour Finch.Pool

alias Finch.Conn
alias Finch.HTTP1.Conn
alias Finch.Telemetry

def child_spec(opts) do
Expand Down
16 changes: 6 additions & 10 deletions lib/finch/pool_manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ defmodule Finch.PoolManager do

defp do_start_pools(shp, config) do
pool_config = pool_config(config, shp)

pool_args = pool_args(shp, config, pool_config)

pool_mod = pool_mod(pool_config.protocol)

Enum.map(1..pool_config.count, fn _ ->
# Choose pool type here...
{:ok, pid} = DynamicSupervisor.start_child(config.supervisor_name, {pool_mod, pool_args})
{pid, pool_mod}
{:ok, pid} =
DynamicSupervisor.start_child(config.supervisor_name, {pool_config.mod, pool_args})

{pid, pool_config.mod}
end)
|> hd()
end
Expand Down Expand Up @@ -118,12 +117,9 @@ defmodule Finch.PoolManager do

defp maybe_add_hostname(config, _), do: config

defp pool_mod(:http1), do: Finch.HTTP1.Pool
defp pool_mod(:http2), do: Finch.HTTP2.Pool

defp pool_args(shp, config, %{protocol: :http1} = pool_config),
defp pool_args(shp, config, %{mod: Finch.HTTP1.Pool} = pool_config),
do: {shp, config.registry_name, pool_config.size, pool_config, pool_config.pool_max_idle_time}

defp pool_args(shp, config, %{protocol: :http2} = pool_config),
defp pool_args(shp, config, %{mod: Finch.HTTP2.Pool} = pool_config),
do: {shp, config.registry_name, pool_config.size, pool_config}
end
2 changes: 1 addition & 1 deletion test/finch/http1/integration_proxy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule Finch.HTTP1.IntegrationProxyTest do
name: ProxyFinch,
pools: %{
default: [
protocol: :http1,
protocols: [:http1],
conn_opts: [
proxy: {:http, "localhost", proxy_port, []}
]
Expand Down
6 changes: 3 additions & 3 deletions test/finch/http1/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Finch.HTTP1.IntegrationTest do
name: H2Finch,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
conn_opts: [
transport_opts: [
verify: :verify_none
Expand Down Expand Up @@ -114,7 +114,7 @@ defmodule Finch.HTTP1.IntegrationTest do
name: H1Finch,
pools: %{
default: [
protocol: :http1
protocols: [:http1]
]
}}
)
Expand All @@ -132,7 +132,7 @@ defmodule Finch.HTTP1.IntegrationTest do
name: H1Finch,
pools: %{
default: [
protocol: :http1,
protocols: [:http1],
conn_opts: [
transport_opts: [
reuse_sessions: false,
Expand Down
4 changes: 2 additions & 2 deletions test/finch/http1/pool_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule Finch.HTTP1.PoolTest do
name: IdleFinch,
pools: %{
default: [
protocol: :http1,
protocols: [:http1],
pool_max_idle_time: 5
]
}}
Expand Down Expand Up @@ -63,7 +63,7 @@ defmodule Finch.HTTP1.PoolTest do
@describetag bypass: false

setup %{finch_name: finch_name} do
start_supervised!({Finch, name: finch_name, pools: %{default: [protocol: :http1]}})
start_supervised!({Finch, name: finch_name, pools: %{default: [protocols: [:http1]]}})
:ok
end

Expand Down
2 changes: 1 addition & 1 deletion test/finch/http1/telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Finch.HTTP1.TelemetryTest do
end)

start_supervised!(
{Finch, name: finch_name, pools: %{default: [protocol: :http1, conn_max_idle_time: 10]}}
{Finch, name: finch_name, pools: %{default: [protocols: [:http1], conn_max_idle_time: 10]}}
)

:ok
Expand Down
12 changes: 6 additions & 6 deletions test/finch/http2/integration_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Finch.HTTP2.IntegrationTest do
name: TestFinch,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
count: 5,
conn_opts: [
transport_opts: [
Expand All @@ -41,7 +41,7 @@ defmodule Finch.HTTP2.IntegrationTest do
name: TestFinch,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
count: 5,
conn_opts: [
transport_opts: [
Expand All @@ -65,7 +65,7 @@ defmodule Finch.HTTP2.IntegrationTest do
name: TestFinch,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
count: 1,
conn_opts: [
transport_opts: [
Expand Down Expand Up @@ -109,7 +109,7 @@ defmodule Finch.HTTP2.IntegrationTest do
name: TestFinch,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
count: 5,
conn_opts: [
transport_opts: [
Expand Down Expand Up @@ -138,7 +138,7 @@ defmodule Finch.HTTP2.IntegrationTest do
name: TestFinch,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
conn_opts: [
transport_opts: [
verify: :verify_none
Expand Down Expand Up @@ -168,7 +168,7 @@ defmodule Finch.HTTP2.IntegrationTest do
name: TestFinch,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
conn_opts: [
transport_opts: [
verify: :verify_none
Expand Down
2 changes: 1 addition & 1 deletion test/finch/http2/pool_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ defmodule Finch.HTTP2.PoolTest do
name: finch_name,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
count: 5,
conn_opts: [
transport_opts: [
Expand Down
2 changes: 1 addition & 1 deletion test/finch/http2/telemetry_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Finch.HTTP2.TelemetryTest do
end)

start_supervised!(
{Finch, name: finch_name, pools: %{default: [protocol: :http2, conn_max_idle_time: 10]}}
{Finch, name: finch_name, pools: %{default: [protocols: [:http2], conn_max_idle_time: 10]}}
)

:ok
Expand Down
16 changes: 8 additions & 8 deletions test/finch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ defmodule FinchTest do
name: finch_name,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
count: 1,
conn_opts: [
transport_opts: [
Expand Down Expand Up @@ -329,7 +329,7 @@ defmodule FinchTest do
name: finch_name,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
count: 1,
conn_opts: [
transport_opts: [
Expand Down Expand Up @@ -379,7 +379,7 @@ defmodule FinchTest do
name: finch_name,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
count: 1,
conn_opts: [
transport_opts: [
Expand Down Expand Up @@ -601,7 +601,7 @@ defmodule FinchTest do
test "are passed through to the conn", %{bypass: bypass} do
expect_any(bypass)

start_supervised!({Finch, name: H1Finch, pools: %{default: [protocol: :http1]}})
start_supervised!({Finch, name: H1Finch, pools: %{default: [protocols: [:http1]]}})

assert {:ok, _} = Finch.build(:get, endpoint(bypass)) |> Finch.request(H1Finch)

Expand Down Expand Up @@ -660,7 +660,7 @@ defmodule FinchTest do
name: finch_name,
pools: %{
default: [
protocol: :http2,
protocols: [:http2],
count: 1,
conn_opts: [
transport_opts: [
Expand Down Expand Up @@ -839,7 +839,7 @@ defmodule FinchTest do
name: finch_name,
pools: %{
default: [
protocol: :http2
protocols: [:http2]
]
}}
)
Expand Down Expand Up @@ -867,7 +867,7 @@ defmodule FinchTest do
name: finch_name,
pools: %{
default: [
protocol: :http2
protocols: [:http2]
]
}}
)
Expand Down Expand Up @@ -915,7 +915,7 @@ defmodule FinchTest do
name: finch_name,
pools: %{
default: [
protocol: :http2
protocols: [:http2]
]
}}
)
Expand Down
Loading