Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(gateway): close over-budget identify and resume with 4008

Hampusd268383d837b
diff --git a/fluxer_docs/src/content/docs/gateway/opcodes-and-close-codes.md b/fluxer_docs/src/content/docs/gateway/opcodes-and-close-codes.md
index 5a7cd5dd3..c0ebd63f5 100644
--- a/fluxer_docs/src/content/docs/gateway/opcodes-and-close-codes.md
+++ b/fluxer_docs/src/content/docs/gateway/opcodes-and-close-codes.md
@@ -62,7 +62,7 @@ Other refused voice joins get no opcode 12.
| Code | Name | Meaning |
| --- | --- | --- |
-| 4000 | Unknown error | The node is draining, session creation failed, or the session in Resume could not be reached |
+| 4000 | Unknown error | The node is draining, session creation failed, or the session in Resume did not answer |
| 4001 | Unknown opcode | An undefined or Gateway-only opcode, a payload with no `d`, or opcode 28 without [thread access](/gateway/threads/#session-flag) or an object `d` |
| 4002 | Decode error | The payload size, compression, encoding or command fields are invalid |
| 4003 | Not authenticated | A command arrived before Identify or Resume attached a session |
@@ -96,7 +96,7 @@ Every close with a 4000-range code has a reason. Branch on the code and log the
| Reason | Code | Cause |
| --- | --- | --- |
| `Failed to start session` | 4000 | Session creation failed for an unclassified reason |
-| `Session unavailable` | 4000 | The session could not be reached during Resume |
+| `Session unavailable` | 4000 | The session did not answer during Resume |
| `Session drain requested; reconnect to continue` | 4000 | Opcode 7 came just before the close |
| `Encode failed` | 4002 | Hello could not be encoded |
| `Compression failed: zstd-stream` | 4002 | Hello could not be compressed on a zstd connection |
@@ -112,6 +112,8 @@ Every close with a 4000-range code has a reason. Branch on the code and log the
| `Too many connections` | 4008 | The source IP has 256 concurrent connections |
| `Rate limited` | 4008 | The connection, source IP or user payload budget was exceeded |
| `Too many sessions` | 4008 | The user already has 100 live sessions, which doesn't apply to bots |
+| `Identify rate limited` | 4008 | The instance is starting too many sessions at once, so try Identify again after a backoff |
+| `Resume rate limited` | 4008 | The instance is resuming too many sessions at once, so try Resume again after a backoff |
| `Heartbeat timeout` | 4009 | No heartbeat was accepted for 45,000 ms |
The Gateway drops [some over-budget commands](/gateway/commands/), such as Presence Update and Voice Signal, without closing the connection.
diff --git a/fluxer_docs/src/content/docs/gateway/overview.md b/fluxer_docs/src/content/docs/gateway/overview.md
index b02dc77ff..341c40766 100644
--- a/fluxer_docs/src/content/docs/gateway/overview.md
+++ b/fluxer_docs/src/content/docs/gateway/overview.md
@@ -88,6 +88,7 @@ Identify creates a session and the Gateway answers with Ready. These failures cl
| --- | --- | --- |
| 4004 | `Invalid token` | The token is not valid |
| 4008 | `Too many sessions` | The user already has 100 live sessions (bots have no limit) |
+| 4008 | `Identify rate limited` | The instance is starting too many sessions at once, so try again after a backoff |
| 4010 | `Invalid shard` | The `shard` pair is malformed |
| 4011 | `Sharding required` | A bot shard owns more than 2,500 guilds |
| 4000 | `Failed to start session` | The session could not be created for another reason |
@@ -131,7 +132,7 @@ The Gateway replays every Dispatch above `seq` in order and then sends [Resumed]
| The session is unknown or expired, or `seq` is below the replay floor | [Invalid Session](#invalid-session), and the socket can Identify |
| `seq` is above the session sequence or below the acknowledged one | Close with `4007` and reason `Invalid sequence` |
| The token does not own the session | Close with `4004` and reason `Invalid token` |
-| The session cannot be reached | Close with `4000` and reason `Session unavailable` |
+| The session does not answer | Close with `4000` and reason `Session unavailable` |
## Reconnect
diff --git a/fluxer_gateway/src/gateway/gateway_handler_dispatch.erl b/fluxer_gateway/src/gateway/gateway_handler_dispatch.erl
index 62f932cb7..43294512d 100644
--- a/fluxer_gateway/src/gateway/gateway_handler_dispatch.erl
+++ b/fluxer_gateway/src/gateway/gateway_handler_dispatch.erl
@@ -106,8 +106,9 @@ handle_authenticated_opcode(_, _, State) ->
handle_rate_limited_resume(Data, State) ->
case session_abuse_protection:check_identify_rate(peer_ip(State)) of
{error, identify_rate_limited} ->
- logger:debug("Holding gateway resume: reason=rate_limited"),
- {ok, State};
+ gateway_handler_encode:close_with_reason(
+ rate_limited, <<"Resume rate limited">>, State
+ );
ok ->
gateway_handler_identify:handle_resume(Data, State)
end.
diff --git a/fluxer_gateway/src/gateway/gateway_handler_identify.erl b/fluxer_gateway/src/gateway/gateway_handler_identify.erl
index 1cd6d4bea..4ee150e27 100644
--- a/fluxer_gateway/src/gateway/gateway_handler_identify.erl
+++ b/fluxer_gateway/src/gateway/gateway_handler_identify.erl
@@ -27,7 +27,9 @@ handle_identify(Data, PeerIP, State) ->
handle_rate_limited_identify(Data, PeerIP, State) ->
case session_abuse_protection:check_identify_rate(PeerIP) of
{error, identify_rate_limited} ->
- hold_identify(<<"Identify rate limited">>, State);
+ gateway_handler_encode:close_with_reason(
+ rate_limited, <<"Identify rate limited">>, State
+ );
ok ->
handle_identify_validated(Data, PeerIP, State)
end.