From b9ed6fe67c1700fa762df938cd783f0b17edf622 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Thu, 20 Jun 2024 18:17:51 +0100 Subject: [PATCH 1/2] Add new config flag to disable HTTP 2 --- lib/nostrum/api/ratelimiter.ex | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/nostrum/api/ratelimiter.ex b/lib/nostrum/api/ratelimiter.ex index 826d4e86b..f9e9ca0c5 100644 --- a/lib/nostrum/api/ratelimiter.ex +++ b/lib/nostrum/api/ratelimiter.ex @@ -366,17 +366,7 @@ defmodule Nostrum.Api.Ratelimiter do def connecting(:internal, :open, data) do domain = to_charlist(Constants.domain()) - open_opts = %{ - connect_timeout: :timer.seconds(5), - domain_lookup_timeout: :timer.seconds(5), - # Do not retry here. If we retry, it is possible that after the state - # machine heads into disconnected state, it receives an unexpected - # `:gun_up` message. We want the state machine to manage the connection - # lifecycle fully on its own. - retry: 0, - tls_handshake_timeout: :timer.seconds(5), - tls_opts: Constants.gun_tls_opts() - } + open_opts = get_open_opts() {:ok, conn_pid} = :gun.open(domain, 443, open_opts) {:keep_state, %{data | conn: conn_pid}} @@ -945,6 +935,26 @@ defmodule Nostrum.Api.Ratelimiter do # End of callback functions + defp get_open_opts do + default_opts = %{ + connect_timeout: :timer.seconds(5), + domain_lookup_timeout: :timer.seconds(5), + # Do not retry here. If we retry, it is possible that after the state + # machine heads into disconnected state, it receives an unexpected + # `:gun_up` message. We want the state machine to manage the connection + # lifecycle fully on its own. + retry: 0, + tls_handshake_timeout: :timer.seconds(5), + tls_opts: Constants.gun_tls_opts() + } + + if Application.get_env(:nostrum, :force_http1, false) do + Map.put(default_opts, :protocols, [:http]) + else + default_opts + end + end + defp parse_response(status, headers), do: {:ok, {status, headers, ""}} defp parse_response(status, headers, buffer), From eab83514e6f5fb8f08b23061e049b1abc3451f30 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Thu, 20 Jun 2024 18:17:59 +0100 Subject: [PATCH 2/2] Document new `force_http1` flag in Intro guide --- guides/intro/intro.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/guides/intro/intro.md b/guides/intro/intro.md index 6c53e8b01..35d53b33c 100644 --- a/guides/intro/intro.md +++ b/guides/intro/intro.md @@ -98,6 +98,9 @@ Apart from the `token` field mentioned above, the following fields are also supp variables or by [setting the system flag yourself](http://erlang.org/doc/man/erlang.html#system_flag-2). Defaults to whatever your system recommends, which is probably `65535`. +- `force_http1` - Set to `true` if you wish to disable automatic use of HTTP 2 + or newer HTTP versions for API requests to Discord. Useful to diagnose issues + with ratelimiter connections during abnormal network conditions. ### Internal options