mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-10-02 07:18:49 +00:00
gateway: Load latency on reconnection
This commit is contained in:
parent
9d3f579e90
commit
23140acf87
|
@ -354,6 +354,11 @@ func (g *gatewayImpl) OnOp(ctx context.Context, op ws.Op) bool {
|
||||||
g.gateway.QueueReconnect()
|
g.gateway.QueueReconnect()
|
||||||
|
|
||||||
case *HelloEvent:
|
case *HelloEvent:
|
||||||
|
g.heartrate = data.HeartbeatInterval.Duration()
|
||||||
|
g.gateway.ResetHeartbeat(g.heartrate)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
g.beatMutex.Lock()
|
g.beatMutex.Lock()
|
||||||
// Determine that we shouldn't reconnect if the last time we've received
|
// Determine that we shouldn't reconnect if the last time we've received
|
||||||
// a heart beat was over (deadbeatDuration) ago.
|
// a heart beat was over (deadbeatDuration) ago.
|
||||||
|
@ -361,11 +366,11 @@ func (g *gatewayImpl) OnOp(ctx context.Context, op ws.Op) bool {
|
||||||
// Reset gateway times.
|
// Reset gateway times.
|
||||||
g.echoBeat = time.Time{}
|
g.echoBeat = time.Time{}
|
||||||
g.sentBeat = time.Time{}
|
g.sentBeat = time.Time{}
|
||||||
|
// Set the last sent beat time so we can treat sending an Identify or
|
||||||
|
// Resume as sending a heartbeat.
|
||||||
|
g.lastSentBeat = now
|
||||||
g.beatMutex.Unlock()
|
g.beatMutex.Unlock()
|
||||||
|
|
||||||
g.heartrate = data.HeartbeatInterval.Duration()
|
|
||||||
g.gateway.ResetHeartbeat(g.heartrate)
|
|
||||||
|
|
||||||
// Send Discord either the Identify packet (if it's a fresh
|
// Send Discord either the Identify packet (if it's a fresh
|
||||||
// connection), or a Resume packet (if it's a dead connection).
|
// connection), or a Resume packet (if it's a dead connection).
|
||||||
if !resumable || g.state.SessionID == "" || g.state.Sequence == 0 {
|
if !resumable || g.state.SessionID == "" || g.state.Sequence == 0 {
|
||||||
|
@ -410,23 +415,31 @@ func (g *gatewayImpl) OnOp(ctx context.Context, op ws.Op) bool {
|
||||||
g.SendHeartbeat(ctx)
|
g.SendHeartbeat(ctx)
|
||||||
|
|
||||||
case *HeartbeatAckEvent:
|
case *HeartbeatAckEvent:
|
||||||
now := time.Now()
|
g.useLastSentBeat()
|
||||||
|
|
||||||
g.beatMutex.Lock()
|
|
||||||
g.sentBeat = g.lastSentBeat
|
|
||||||
g.echoBeat = now
|
|
||||||
g.beatMutex.Unlock()
|
|
||||||
|
|
||||||
case *ReconnectEvent:
|
case *ReconnectEvent:
|
||||||
g.gateway.QueueReconnect()
|
g.gateway.QueueReconnect()
|
||||||
|
|
||||||
case *ReadyEvent:
|
case *ReadyEvent:
|
||||||
g.state.SessionID = data.SessionID
|
g.state.SessionID = data.SessionID
|
||||||
|
g.useLastSentBeat()
|
||||||
|
|
||||||
|
case *ResumedEvent:
|
||||||
|
g.useLastSentBeat()
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (g *gatewayImpl) useLastSentBeat() {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
g.beatMutex.Lock()
|
||||||
|
g.sentBeat = g.lastSentBeat
|
||||||
|
g.echoBeat = now
|
||||||
|
g.beatMutex.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
func (g *gatewayImpl) isDead() bool {
|
func (g *gatewayImpl) isDead() bool {
|
||||||
if g.heartrate == 0 {
|
if g.heartrate == 0 {
|
||||||
return false
|
return false
|
||||||
|
|
Loading…
Reference in a new issue