Skip to content

Commit

Permalink
voice: fix race condition where heartbeat is sent before authenticati…
Browse files Browse the repository at this point in the history
…on (#511)
  • Loading branch information
lost-illusi0n authored Jan 30, 2022
1 parent 6e7238b commit 824c593
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion voice/src/main/kotlin/gateway/handler/HeartbeatHandler.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.kord.voice.gateway.handler

import dev.kord.voice.gateway.*
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.ObsoleteCoroutinesApi
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
Expand All @@ -18,11 +19,16 @@ internal class HeartbeatHandler(
private val timeSource: TimeSource = TimeSource.Monotonic
) : GatewayEventHandler(flow, "HeartbeatHandler") {
private var timestamp: TimeMark = timeSource.markNow()
private var interval by atomic(0L)

override suspend fun start() = coroutineScope {
on<Hello> {
interval = it.heartbeatInterval.toLong()
}

on<Ready> {
launch {
ticker.tickAt(it.heartbeatInterval.toLong()) {
ticker.tickAt(interval) {
timestamp = timeSource.markNow()
send(Heartbeat(timestamp.elapsedNow().inWholeMilliseconds))
}
Expand Down

0 comments on commit 824c593

Please sign in to comment.