Skip to content

Commit

Permalink
fallback to ipv4/ipv6 in the event of an unreachable host error (#101)
Browse files Browse the repository at this point in the history
If there is a request failure and the reason implies that the error is
due to the usage being ipv4 or ipv6, the alternative is set and the
request retries. A variable is used to determine if a retry should be
attempted or not. If it is false, this means that a retry is already
being attempted, and the request should raise.

This is based on:

https://github.com/elixir-lang/elixir/blob/9c42348f6407fa315bae00acffe433e9417e2c2e/lib/mix/lib/mix/utils.ex#L687
  • Loading branch information
Gazler authored Oct 18, 2024
1 parent f729dc3 commit f13003f
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/tailwind.ex
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ defmodule Tailwind do
end
end

defp fetch_body!(url) do
defp fetch_body!(url, retry \\ true) do
scheme = URI.parse(url).scheme
url = String.to_charlist(url)
Logger.debug("Downloading tailwind from #{url}")
Expand Down Expand Up @@ -301,10 +301,16 @@ defmodule Tailwind do

options = [body_format: :binary]

case :httpc.request(:get, {url, []}, http_options, options) do
{:ok, {{_, 200, _}, _headers, body}} ->
case {retry, :httpc.request(:get, {url, []}, http_options, options)} do
{_, {:ok, {{_, 200, _}, _headers, body}}} ->
body

{true, {:error, {:failed_connect, [{:to_address, _}, {inet, _, reason}]}}}
when inet in [:inet, :inet6] and
reason in [:ehostunreach, :enetunreach, :eprotonosupport, :nxdomain] ->
:httpc.set_options(ipfamily: fallback(inet))
fetch_body!(url, false)

other ->
raise """
Couldn't fetch #{url}: #{inspect(other)}
Expand All @@ -324,6 +330,9 @@ defmodule Tailwind do
end
end

defp fallback(:inet), do: :inet6
defp fallback(:inet6), do: :inet

defp proxy_for_scheme("http") do
System.get_env("HTTP_PROXY") || System.get_env("http_proxy")
end
Expand Down

0 comments on commit f13003f

Please sign in to comment.