Skip to content

Commit

Permalink
Merge pull request #608 from Kraigie/jb3/gun-force-http-1
Browse files Browse the repository at this point in the history
Add flag to disable HTTP 2
  • Loading branch information
jchristgit authored Jun 20, 2024
2 parents ace905f + eab8351 commit 0142400
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
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

0 comments on commit 0142400

Please # to comment.