fix(gateway): Make heartbeat logging clearer (#3793)

* Make heartbeat logging clearer

* Update packages/gateway/src/Shard.ts

Co-authored-by: LTS20050703 <lts20050703@gmail.com>

---------

Co-authored-by: LTS20050703 <lts20050703@gmail.com>
Co-authored-by: Awesome Stickz <awesome@stickz.dev>
This commit is contained in:
Fleny
2024-07-30 23:36:03 -05:00
committed by GitHub
co-authored by LTS20050703 Awesome Stickz
parent e32c670ab1
commit 658acfb906
+15 -16
View File
@@ -226,7 +226,7 @@ export class DiscordenoShard {
}, },
true, true,
) )
this.logger.debug(`[Gateway] Resuming Shard #${this.id} after send resumg`) this.logger.debug(`[Shard] Resuming Shard #${this.id} after send resume`)
return await new Promise((resolve) => { return await new Promise((resolve) => {
this.resolves.set('RESUMED', () => resolve()) this.resolves.set('RESUMED', () => resolve())
@@ -505,7 +505,8 @@ export class DiscordenoShard {
/** Start sending heartbeat payloads to Discord in the provided interval. */ /** Start sending heartbeat payloads to Discord in the provided interval. */
startHeartbeating(interval: number): void { startHeartbeating(interval: number): void {
this.logger.debug(`[Gateway] Start Heartbeating Shard #${this.id}`) this.logger.debug(`[Shard] Start heartbeating on shard #${this.id}`)
// If old heartbeast exist like after resume, clear the old ones. // If old heartbeast exist like after resume, clear the old ones.
if (this.heart.intervalId) clearInterval(this.heart.intervalId) if (this.heart.intervalId) clearInterval(this.heart.intervalId)
if (this.heart.timeoutId) clearTimeout(this.heart.timeoutId) if (this.heart.timeoutId) clearTimeout(this.heart.timeoutId)
@@ -515,7 +516,7 @@ export class DiscordenoShard {
// Only set the shard's state to `Unidentified` // Only set the shard's state to `Unidentified`
// if heartbeating has not been started due to an identify or resume action. // if heartbeating has not been started due to an identify or resume action.
if ([ShardState.Disconnected, ShardState.Offline].includes(this.state)) { if ([ShardState.Disconnected, ShardState.Offline].includes(this.state)) {
this.logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} a`) this.logger.debug(`[Shard] Shard is disconnected or offline but the heartbeat was started #${this.id}`)
this.state = ShardState.Unidentified this.state = ShardState.Unidentified
} }
@@ -524,11 +525,13 @@ export class DiscordenoShard {
// `Math.random()` can be `0` so we use `0.5` if this happens // `Math.random()` can be `0` so we use `0.5` if this happens
// Reference: https://discord.com/developers/docs/topics/gateway#heartbeating // Reference: https://discord.com/developers/docs/topics/gateway#heartbeating
const jitter = Math.ceil(this.heart.interval * (Math.random() || 0.5)) const jitter = Math.ceil(this.heart.interval * (Math.random() || 0.5))
this.heart.timeoutId = setTimeout(() => { this.heart.timeoutId = setTimeout(() => {
this.logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} b`) this.logger.debug(`[Shard] Beginning heartbeating process for shard #${this.id}`)
if (!this.isOpen()) return if (!this.isOpen()) return
this.logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} c ${this.previousSequenceNumber!}`)
this.logger.debug(`[Shard] Heartbeating on #${this.id}. Previous sequence number: ${this.previousSequenceNumber}`)
// Using a direct socket.send call here because heartbeat requests are reserved by us. // Using a direct socket.send call here because heartbeat requests are reserved by us.
this.socket?.send( this.socket?.send(
@@ -538,33 +541,29 @@ export class DiscordenoShard {
}), }),
) )
this.logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} d`)
this.heart.lastBeat = Date.now() this.heart.lastBeat = Date.now()
this.heart.acknowledged = false this.heart.acknowledged = false
// After the random heartbeat jitter we can start a normal interval. // After the random heartbeat jitter we can start a normal interval.
this.heart.intervalId = setInterval(async () => { this.heart.intervalId = setInterval(async () => {
this.logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} e`) if (!this.isOpen()) {
if (!this.isOpen()) return this.logger.debug(`[Shard] Shard #${this.id} is not open, but attempted heartbeat, ignoring.`)
this.logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} f`) return
// gateway.debug("GW DEBUG", `Running setInterval in heartbeat file. Shard: ${shardId}`); }
// gateway.debug("GW HEARTBEATING", { shardId, shard: currentShard });
// The Shard did not receive a heartbeat ACK from Discord in time, // The Shard did not receive a heartbeat ACK from Discord in time,
// therefore we have to assume that the connection has failed or got "zombied". // therefore we have to assume that the connection has failed or got "zombied".
// The Shard needs to start a re-identify action accordingly. // The Shard needs to start a re-identify action accordingly.
// Reference: https://discord.com/developers/docs/topics/gateway#heartbeating-example-gateway-heartbeat-ack // Reference: https://discord.com/developers/docs/topics/gateway#heartbeating-example-gateway-heartbeat-ack
if (!this.heart.acknowledged) { if (!this.heart.acknowledged) {
this.logger.debug(`[Shard] Heartbeat not acknowledged for shard #${this.id}.`) this.logger.debug(`[Shard] Heartbeat not acknowledged for shard #${this.id}. Assuming zombied connection.`)
this.close(ShardSocketCloseCodes.ZombiedConnection, 'Zombied connection, did not receive an heartbeat ACK in time.') this.close(ShardSocketCloseCodes.ZombiedConnection, 'Zombied connection, did not receive an heartbeat ACK in time.')
return await this.identify() return await this.identify()
} }
this.heart.acknowledged = false this.logger.debug(`[Shard] Heartbeating on #${this.id}. Previous sequence number: ${this.previousSequenceNumber}`)
this.logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} g`)
// Using a direct socket.send call here because heartbeat requests are reserved by us. // Using a direct socket.send call here because heartbeat requests are reserved by us.
this.socket?.send( this.socket?.send(
JSON.stringify({ JSON.stringify({
@@ -572,9 +571,9 @@ export class DiscordenoShard {
d: this.previousSequenceNumber, d: this.previousSequenceNumber,
}), }),
) )
this.logger.debug(`[Gateway] Start Heartbeating Shard #${this.id} h`)
this.heart.lastBeat = Date.now() this.heart.lastBeat = Date.now()
this.heart.acknowledged = false
this.events.heartbeat?.(this) this.events.heartbeat?.(this)
}, this.heart.interval) }, this.heart.interval)