Skip to content

Commit f584b23

Browse files
committed
Add ResumeSynchronizationEvent
NB: This is potentially breaking as it replaces a `sealed` attribute
1 parent ad9a687 commit f584b23

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkNode.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dev.arbjerg.lavalink.client
22

33
import dev.arbjerg.lavalink.client.event.ClientEvent
4+
import dev.arbjerg.lavalink.client.event.ResumeSynchronizationEvent
45
import dev.arbjerg.lavalink.client.http.HttpBuilder
56
import dev.arbjerg.lavalink.client.player.*
67
import dev.arbjerg.lavalink.client.player.Track
@@ -17,12 +18,14 @@ import kotlinx.serialization.serializer
1718
import okhttp3.Call
1819
import okhttp3.OkHttpClient
1920
import okhttp3.Response
21+
import org.slf4j.LoggerFactory
2022
import reactor.core.Disposable
2123
import reactor.core.publisher.Flux
2224
import reactor.core.publisher.Mono
2325
import reactor.core.publisher.Sinks
2426
import reactor.core.publisher.Sinks.Many
2527
import reactor.kotlin.core.publisher.toMono
28+
import reactor.util.retry.Retry
2629
import java.io.Closeable
2730
import java.io.IOException
2831
import java.time.Duration
@@ -38,6 +41,7 @@ class LavalinkNode(
3841
private val nodeOptions: NodeOptions,
3942
val lavalink: LavalinkClient
4043
) : Disposable, Closeable {
44+
private val logger = LoggerFactory.getLogger(LavalinkNode::class.java)
4145
// "safe" uri with all paths removed
4246
val baseUri = "${nodeOptions.serverUri.scheme}://${nodeOptions.serverUri.host}:${nodeOptions.serverUri.port}"
4347

@@ -238,7 +242,7 @@ class LavalinkNode(
238242
}
239243

240244
/**
241-
* Enables resuming. This causes Lavalink to continue playing for [duration], during which
245+
* Enables resuming. This causes Lavalink to continue playing for [timeout] amount of time, during which
242246
* we can reconnect without losing our session data. */
243247
fun enableResuming(timeout: Duration): Mono<Session> {
244248
return rest.patchSession(Session(resuming = true, timeout.seconds)).doOnSuccess {
@@ -428,7 +432,9 @@ class LavalinkNode(
428432
}
429433

430434
internal fun synchronizeAfterResume() {
431-
getPlayers().subscribe { players ->
435+
getPlayers()
436+
.retryWhen(Retry.fixedDelay(3, Duration.ofSeconds(1)))
437+
.map { players ->
432438
val remoteGuildIds = players.map { it.guildId }
433439

434440
players.forEach { player ->
@@ -450,6 +456,13 @@ class LavalinkNode(
450456
val link = lavalink.getLinkIfCached(guildId) ?: return@forEach
451457
if (link.node == this) link.state = LinkState.DISCONNECTED
452458
}
459+
460+
ResumeSynchronizationEvent(this, failureReason = null)
461+
}.doOnError {
462+
logger.error("Failure while attempting synchronization with $this", it)
463+
sink.tryEmitNext(ResumeSynchronizationEvent(this, failureReason = it))
464+
}.subscribe {
465+
sink.tryEmitNext(it)
453466
}
454467
}
455468

src/main/kotlin/dev/arbjerg/lavalink/client/event/events.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import dev.arbjerg.lavalink.client.player.toCustom
77
import dev.arbjerg.lavalink.protocol.v4.*
88
import dev.arbjerg.lavalink.protocol.v4.Message.EmittedEvent.TrackEndEvent.AudioTrackEndReason
99

10-
internal fun Message.toClientEvent(node: LavalinkNode) = when (this) {
10+
internal fun Message.toClientEvent(node: LavalinkNode): ClientEvent = when (this) {
1111
is Message.ReadyEvent -> ReadyEvent(node, resumed, sessionId)
1212
is Message.EmittedEvent.TrackEndEvent -> TrackEndEvent(node, guildId.toLong(), track.toCustom(), reason)
1313
is Message.EmittedEvent.TrackExceptionEvent -> TrackExceptionEvent(node, guildId.toLong(), track.toCustom(), exception.toCustom())
@@ -18,12 +18,19 @@ internal fun Message.toClientEvent(node: LavalinkNode) = when (this) {
1818
is Message.StatsEvent -> StatsEvent(node, frameStats, players, playingPlayers, uptime, memory, cpu)
1919
}
2020

21-
sealed class ClientEvent(open val node: LavalinkNode)
21+
abstract class ClientEvent(open val node: LavalinkNode)
2222

2323
// Normal events
2424
data class ReadyEvent(override val node: LavalinkNode, val resumed: Boolean, val sessionId: String)
2525
: ClientEvent(node)
2626

27+
/**
28+
* Represents a successful or failed synchronization after a [ReadyEvent] with [ReadyEvent.resumed] set to true.
29+
*
30+
* Whether it is successful depends on whether [failureReason] is null.
31+
*/
32+
data class ResumeSynchronizationEvent(override val node: LavalinkNode, val failureReason: Throwable?) : ClientEvent(node)
33+
2734
data class PlayerUpdateEvent(override val node: LavalinkNode, val guildId: Long, val state: PlayerState)
2835
: ClientEvent(node)
2936

0 commit comments

Comments
 (0)