Avoid PostgreSQL for empty history projections (#8728)

This commit is contained in:
2026-07-20 07:22:02 -07:00
committed by GitHub
parent 8164e91125
commit 6410ad2eef
2 changed files with 9 additions and 4 deletions
@@ -294,10 +294,14 @@ class PostgresHistory private[service] (
override def sinceFromState(
startCount: Int,
startingState: GameState
): Vector[ActionResultWithResultingState] = withDbLock {
): Vector[ActionResultWithResultingState] = withLock {
val clamped = math.max(0, math.min(startCount, cachedCount))
if clamped != startCount then since(clamped)
else replayFromKnownState(emitFromSeq = clamped, startingState = startingState, endExclusiveSeq = cachedCount)
if clamped == cachedCount then Vector.empty
else
withConnection {
if clamped != startCount then replayFrom(emitFromSeq = clamped, endExclusiveSeq = cachedCount)
else replayFromKnownState(emitFromSeq = clamped, startingState = startingState, endExclusiveSeq = cachedCount)
}
}
override def sinceDate(date: Date): Vector[ActionResultWithResultingState] = withDbLock {
@@ -305,7 +305,7 @@ class PostgresHistorySchemaBatchTest extends AnyFlatSpec with Matchers with Mock
history.shardokPlayerAvailableCommands("battle", 1) shouldBe Some(availableCommand)
}
"PostgresHistory state cache" should "serve an exact hit without opening a database connection" in {
"PostgresHistory state cache" should "serve exact state and empty range hits without opening a database connection" in {
val cachedState = GameState(
gameId = 0x1234L,
currentRoundId = 7,
@@ -338,6 +338,7 @@ class PostgresHistorySchemaBatchTest extends AnyFlatSpec with Matchers with Mock
)
history.stateAfter(1) shouldBe cachedState
history.sinceFromState(1, cachedState) shouldBe empty
}
"PostgresHistory Shardok write-behind" should "publish accepted updates before a blocked database write completes" in {