Skip to content

Commit d92e73f

Browse files
committed
Add tracking of Session state on update/connect
1 parent 5389d6b commit d92e73f

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class LavalinkNode(
4444
val name = nodeOptions.name
4545
val regionFilter = nodeOptions.regionFilter
4646
val password = nodeOptions.password
47+
internal var cachedSession: Session? = null
4748

4849
var sessionId: String? = nodeOptions.sessionId
4950
internal set
@@ -239,7 +240,9 @@ class LavalinkNode(
239240
* Enables resuming. This causes Lavalink to continue playing for [duration], during which
240241
* we can reconnect without losing our session data. */
241242
fun enableResuming(timeout: Duration): Mono<Session> {
242-
return rest.patchSession(Session(resuming = true, timeout.seconds))
243+
return rest.patchSession(Session(resuming = true, timeout.seconds)).doOnSuccess {
244+
cachedSession = it
245+
}
243246
}
244247

245248
/**
@@ -248,7 +251,9 @@ class LavalinkNode(
248251
* This is the default behavior, reversing calls to [enableResuming].
249252
*/
250253
fun disableResuming(): Mono<Session> {
251-
return rest.patchSession(Session(resuming = false, timeoutSeconds = 0))
254+
return rest.patchSession(Session(resuming = false, timeoutSeconds = 0)).doOnSuccess {
255+
cachedSession = it
256+
}
252257
}
253258

254259
/**

src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkRestClient.kt

+6
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ class LavalinkRestClient(val node: LavalinkNode) {
7676
}.toMono()
7777
}
7878

79+
fun getSession(): Mono<Session> {
80+
return newRequest {
81+
path("/v4/sessions/${node.sessionId}")
82+
}.toMono()
83+
}
84+
7985
/**
8086
* Make a request to the lavalink node. This is internal to keep it looking nice in kotlin. Java compatibility is in the node class.
8187
*/

src/main/kotlin/dev/arbjerg/lavalink/internal/LavalinkSocket.kt

+7
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ class LavalinkSocket(private val node: LavalinkNode) : WebSocketListener(), Clos
7575
.subscribe()
7676
}
7777

78+
if (!resumed) {
79+
node.cachedSession = null
80+
}
81+
if (node.cachedSession == null) {
82+
node.rest.getSession().subscribe { node.cachedSession = null }
83+
}
84+
7885
// Move players from older, unavailable nodes to ourselves.
7986
node.transferOrphansToSelf()
8087
}

0 commit comments

Comments
 (0)