Repository: discordjs/discord.js
File: packages/ws/src/ws/WebSocketShard.ts (TypeScript)

Code before:
		// send more than 5 of those in a 60 second interval. This way we can avoid more complex queueing logic.

		if (ImportantGatewayOpcodes.has(payload.op)) {
			this.connection.send(JSON.stringify(payload));
			return;
		}

		if (this.#status !== WebSocketShardStatus.Ready && !ImportantGatewayOpcodes.has(payload.op)) {
			this.debug(['Tried to send a non-crucial payload before the shard was ready, waiting']);
			// This will throw if the shard throws an error event in the meantime, just requeue the payload
			try {
				await once(this, WebSocketShardEvents.Ready);
			} catch {
				return this.send(payload);
			}
		}

		await this.sendQueue.wait();

		const now = Date.now();
		if (now >= this.sendRateLimitState.resetAt) {
			this.sendRateLimitState = getInitialSendRateLimitState();
		}

		if (this.sendRateLimitState.sent + 1 >= 115) {
			// Sprinkle in a little randomness just incase.
			const sleepFor = this.sendRateLimitState.resetAt - now + Math.random() * 1_500;

			this.debug([`Was about to hit the send rate limit, sleeping for ${sleepFor}ms`]);
			const controller = new AbortController();

			// Sleep for the remaining time, but if the connection closes in the meantime, we shouldn't wait the remainder to avoid blocking the new conn
			const interrupted = await Promise.race([
				sleep(sleepFor).then(() => false),
				once(this, WebSocketShardEvents.Closed, { signal: controller.signal }).then(() => true),
			]);

			if (interrupted) {
				this.debug(['Connection closed while waiting for the send rate limit to reset, re-queueing payload']);
				this.sendQueue.shift();
				return this.send(payload);
			}

			// This is so the listener from the `once` call is removed
			controller.abort();
		}

		this.sendRateLimitState.sent++;

		this.sendQueue.shift();
		this.connection.send(JSON.stringify(payload));

Review comment (suggestion):
```suggestion
			// Sprinkle in a little randomness just in case.
```

Code after:
		// send more than 5 of those in a 60 second interval. This way we can avoid more complex queueing logic.

		if (ImportantGatewayOpcodes.has(payload.op)) {
			this.connection.send(JSON.stringify(payload));
			return;
		}

		if (this.#status !== WebSocketShardStatus.Ready && !ImportantGatewayOpcodes.has(payload.op)) {
			this.debug(['Tried to send a non-crucial payload before the shard was ready, waiting']);
			// This will throw if the shard throws an error event in the meantime, just requeue the payload
			try {
				await once(this, WebSocketShardEvents.Ready);
			} catch {
				return this.send(payload);
			}
		}

		await this.sendQueue.wait();

		const now = Date.now();
		if (now >= this.sendRateLimitState.resetAt) {
			this.sendRateLimitState = getInitialSendRateLimitState();
		}

		if (this.sendRateLimitState.sent + 1 >= 115) {
			// Sprinkle in a little randomness just in case.
			const sleepFor = this.sendRateLimitState.resetAt - now + Math.random() * 1_500;

			this.debug([`Was about to hit the send rate limit, sleeping for ${sleepFor}ms`]);
			const controller = new AbortController();

			// Sleep for the remaining time, but if the connection closes in the meantime, we shouldn't wait the remainder to avoid blocking the new conn
			const interrupted = await Promise.race([
				sleep(sleepFor).then(() => false),
				once(this, WebSocketShardEvents.Closed, { signal: controller.signal }).then(() => true),
			]);

			if (interrupted) {
				this.debug(['Connection closed while waiting for the send rate limit to reset, re-queueing payload']);
				this.sendQueue.shift();
				return this.send(payload);
			}

			// This is so the listener from the `once` call is removed
			controller.abort();
		}

		this.sendRateLimitState.sent++;

		this.sendQueue.shift();
		this.connection.send(JSON.stringify(payload));