Skip to content

Commit

Permalink
Use getOrPut() to resolve race conditon with Links
Browse files Browse the repository at this point in the history
  • Loading branch information
freyacodes committed Mar 14, 2024
1 parent 11c4a59 commit f94a045
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/main/kotlin/dev/arbjerg/lavalink/client/LavalinkClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,9 @@ class LavalinkClient(val userId: Long) : Closeable, Disposable {
*/
@JvmOverloads
fun getOrCreateLink(guildId: Long, region: VoiceRegion? = null): Link {
if (!linkMap.containsKey(guildId)) {
val bestNode = loadBalancer.selectNode(region, guildId)
linkMap[guildId] = Link(guildId, bestNode)
return linkMap.getOrPut(guildId) {
Link(guildId, loadBalancer.selectNode(region, guildId))
}

return linkMap[guildId]!!
}

/**
Expand Down

0 comments on commit f94a045

Please sign in to comment.