From 2a83c0ba935355b4ada582315de68b456e727af7 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 7 Mar 2019 23:18:59 +0000 Subject: [PATCH 1/6] http: safely catch erlang exits and elixir errors from hackney (ref #672) --- lib/pleroma/http/http.ex | 41 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/pleroma/http/http.ex b/lib/pleroma/http/http.ex index 26214ef3f..c6d86b3d3 100644 --- a/lib/pleroma/http/http.ex +++ b/lib/pleroma/http/http.ex @@ -27,22 +27,29 @@ defmodule Pleroma.HTTP do """ def request(method, url, body \\ "", headers \\ [], options \\ []) do - options = - process_request_options(options) - |> process_sni_options(url) - |> process_adapter_options() + try do + options = + process_request_options(options) + |> process_sni_options(url) - params = Keyword.get(options, :params, []) + params = Keyword.get(options, :params, []) - %{} - |> Builder.method(method) - |> Builder.headers(headers) - |> Builder.opts(options) - |> Builder.url(url) - |> Builder.add_param(:body, :body, body) - |> Builder.add_param(:query, :query, params) - |> Enum.into([]) - |> (&Tesla.request(Connection.new(), &1)).() + %{} + |> Builder.method(method) + |> Builder.headers(headers) + |> Builder.opts(options) + |> Builder.url(url) + |> Builder.add_param(:body, :body, body) + |> Builder.add_param(:query, :query, params) + |> Enum.into([]) + |> (&Tesla.request(Connection.new(), &1)).() + rescue + e -> + {:error, e} + catch + :exit, e -> + {:error, e} + end end defp process_sni_options(options, nil), do: options @@ -57,12 +64,6 @@ defmodule Pleroma.HTTP do end end - def process_adapter_options(options) do - adapter_options = Pleroma.Config.get([:http, :adapter], []) - - options ++ [adapter: adapter_options] - end - def process_request_options(options) do config = Application.get_env(:pleroma, :http, []) proxy = Keyword.get(config, :proxy_url, nil) From 50ba4ba2c9ebe522f5981d19e51f7ae41a02e7d8 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 7 Mar 2019 23:31:59 +0000 Subject: [PATCH 2/6] http: connection: merge hackney option lists instead of concatenating them this ensures the right pools are used --- lib/pleroma/http/connection.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/http/connection.ex b/lib/pleroma/http/connection.ex index b798eaa5a..cc2e96e2b 100644 --- a/lib/pleroma/http/connection.ex +++ b/lib/pleroma/http/connection.ex @@ -31,6 +31,6 @@ defmodule Pleroma.HTTP.Connection do # defp hackney_options(opts) do options = Keyword.get(opts, :adapter, []) - @hackney_options ++ options + Keyword.merge(@hackney_options, options) end end From 773f532b148eb0c9ca1e73c3b4ebaf6aa8337da9 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 7 Mar 2019 23:32:58 +0000 Subject: [PATCH 3/6] http: actually pass the options list to the Connection factory --- lib/pleroma/http/http.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/http/http.ex b/lib/pleroma/http/http.ex index c6d86b3d3..c5f720bc9 100644 --- a/lib/pleroma/http/http.ex +++ b/lib/pleroma/http/http.ex @@ -42,7 +42,7 @@ defmodule Pleroma.HTTP do |> Builder.add_param(:body, :body, body) |> Builder.add_param(:query, :query, params) |> Enum.into([]) - |> (&Tesla.request(Connection.new(), &1)).() + |> (&Tesla.request(Connection.new(options), &1)).() rescue e -> {:error, e} From 19afd9f81ff0b0e966a432db6ea15930d3712c85 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Thu, 7 Mar 2019 23:49:02 +0000 Subject: [PATCH 4/6] http: rework connection timeouts to match hackney docs, enforce 1 second max TCP connection timeout --- lib/pleroma/http/connection.ex | 4 ++-- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 1 - lib/pleroma/web/rel_me.ex | 1 - lib/pleroma/web/rich_media/parser.ex | 1 - 4 files changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/http/connection.ex b/lib/pleroma/http/connection.ex index cc2e96e2b..926f892ba 100644 --- a/lib/pleroma/http/connection.ex +++ b/lib/pleroma/http/connection.ex @@ -8,8 +8,8 @@ defmodule Pleroma.HTTP.Connection do """ @hackney_options [ - timeout: 10000, - recv_timeout: 20000, + connect_timeout: 1_000, + recv_timeout: 10_000, follow_redirect: true, pool: :federation ] diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 8c58f4545..26921d386 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1452,7 +1452,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do url, [], adapter: [ - timeout: timeout, recv_timeout: timeout, pool: :default ] diff --git a/lib/pleroma/web/rel_me.ex b/lib/pleroma/web/rel_me.ex index ab29a36e3..eaca41132 100644 --- a/lib/pleroma/web/rel_me.ex +++ b/lib/pleroma/web/rel_me.ex @@ -5,7 +5,6 @@ defmodule Pleroma.Web.RelMe do @hackney_options [ pool: :media, - timeout: 2_000, recv_timeout: 2_000, max_body: 2_000_000 ] diff --git a/lib/pleroma/web/rich_media/parser.ex b/lib/pleroma/web/rich_media/parser.ex index 4341141df..4bd271d8e 100644 --- a/lib/pleroma/web/rich_media/parser.ex +++ b/lib/pleroma/web/rich_media/parser.ex @@ -11,7 +11,6 @@ defmodule Pleroma.Web.RichMedia.Parser do @hackney_options [ pool: :media, - timeout: 2_000, recv_timeout: 2_000, max_body: 2_000_000 ] From ba48bd901c3beabb6bcb5af7b8d4232965e0c618 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 8 Mar 2019 22:50:43 +0000 Subject: [PATCH 5/6] http: connection: relax the timeouts a little --- lib/pleroma/http/connection.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/http/connection.ex b/lib/pleroma/http/connection.ex index 926f892ba..1477c6dd6 100644 --- a/lib/pleroma/http/connection.ex +++ b/lib/pleroma/http/connection.ex @@ -8,8 +8,8 @@ defmodule Pleroma.HTTP.Connection do """ @hackney_options [ - connect_timeout: 1_000, - recv_timeout: 10_000, + connect_timeout: 2_000, + recv_timeout: 20_000, follow_redirect: true, pool: :federation ] From 1f78d23eed1fe80622837bc45831c406de244bef Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 8 Mar 2019 22:59:10 +0000 Subject: [PATCH 6/6] http: connection: unify adapter configuration and defaults --- lib/pleroma/http/connection.ex | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/http/connection.ex b/lib/pleroma/http/connection.ex index 1477c6dd6..c0173465a 100644 --- a/lib/pleroma/http/connection.ex +++ b/lib/pleroma/http/connection.ex @@ -31,6 +31,10 @@ defmodule Pleroma.HTTP.Connection do # defp hackney_options(opts) do options = Keyword.get(opts, :adapter, []) - Keyword.merge(@hackney_options, options) + adapter_options = Pleroma.Config.get([:http, :adapter], []) + + @hackney_options + |> Keyword.merge(adapter_options) + |> Keyword.merge(options) end end