Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add flag to disable HTTP 2 #608

Merged
merged 2 commits into from
Jun 20, 2024
Merged
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
3 changes: 3 additions & 0 deletions guides/intro/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 21 additions & 11 deletions lib/nostrum/api/ratelimiter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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),
Expand Down