Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
switch to softValues for ClientState store, remove hard client state …
Browse files Browse the repository at this point in the history
…count limit
  • Loading branch information
sanity committed May 26, 2023
1 parent 679c7ad commit 191eedf
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 11 deletions.
2 changes: 0 additions & 2 deletions api/kweb-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,6 @@ public abstract class kweb/config/KwebConfiguration {
public fun <init> ()V
public abstract fun getBuildpageTimeout ()Ljava/time/Duration;
public fun getClientOfflineToastTextTemplate ()Ljava/lang/String;
public abstract fun getClientStateMaxSize ()J
public abstract fun getClientStateStatsEnabled ()Z
public abstract fun getClientStateTimeout ()Ljava/time/Duration;
public fun getHandleFavicon ()Z
Expand All @@ -1023,7 +1022,6 @@ protected final class kweb/config/KwebConfiguration$Accessor {
public class kweb/config/KwebDefaultConfiguration : kweb/config/KwebConfiguration {
public fun <init> ()V
public fun getBuildpageTimeout ()Ljava/time/Duration;
public fun getClientStateMaxSize ()J
public fun getClientStateStatsEnabled ()Z
public fun getClientStateTimeout ()Ljava/time/Duration;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/kweb/Kweb.kt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ class Kweb private constructor(
*/
val clientState: Cache<String, RemoteClientState> = CacheBuilder.newBuilder()
.expireAfterAccess(kwebConfig.clientStateTimeout)
.maximumSize(kwebConfig.clientStateMaxSize)
// Use soft values so older cached states will be deleted if we're running out of memory.
// Has some disadvantages - like unpredictable behavior, but better than crashing due to OOM.
.softValues()
.apply { if (kwebConfig.clientStateStatsEnabled) recordStats() }
.removalListener<String, RemoteClientState> { rl ->
rl.value?.triggerCloseListeners()
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/kweb/config/KwebConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import java.util.*
*/
abstract class KwebConfiguration {

abstract val clientStateMaxSize: Long

private val logger = KotlinLogging.logger {}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/main/kotlin/kweb/config/KwebDefaultConfiguration.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,5 @@ open class KwebDefaultConfiguration : KwebConfiguration() {

override val clientStateTimeout: Duration =
Accessor.getProperty("kweb.client.state.timeout")?.let { Duration.parse(it) }
?: Duration.ofMinutes(5)

private val estimatedMemoryPerClient = 102_400L // 100KB
override val clientStateMaxSize: Long =
Accessor.getProperty("kweb.client.state.max.size")?.toLongOrNull()
?: (Runtime.getRuntime().maxMemory() / estimatedMemoryPerClient)
?: Duration.ofHours(1)
}

0 comments on commit 191eedf

Please sign in to comment.