From b33b785a56bfe8a17fa537bfa54aaa973d268dd5 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Thu, 24 Nov 2022 12:01:51 +0000 Subject: [PATCH 1/3] Add configurable HTTP timeouts --- CHANGELOG.md | 3 +++ config/config.exs | 2 ++ config/description.exs | 14 +++++++++++++ lib/pleroma/http/adapter_helper/default.ex | 8 +++++++- test/pleroma/http/adapter_helper_test.exs | 23 ++++++++++++++++++++-- 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 781fbe6e5..cbf1ac832 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## Unreleased +## Added +- Config: HTTP timeout options, :pool\_timeout and :receive\_timeout + ## Changed - MastoAPI: Accept BooleanLike input on `/api/v1/accounts/:id/follow` (fixes follows with mastodon.py) diff --git a/config/config.exs b/config/config.exs index 5278896f1..04fcd4964 100644 --- a/config/config.exs +++ b/config/config.exs @@ -180,6 +180,8 @@ config :tesla, :adapter, {Tesla.Adapter.Finch, name: MyFinch} # Configures http settings, upstream proxy etc. config :pleroma, :http, + pool_timeout: :timer.seconds(5), + receive_timeout: :timer.seconds(15), proxy_url: nil, user_agent: :default, adapter: [] diff --git a/config/description.exs b/config/description.exs index 994bb7280..7d93e1c0b 100644 --- a/config/description.exs +++ b/config/description.exs @@ -2658,6 +2658,20 @@ config :pleroma, :config_description, [ type: :group, description: "HTTP settings", children: [ + %{ + key: :pool_timeout, + label: "HTTP Pool Request Timeout", + type: :integer, + description: "Timeout for initiating HTTP requests (in ms, default 5000)", + suggestions: [5000] + }, + %{ + key: :receive_timeout, + label: "HTTP Receive Timeout", + type: :integer, + description: "Timeout for waiting on remote servers to respond to HTTP requests (in ms, default 15000)", + suggestions: [15000] + }, %{ key: :proxy_url, label: "Proxy URL", diff --git a/lib/pleroma/http/adapter_helper/default.ex b/lib/pleroma/http/adapter_helper/default.ex index 630536871..fc377b376 100644 --- a/lib/pleroma/http/adapter_helper/default.ex +++ b/lib/pleroma/http/adapter_helper/default.ex @@ -10,7 +10,13 @@ defmodule Pleroma.HTTP.AdapterHelper.Default do @spec options(keyword(), URI.t()) :: keyword() def options(opts, _uri) do proxy = Pleroma.Config.get([:http, :proxy_url]) - AdapterHelper.maybe_add_proxy(opts, AdapterHelper.format_proxy(proxy)) + pool_timeout = Pleroma.Config.get([:http, :pool_timeout], 5000) + receive_timeout = Pleroma.Config.get([:http, :receive_timeout], 15_000) + + opts + |> AdapterHelper.maybe_add_proxy(AdapterHelper.format_proxy(proxy)) + |> Keyword.put(:pool_timeout, pool_timeout) + |> Keyword.put(:receive_timeout, receive_timeout) end @spec get_conn(URI.t(), keyword()) :: {:ok, keyword()} diff --git a/test/pleroma/http/adapter_helper_test.exs b/test/pleroma/http/adapter_helper_test.exs index 55ffe4921..ba09f3422 100644 --- a/test/pleroma/http/adapter_helper_test.exs +++ b/test/pleroma/http/adapter_helper_test.exs @@ -3,8 +3,7 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.HTTP.AdapterHelperTest do - use ExUnit.Case, async: true - + use Pleroma.DataCase, async: true alias Pleroma.HTTP.AdapterHelper describe "format_proxy/1" do @@ -47,4 +46,24 @@ defmodule Pleroma.HTTP.AdapterHelperTest do ] end end + + describe "timeout settings" do + test "should default to 5000/15000" do + options = AdapterHelper.options(%URI{host: 'somewhere'}) + assert options[:pool_timeout] == 5000 + assert options[:receive_timeout] == 15_000 + end + + test "pool_timeout should be overridden by :http, :pool_timeout" do + clear_config([:http, :pool_timeout], 10_000) + options = AdapterHelper.options(%URI{host: 'somewhere'}) + assert options[:pool_timeout] == 10_000 + end + + test "receive_timeout should be overridden by :http, :receive_timeout" do + clear_config([:http, :receive_timeout], 20_000) + options = AdapterHelper.options(%URI{host: 'somewhere'}) + assert options[:receive_timeout] == 20_000 + end + end end -- 2.34.1 From a8f3eecbed38827da69a80433296d51133a41202 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Thu, 24 Nov 2022 12:07:22 +0000 Subject: [PATCH 2/3] Add documentation for HTTP timeouts --- docs/docs/configuration/cheatsheet.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/docs/configuration/cheatsheet.md b/docs/docs/configuration/cheatsheet.md index ee35d95bc..94c32f2a8 100644 --- a/docs/docs/configuration/cheatsheet.md +++ b/docs/docs/configuration/cheatsheet.md @@ -523,6 +523,8 @@ Available caches: ### :http +* `receive_timeout`: the amount of time, in ms, to wait for a remote server to respond to a request. (default: `15000`) +* `pool_timeout`: the amount of time, in ms, to wait to check out an HTTP connection from the pool. This likely does not need changing unless your instance is _very_ busy with outbound requests. (default `5000`) * `proxy_url`: an upstream proxy to fetch posts and/or media with, (default: `nil`); for example `http://127.0.0.1:3192`. Does not support SOCKS5 proxy, only http(s). * `send_user_agent`: should we include a user agent with HTTP requests? (default: `true`) * `user_agent`: what user agent should we use? (default: `:default`), must be string or `:default` -- 2.34.1 From e533b81f7b31b4504878a130a617e2a21e283705 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Thu, 24 Nov 2022 12:09:00 +0000 Subject: [PATCH 3/3] Mix format --- config/description.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/description.exs b/config/description.exs index 7d93e1c0b..fc10cbf81 100644 --- a/config/description.exs +++ b/config/description.exs @@ -2669,7 +2669,8 @@ config :pleroma, :config_description, [ key: :receive_timeout, label: "HTTP Receive Timeout", type: :integer, - description: "Timeout for waiting on remote servers to respond to HTTP requests (in ms, default 15000)", + description: + "Timeout for waiting on remote servers to respond to HTTP requests (in ms, default 15000)", suggestions: [15000] }, %{ -- 2.34.1