vsTerminus / Mojo-Discord

Perl Modules that implement parts of the Discord API. Intended for Text Chat Bots.
MIT License
33 stars 10 forks source link

Skip reconnect delay on Resume #36

Closed incognico closed 3 years ago

incognico commented 3 years ago

When the connection gets closed with reason Resume, the reconnect delay must be skipped - we must reconnect asap.

Docs:

It's also possible that your client cannot reconnect in time to resume, in which case the client will receive a Opcode 9 Invalid Session and is expected to wait a random amount of time—between 1 and 5 seconds—then send a fresh Opcode 2 Identify

This is exactly what is happening currently, we get closed with reason Resume, the reconnect timer eats up the time window, we resume but the session is invalid already.

[2021-03-06 10:48:27.69587] [2439] [info] [Gateway.pm] [gw_disconnect] Closing websocket with reason: Resume.
[2021-03-06 10:48:27.69613] [2439] [info] [Gateway.pm] [on_finish] Websocket connection closed with Code $code = 1005;
 (No Status Received)
[2021-03-06 10:48:27.69617] [2439] [debug] [Gateway.pm] [on_finish] Removing heartbeat timer
[2021-03-06 10:48:27.69622] [2439] [debug] [Gateway.pm] [reconnect] Automatic reconnect is enabled.
[2021-03-06 10:48:27.69624] [2439] [info] [Gateway.pm] [reconnect] Reconnecting and resuming previous session.
[2021-03-06 10:48:27.69630] [2439] [debug] [Gateway.pm] [reconnect] Reconnect timer increased to 6 seconds
[2021-03-06 10:48:31.69738] [2439] [debug] [Gateway.pm] [gw_connect] Resume? 1
[2021-03-06 10:48:31.73180] [2439] [debug] [Gateway.pm] [gateway] Gateway URL: wss://gateway.discord.gg
[2021-03-06 10:48:31.73193] [2439] [debug] [Gateway.pm] [gw_connect] Connecting to wss://gateway.discord.gg?v=8&encoding=json
[2021-03-06 10:48:31.88082] [2439] [info] [Gateway.pm] [gw_connect] WebSocket Connection Established
[2021-03-06 10:48:31.88094] [2439] [debug] [Gateway.pm] [send_resume] Sending OP $op SEQ $s RESUME
[2021-03-06 10:48:31.88113] [2439] [debug] [Gateway.pm] [send_op] Sent: {"d":{"seq":"3931","session_id":"0787980b8c2017f4e9f91e0d666524d3","token":"MzkzM**********jgK4s"},"op":6}
[2021-03-06 10:48:37.42269] [2439] [info] [Gateway.pm] [gw_disconnect] Closing websocket with reason: Invalid Session.
[2021-03-06 10:48:37.42297] [2439] [info] [Gateway.pm] [on_finish] Websocket connection closed with Code $code = 1005;
 (No Status Received)
[...reconnect with new session follows...]
vsTerminus commented 3 years ago

Fixed locally, will push once I validate the logic is working.