Skip to content

Commit

Permalink
Only attempt resume if the gateway says we can
Browse files Browse the repository at this point in the history
Per [1] and [2], if the `d` field is set to `true` we may attempt to
resume again with the same session, otherwise we should not. nostrum
would previously loop on trying to resume on the same session after an
invalid session was sent, and the behaviour of simply sending an
identify payload did not work as expected. When the session that we try
to resume with is invalid, perform a reconnect and do the whole
handshake, greeting and meeting the parents thing all over again.

[1]: https://discord.com/developers/docs/topics/gateway-events#invalid-session
[2]: https://discord.com/developers/docs/topics/gateway#resuming
  • Loading branch information
jchristgit committed May 4, 2024
1 parent c34a563 commit e303f2b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/nostrum/shard/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ defmodule Nostrum.Shard.Event do
end
end

def handle(:invalid_session, _payload, state) do
Logger.info("INVALID_SESSION")
{{state, Payload.identity_payload(state)}, []}
def handle(:invalid_session, %{d: _can_resume? = true}, state) do
Logger.info("Invalid but resumable session. Attempting to resume at #{state.session}")
{{state, Payload.resume_payload(state)}, []}
end

def handle(:invalid_session, %{d: _can_resume? = false}, state) do
Logger.info("Invalid and un-resumable session at #{state.session}. Events may be lost.")
{{state, :reconnect}, []}
end

def handle(:reconnect, _payload, state) do
Expand Down

0 comments on commit e303f2b

Please # to comment.