From 9345658ff19c95992940b2ddfa072ffb568f0721 Mon Sep 17 00:00:00 2001 From: The Major Date: Mon, 12 Jun 2023 13:35:47 +0000 Subject: [PATCH 1/3] fix reconnect requests from discord --- lib/nostrum/shard/event.ex | 2 +- lib/nostrum/shard/session.ex | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/nostrum/shard/event.ex b/lib/nostrum/shard/event.ex index 6ad0d61eb..fb82d0a95 100644 --- a/lib/nostrum/shard/event.ex +++ b/lib/nostrum/shard/event.ex @@ -63,7 +63,7 @@ defmodule Nostrum.Shard.Event do def handle(:reconnect, _payload, state) do Logger.info("RECONNECT") - {state, []} + {{state, :reconnect}, []} end def handle(event, _payload, state) do diff --git a/lib/nostrum/shard/session.ex b/lib/nostrum/shard/session.ex index 3e9dfe7cd..bec16b82a 100644 --- a/lib/nostrum/shard/session.ex +++ b/lib/nostrum/shard/session.ex @@ -154,7 +154,20 @@ defmodule Nostrum.Shard.Session do def connecting_http(:enter, from, %{resume_gateway: resume_gateway} = data) when resume_gateway != nil do Logger.debug("Resuming on #{inspect(resume_gateway)}") - connecting_http(:enter, from, %{data | gateway: resume_gateway}) + + # strip off the wss:// prefix if it's there + # since :gun doesn't know how to handle it + resume_url = + case resume_gateway do + <<"wss://", gateway_url::binary>> -> + gateway_url + + gateway_url -> + gateway_url + end + + # if we don't set resume_gateway to nil, we'll have an infinite loop + connecting_http(:enter, from, %{data | gateway: resume_url, resume_gateway: nil}) end def connecting_http(:enter, _from, %{gateway: gateway} = data) do @@ -250,6 +263,13 @@ defmodule Nostrum.Shard.Session do :ok = :gun.update_flow(data_with_seq.conn, stream, @standard_flow) case from_handle do + {updated_data, :reconnect} -> + :gun.close(data_with_seq.conn) + :gun.flush(data_with_seq.conn) + connect = {:next_event, :internal, :connect} + new_data = %{updated_data | conn: nil, stream: nil} + {:next_state, :disconnected, new_data, connect} + {updated_data, reply} -> :ok = :gun.ws_send(data_with_seq.conn, stream, {:binary, reply}) {:keep_state, updated_data, heartbeat_actions} From 62ea348bfe743bd9037a59247abbb1b489e6c14c Mon Sep 17 00:00:00 2001 From: The Major Date: Mon, 12 Jun 2023 13:38:44 +0000 Subject: [PATCH 2/3] run mix format --- lib/nostrum/shard/session.ex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/nostrum/shard/session.ex b/lib/nostrum/shard/session.ex index bec16b82a..518f27eae 100644 --- a/lib/nostrum/shard/session.ex +++ b/lib/nostrum/shard/session.ex @@ -145,9 +145,9 @@ defmodule Nostrum.Shard.Session do {:next_state, :connecting_http, data} end - #def disconnected(_kind, _request, _data) do + # def disconnected(_kind, _request, _data) do # {:keep_state_and_data, :postpone} - #end + # end # If we've been here before, we want to use the resume gateway URL to connect # instead of the regular gateway URL. @@ -196,9 +196,9 @@ defmodule Nostrum.Shard.Session do {:stop, :connect_http_timeout} end - #def connecting_http(_kind, _request, _data) do + # def connecting_http(_kind, _request, _data) do # {:keep_state_and_data, :postpone} - #end + # end def connecting_ws(:enter, _from, %{conn: conn} = data) do Logger.debug("Upgrading connection to websocket") From 046754c701a6d4a041c04bc867b9b5bae273892f Mon Sep 17 00:00:00 2001 From: The Major Date: Mon, 12 Jun 2023 23:37:24 +0000 Subject: [PATCH 3/3] make requested change --- lib/nostrum/shard/session.ex | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/lib/nostrum/shard/session.ex b/lib/nostrum/shard/session.ex index 518f27eae..e7e78378d 100644 --- a/lib/nostrum/shard/session.ex +++ b/lib/nostrum/shard/session.ex @@ -151,23 +151,14 @@ defmodule Nostrum.Shard.Session do # If we've been here before, we want to use the resume gateway URL to connect # instead of the regular gateway URL. - def connecting_http(:enter, from, %{resume_gateway: resume_gateway} = data) - when resume_gateway != nil do + # + # We also have to strip the "wss://" prefix from the URL, as `:gun` doesn't + # currently understand it. + def connecting_http(:enter, from, %{resume_gateway: "wss://" <> resume_gateway} = data) do Logger.debug("Resuming on #{inspect(resume_gateway)}") - # strip off the wss:// prefix if it's there - # since :gun doesn't know how to handle it - resume_url = - case resume_gateway do - <<"wss://", gateway_url::binary>> -> - gateway_url - - gateway_url -> - gateway_url - end - # if we don't set resume_gateway to nil, we'll have an infinite loop - connecting_http(:enter, from, %{data | gateway: resume_url, resume_gateway: nil}) + connecting_http(:enter, from, %{data | gateway: resume_gateway, resume_gateway: nil}) end def connecting_http(:enter, _from, %{gateway: gateway} = data) do