diff --git a/src/main/kotlin/eventDemo/business/command/GameCommandHandler.kt b/src/main/kotlin/eventDemo/business/command/GameCommandHandler.kt index d7c06ac..0f858a4 100644 --- a/src/main/kotlin/eventDemo/business/command/GameCommandHandler.kt +++ b/src/main/kotlin/eventDemo/business/command/GameCommandHandler.kt @@ -15,8 +15,12 @@ import io.github.oshai.kotlinlogging.KotlinLogging import io.github.oshai.kotlinlogging.withLoggingContext import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.channels.SendChannel +import kotlinx.datetime.Clock +import kotlinx.datetime.Instant import java.util.UUID import java.util.concurrent.ConcurrentHashMap +import kotlin.time.Duration +import kotlin.time.Duration.Companion.minutes /** * Listen [GameCommand] on [CommandStreamChannel], check the validity and execute an action. @@ -112,7 +116,9 @@ private fun SendChannel.sendError(command: GameCommand): suspend ( /** * Map to record the command that triggered the event. */ -private class EventCommandMap { +private class EventCommandMap( + val retention: Duration = 10.minutes, +) { val map = ConcurrentHashMap() fun set( @@ -120,7 +126,12 @@ private class EventCommandMap { channel: SendChannel, commandId: CommandId, ) { - map[eventId] = Output(channel, commandId) + map[eventId] = Output(channel, commandId, Clock.System.now()) + + map + .filterValues { it.date < (Clock.System.now() - retention) } + .keys + .forEach(map::remove) } operator fun get(eventId: UUID): Output? = @@ -129,5 +140,6 @@ private class EventCommandMap { data class Output( val channel: SendChannel, val commandId: CommandId, + val date: Instant, ) }