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

Use Mint.HTTP1 in HTTP1 conn #270

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 16 additions & 15 deletions lib/finch/http1/conn.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Finch.HTTP1.Conn do

alias Finch.SSL
alias Finch.Telemetry
alias Mint.HTTP1

def new(scheme, host, port, opts, parent) do
%{
Expand Down Expand Up @@ -49,7 +50,7 @@ defmodule Finch.HTTP1.Conn do
|> Keyword.put(:mode, :passive)
|> Keyword.put_new(:protocols, [:http1])

case Mint.HTTP.connect(conn.scheme, conn.host, conn.port, conn_opts) do
case HTTP1.connect(conn.scheme, conn.host, conn.port, conn_opts) do
{:ok, mint} ->
Telemetry.stop(:connect, start_time, meta)
SSL.maybe_log_secrets(conn.scheme, conn_opts, mint)
Expand All @@ -63,16 +64,16 @@ defmodule Finch.HTTP1.Conn do
end

def transfer(conn, pid) do
case Mint.HTTP.controlling_process(conn.mint, pid) do
# Mint.HTTP.controlling_process causes a side-effect, but it doesn't actually
case HTTP1.controlling_process(conn.mint, pid) do
# controlling_process/2 causes a side-effect, but it doesn't actually
# change the conn, so we can ignore the value returned above.
{:ok, _} -> {:ok, conn}
{:error, error} -> {:error, conn, error}
end
end

def open?(%{mint: nil}), do: false
def open?(%{mint: mint}), do: Mint.HTTP.open?(mint)
def open?(%{mint: mint}), do: HTTP1.open?(mint)

def idle_time(conn, unit \\ :native) do
idle_time = System.monotonic_time() - conn.last_checkin
Expand All @@ -84,7 +85,7 @@ defmodule Finch.HTTP1.Conn do
def reusable?(%{max_idle_time: max_idle_time}, idle_time), do: idle_time <= max_idle_time

def set_mode(conn, mode) when mode in [:active, :passive] do
case Mint.HTTP.set_mode(conn.mint, mode) do
case HTTP1.set_mode(conn.mint, mode) do
{:ok, mint} -> {:ok, %{conn | mint: mint}}
_ -> {:error, "Connection is dead"}
end
Expand All @@ -93,7 +94,7 @@ defmodule Finch.HTTP1.Conn do
def discard(%{mint: nil}, _), do: :unknown

def discard(conn, message) do
case Mint.HTTP.stream(conn.mint, message) do
case HTTP1.stream(conn.mint, message) do
{:ok, mint, _responses} -> {:ok, %{conn | mint: mint}}
{:error, _, reason, _} -> {:error, reason}
:unknown -> :unknown
Expand All @@ -112,7 +113,7 @@ defmodule Finch.HTTP1.Conn do
start_time = Telemetry.start(:send, metadata, extra_measurements)

try do
case Mint.HTTP.request(
case HTTP1.request(
conn.mint,
req.method,
full_path,
Expand Down Expand Up @@ -174,23 +175,23 @@ defmodule Finch.HTTP1.Conn do

defp maybe_stream_request_body(mint, ref, {:stream, stream}) do
with {:ok, mint} <- stream_request_body(mint, ref, stream) do
Mint.HTTP.stream_request_body(mint, ref, :eof)
HTTP1.stream_request_body(mint, ref, :eof)
end
end

defp maybe_stream_request_body(mint, _, _), do: {:ok, mint}

defp stream_request_body(mint, ref, stream) do
Enum.reduce_while(stream, {:ok, mint}, fn
chunk, {:ok, mint} -> {:cont, Mint.HTTP.stream_request_body(mint, ref, chunk)}
chunk, {:ok, mint} -> {:cont, HTTP1.stream_request_body(mint, ref, chunk)}
_chunk, error -> {:halt, error}
end)
end

def close(%{mint: nil} = conn), do: conn

def close(conn) do
{:ok, mint} = Mint.HTTP.close(conn.mint)
{:ok, mint} = HTTP1.close(conn.mint)
%{conn | mint: mint}
end

Expand Down Expand Up @@ -243,7 +244,7 @@ defmodule Finch.HTTP1.Conn do
resp_metadata
)
when timeouts.request_timeout < 0 do
{:ok, mint} = Mint.HTTP1.close(mint)
{:ok, mint} = HTTP1.close(mint)
{:error, mint, %Mint.TransportError{reason: :timeout}, resp_metadata}
end

Expand All @@ -259,7 +260,7 @@ defmodule Finch.HTTP1.Conn do
) do
start_time = System.monotonic_time(:millisecond)

case Mint.HTTP.recv(mint, 0, timeouts.receive_timeout) do
case HTTP1.recv(mint, 0, timeouts.receive_timeout) do
{:ok, mint, entries} ->
timeouts =
if is_integer(timeouts.request_timeout) do
Expand Down Expand Up @@ -311,7 +312,7 @@ defmodule Finch.HTTP1.Conn do
)

{:halt, acc} ->
{:ok, mint} = Mint.HTTP1.close(mint)
{:ok, mint} = HTTP1.close(mint)
{:ok, mint, acc, resp_metadata}

other ->
Expand All @@ -335,7 +336,7 @@ defmodule Finch.HTTP1.Conn do
)

{:halt, acc} ->
{:ok, mint} = Mint.HTTP1.close(mint)
{:ok, mint} = HTTP1.close(mint)
{:ok, mint, acc, resp_metadata}

other ->
Expand All @@ -357,7 +358,7 @@ defmodule Finch.HTTP1.Conn do
)

{:halt, acc} ->
{:ok, mint} = Mint.HTTP1.close(mint)
{:ok, mint} = HTTP1.close(mint)
{:ok, mint, acc, resp_metadata}

other ->
Expand Down
2 changes: 1 addition & 1 deletion test/finch/http1/pool_metrics_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ defmodule Finch.HTTP1.PoolMetricsTest do
end

test "get pool status with raise before checkin", %{finch_name: finch_name} do
stub(Mint.HTTP, :request, fn _, _, _, _, _ ->
stub(Mint.HTTP1, :request, fn _, _, _, _, _ ->
raise "unexpected error"
end)

Expand Down
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Finch.HTTP2Server.start(port)
Application.put_env(:finch, :test_https_h2_url, "https://localhost:#{port}")

Mimic.copy(Mint.HTTP)
Mimic.copy(Mint.HTTP1)

ExUnit.start()
Application.ensure_all_started(:bypass)
Expand Down
Loading