-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add player hearbeat processing #21
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
Open
lusu007
wants to merge
3
commits into
main
Choose a base branch
from
feat/player-heartbeat
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package gg.grounds.api | ||
|
|
||
| import gg.grounds.grpc.player.PlayerHeartbeatBatchReply | ||
| import gg.grounds.grpc.player.PlayerHeartbeatBatchRequest | ||
| import gg.grounds.persistence.PlayerSessionRepository | ||
| import jakarta.enterprise.context.ApplicationScoped | ||
| import jakarta.inject.Inject | ||
| import java.time.Instant | ||
| import java.util.UUID | ||
| import org.jboss.logging.Logger | ||
|
|
||
| @ApplicationScoped | ||
| class PlayerHeartbeatService @Inject constructor(private val repository: PlayerSessionRepository) { | ||
| fun handleHeartbeatBatch(request: PlayerHeartbeatBatchRequest): PlayerHeartbeatBatchReply { | ||
| val playerIds = | ||
| parsePlayerIds(request.playerIdsList) | ||
| ?: return PlayerHeartbeatBatchReply.newBuilder() | ||
| .setUpdated(0) | ||
| .setMissing(0) | ||
| .setSuccess(false) | ||
| .setMessage("player_ids must be UUIDs") | ||
| .also { | ||
| LOG.warnf( | ||
| "Player heartbeat batch rejected (count=%d, reason=invalid_player_ids)", | ||
| request.playerIdsList.size, | ||
| ) | ||
| } | ||
| .build() | ||
|
|
||
| if (playerIds.isEmpty()) { | ||
| LOG.debugf("Player heartbeat batch skipped (count=0, reason=empty_request)") | ||
| return PlayerHeartbeatBatchReply.newBuilder() | ||
| .setUpdated(0) | ||
| .setMissing(0) | ||
| .setSuccess(false) | ||
| .setMessage("no player ids provided") | ||
| .build() | ||
| } | ||
|
|
||
| val updated = repository.touchSessions(playerIds, Instant.now()) | ||
| val missing = (playerIds.size - updated).coerceAtLeast(0) | ||
| LOG.debugf( | ||
| "Player heartbeat batch processed (count=%d, updated=%d, missing=%d)", | ||
| playerIds.size, | ||
| updated, | ||
| missing, | ||
| ) | ||
| return PlayerHeartbeatBatchReply.newBuilder() | ||
| .setUpdated(updated) | ||
| .setMissing(missing) | ||
| .setSuccess(true) | ||
| .setMessage("heartbeat accepted") | ||
| .build() | ||
| } | ||
|
|
||
| private fun parsePlayerIds(values: List<String>): List<UUID>? { | ||
| if (values.isEmpty()) { | ||
| return emptyList() | ||
| } | ||
| val trimmed = values.map { it.trim() } | ||
| if (trimmed.any { it.isEmpty() }) { | ||
| return null | ||
| } | ||
| val parsed = trimmed.map { runCatching { UUID.fromString(it) }.getOrNull() } | ||
| return parsed.takeIf { parsedIds -> parsedIds.none { it == null } }?.filterNotNull() | ||
| } | ||
|
|
||
| companion object { | ||
| private val LOG = Logger.getLogger(PlayerHeartbeatService::class.java) | ||
| } | ||
| } | ||
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
32 changes: 32 additions & 0 deletions
32
src/main/kotlin/gg/grounds/presence/PlayerSessionCleanup.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,32 @@ | ||
| package gg.grounds.presence | ||
|
|
||
| import gg.grounds.persistence.PlayerSessionRepository | ||
| import gg.grounds.time.TimeProvider | ||
| import io.quarkus.scheduler.Scheduled | ||
| import jakarta.enterprise.context.ApplicationScoped | ||
| import jakarta.inject.Inject | ||
| import java.time.Duration | ||
| import org.eclipse.microprofile.config.inject.ConfigProperty | ||
| import org.jboss.logging.Logger | ||
|
|
||
| @ApplicationScoped | ||
| class PlayerSessionCleanup | ||
| @Inject | ||
| constructor( | ||
| private val repository: PlayerSessionRepository, | ||
| private val timeProvider: TimeProvider, | ||
| ) { | ||
| @ConfigProperty(name = "grounds.player.sessions.ttl", defaultValue = "90s") | ||
| private lateinit var sessionTtl: Duration | ||
|
|
||
| @Scheduled(every = "{grounds.player.sessions.cleanup-interval}") | ||
| fun expireStaleSessions() { | ||
| val cutoff = timeProvider.now().minus(sessionTtl) | ||
| val removed = repository.deleteStaleSessions(cutoff) | ||
| LOG.infof("Player session cleanup completed (removed=%d, cutoff=%s)", removed, cutoff) | ||
| } | ||
|
|
||
| companion object { | ||
| private val LOG = Logger.getLogger(PlayerSessionCleanup::class.java) | ||
| } | ||
| } |
Oops, something went wrong.
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.