-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add player hearbeats #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2f0ba46
feat: add player hearbeats
lusu007 468fdc6
fix: implement proper success handling
lusu007 fd77406
fix: implement a safeguard to ensure only one hearbeat scheduler is rβ¦
lusu007 945e067
feat: clamp heartbeat interval to session TTL and enrich heartbeat reβ¦
lusu007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
velocity/src/main/kotlin/gg/grounds/presence/PlayerHeartbeatScheduler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package gg.grounds.presence | ||
|
|
||
| import com.velocitypowered.api.proxy.ProxyServer | ||
| import com.velocitypowered.api.scheduler.ScheduledTask | ||
| import java.util.concurrent.TimeUnit | ||
| import kotlin.math.max | ||
| import kotlin.math.min | ||
| import org.slf4j.Logger | ||
|
|
||
| class PlayerHeartbeatScheduler( | ||
| private val plugin: Any, | ||
| private val proxy: ProxyServer, | ||
| private val logger: Logger, | ||
| private val presenceService: PlayerPresenceService, | ||
| ) { | ||
| private var heartbeatTask: ScheduledTask? = null | ||
|
|
||
| fun start() { | ||
| heartbeatTask?.cancel() | ||
| heartbeatTask = null | ||
| val intervalResolution = | ||
| resolveHeartbeatIntervalSeconds( | ||
| System.getenv(HEARTBEAT_INTERVAL_ENV), | ||
| System.getenv(SESSION_TTL_ENV), | ||
| ) | ||
| heartbeatTask = | ||
| proxy.scheduler | ||
| .buildTask(plugin, Runnable { sendHeartbeats() }) | ||
| .repeat(intervalResolution.effectiveIntervalSeconds, TimeUnit.SECONDS) | ||
| .schedule() | ||
| if (intervalResolution.wasClamped) { | ||
| logger.warn( | ||
| "Player heartbeat interval clamped (configuredIntervalSeconds={}, effectiveIntervalSeconds={}, sessionTtlSeconds={}, maxIntervalSeconds={})", | ||
| intervalResolution.configuredIntervalSeconds, | ||
| intervalResolution.effectiveIntervalSeconds, | ||
| intervalResolution.sessionTtlSeconds, | ||
| intervalResolution.maxIntervalSeconds, | ||
| ) | ||
| } | ||
| logger.info( | ||
| "Configured player presence heartbeat task (intervalSeconds={}, sessionTtlSeconds={})", | ||
| intervalResolution.effectiveIntervalSeconds, | ||
| intervalResolution.sessionTtlSeconds, | ||
| ) | ||
| } | ||
|
|
||
| fun stop() { | ||
| heartbeatTask?.cancel() | ||
| heartbeatTask = null | ||
| } | ||
|
|
||
| private fun sendHeartbeats() { | ||
| val playerIds = proxy.allPlayers.map { it.uniqueId } | ||
| if (playerIds.isEmpty()) { | ||
| return | ||
| } | ||
|
|
||
| val result = presenceService.heartbeatBatch(playerIds) | ||
| if (!result.success) { | ||
| logger.error( | ||
| "Player session heartbeat batch failed (playerCount={}, updated={}, missing={}, reason={})", | ||
| playerIds.size, | ||
| result.updated, | ||
| result.missing, | ||
| result.message, | ||
| ) | ||
| } else if (result.missing > 0) { | ||
| logger.warn( | ||
| "Player session heartbeat batch completed with missing sessions (playerCount={}, updated={}, missing={})", | ||
| playerIds.size, | ||
| result.updated, | ||
| result.missing, | ||
| ) | ||
| } else { | ||
| logger.debug( | ||
| "Player session heartbeat batch completed (playerCount={}, updated={}, missing={}, result=success)", | ||
| playerIds.size, | ||
| result.updated, | ||
| result.missing, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| internal data class HeartbeatIntervalResolution( | ||
| val configuredIntervalSeconds: Long, | ||
| val effectiveIntervalSeconds: Long, | ||
| val sessionTtlSeconds: Long, | ||
| val maxIntervalSeconds: Long, | ||
| val wasClamped: Boolean, | ||
| ) | ||
|
|
||
| companion object { | ||
| private const val DEFAULT_HEARTBEAT_INTERVAL_SECONDS = 30L | ||
| private const val DEFAULT_SESSION_TTL_SECONDS = 90L | ||
| private const val HEARTBEAT_INTERVAL_ENV = "PLAYER_PRESENCE_HEARTBEAT_SECONDS" | ||
| private const val SESSION_TTL_ENV = "PLAYER_SESSIONS_TTL" | ||
|
|
||
| internal fun resolveHeartbeatIntervalSeconds( | ||
| heartbeatIntervalRaw: String?, | ||
| sessionTtlRaw: String?, | ||
| ): HeartbeatIntervalResolution { | ||
| val configuredInterval = | ||
| heartbeatIntervalRaw?.trim()?.toLongOrNull()?.takeIf { it > 0 } | ||
| ?: DEFAULT_HEARTBEAT_INTERVAL_SECONDS | ||
| val sessionTtlSeconds = | ||
| parseSessionTtlSeconds(sessionTtlRaw) ?: DEFAULT_SESSION_TTL_SECONDS | ||
| val maxIntervalSeconds = max(1L, sessionTtlSeconds / 3) | ||
| val effectiveInterval = min(configuredInterval, maxIntervalSeconds) | ||
| return HeartbeatIntervalResolution( | ||
| configuredIntervalSeconds = configuredInterval, | ||
| effectiveIntervalSeconds = effectiveInterval, | ||
| sessionTtlSeconds = sessionTtlSeconds, | ||
| maxIntervalSeconds = maxIntervalSeconds, | ||
| wasClamped = effectiveInterval != configuredInterval, | ||
| ) | ||
| } | ||
|
|
||
| private fun parseSessionTtlSeconds(raw: String?): Long? { | ||
| val value = raw?.trim()?.lowercase() ?: return null | ||
| if (value.isEmpty()) { | ||
| return null | ||
| } | ||
| return when { | ||
| value.endsWith("s") -> value.removeSuffix("s").toLongOrNull() | ||
| value.endsWith("m") -> value.removeSuffix("m").toLongOrNull()?.times(60) | ||
| value.endsWith("h") -> value.removeSuffix("h").toLongOrNull()?.times(3600) | ||
| else -> null | ||
| }?.takeIf { it > 0 } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
velocity/src/test/kotlin/gg/grounds/presence/PlayerHeartbeatSchedulerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package gg.grounds.presence | ||
|
|
||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertFalse | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| class PlayerHeartbeatSchedulerTest { | ||
| @Test | ||
| fun resolveHeartbeatIntervalUsesDefaultsWhenUnset() { | ||
| val result = PlayerHeartbeatScheduler.resolveHeartbeatIntervalSeconds(null, null) | ||
|
|
||
| assertEquals(30, result.configuredIntervalSeconds) | ||
| assertEquals(30, result.effectiveIntervalSeconds) | ||
| assertEquals(90, result.sessionTtlSeconds) | ||
| assertEquals(30, result.maxIntervalSeconds) | ||
| assertFalse(result.wasClamped) | ||
| } | ||
|
|
||
| @Test | ||
| fun resolveHeartbeatIntervalClampsWhenAboveSafeMaximum() { | ||
| val result = PlayerHeartbeatScheduler.resolveHeartbeatIntervalSeconds("60", "90s") | ||
|
|
||
| assertEquals(60, result.configuredIntervalSeconds) | ||
| assertEquals(30, result.effectiveIntervalSeconds) | ||
| assertEquals(90, result.sessionTtlSeconds) | ||
| assertEquals(30, result.maxIntervalSeconds) | ||
| assertTrue(result.wasClamped) | ||
| } | ||
|
|
||
| @Test | ||
| fun resolveHeartbeatIntervalDoesNotClampWhenWithinSafeMaximum() { | ||
| val result = PlayerHeartbeatScheduler.resolveHeartbeatIntervalSeconds("20", "90s") | ||
|
|
||
| assertEquals(20, result.configuredIntervalSeconds) | ||
| assertEquals(20, result.effectiveIntervalSeconds) | ||
| assertEquals(90, result.sessionTtlSeconds) | ||
| assertEquals(30, result.maxIntervalSeconds) | ||
| assertFalse(result.wasClamped) | ||
| } | ||
|
|
||
| @Test | ||
| fun resolveHeartbeatIntervalParsesMinuteTtl() { | ||
| val result = PlayerHeartbeatScheduler.resolveHeartbeatIntervalSeconds("90", "6m") | ||
|
|
||
| assertEquals(90, result.configuredIntervalSeconds) | ||
| assertEquals(90, result.effectiveIntervalSeconds) | ||
| assertEquals(360, result.sessionTtlSeconds) | ||
| assertEquals(120, result.maxIntervalSeconds) | ||
| assertFalse(result.wasClamped) | ||
| } | ||
|
|
||
| @Test | ||
| fun resolveHeartbeatIntervalFallsBackForInvalidValues() { | ||
| val result = PlayerHeartbeatScheduler.resolveHeartbeatIntervalSeconds("-5", "oops") | ||
|
|
||
| assertEquals(30, result.configuredIntervalSeconds) | ||
| assertEquals(30, result.effectiveIntervalSeconds) | ||
| assertEquals(90, result.sessionTtlSeconds) | ||
| assertEquals(30, result.maxIntervalSeconds) | ||
| assertFalse(result.wasClamped) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.