Skip to content

Commit

Permalink
fix: load from storage async in init (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori authored Aug 19, 2024
1 parent cb1699c commit 827ce6b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.amplitude.experiment.evaluation.EvaluationEngineImpl
import com.amplitude.experiment.evaluation.EvaluationFlag
import com.amplitude.experiment.evaluation.json
import com.amplitude.experiment.evaluation.topologicalSort
import com.amplitude.experiment.storage.LoadStoreCache
import com.amplitude.experiment.storage.Storage
import com.amplitude.experiment.storage.getFlagStorage
import com.amplitude.experiment.storage.getVariantStorage
Expand Down Expand Up @@ -59,23 +58,31 @@ internal class DefaultExperimentClient internal constructor(
private var user: ExperimentUser? = null
private val engine = EvaluationEngineImpl()

private val variants: LoadStoreCache<Variant> =
private val variants =
getVariantStorage(
this.apiKey,
this.config.instanceName,
storage,
)
private val flags: LoadStoreCache<EvaluationFlag> =
private val flags =
getFlagStorage(
this.apiKey,
this.config.instanceName,
storage,
::mergeInitialFlagsWithStorage,
)

init {
this.variants.load()
this.flags.load()
mergeInitialFlagsWithStorage()
executorService.execute {
synchronized(variants) {
this.variants.load()
}
}
executorService.execute {
synchronized(flags) {
this.flags.load()
}
}
}

private val backoffLock = Any()
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/main/java/com/amplitude/experiment/Experiment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object Experiment {
isDaemon = true
}
}
internal val executorService = ScheduledThreadPoolExecutor(2, daemonThreadFactory)
internal val executorService = ScheduledThreadPoolExecutor(4, daemonThreadFactory)

private val httpClient = OkHttpClient()
private val instances = mutableMapOf<String, ExperimentClient>()
Expand Down
9 changes: 8 additions & 1 deletion sdk/src/main/java/com/amplitude/experiment/storage/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ internal class LoadStoreCache<V>(
private val storage: Storage,
private val decoder: ((value: String) -> V?),
private val encoder: ((value: V) -> String),
private val onLoad: (() -> Unit)? = null,
) {
private val cache: MutableMap<String, V> = mutableMapOf()
private var isLoaded = false

fun get(key: String): V? {
if (!isLoaded) load()
return cache[key]
}

fun getAll(): Map<String, V> {
if (!isLoaded) load()
return HashMap(cache)
}

Expand Down Expand Up @@ -60,6 +64,8 @@ internal class LoadStoreCache<V>(
}.toMap()
clear()
putAll(values)
isLoaded = true
onLoad?.invoke()
}

fun store(values: MutableMap<String, V> = cache) {
Expand Down Expand Up @@ -94,10 +100,11 @@ internal fun getFlagStorage(
deploymentKey: String,
instanceName: String,
storage: Storage,
merger: () -> Unit,
): LoadStoreCache<EvaluationFlag> {
val truncatedDeployment = deploymentKey.takeLast(6)
val namespace = "amp-exp-$instanceName-$truncatedDeployment-flags"
return LoadStoreCache(namespace, storage, ::decodeFlagFromStorage, ::encodeFlagToStorage)
return LoadStoreCache(namespace, storage, ::decodeFlagFromStorage, ::encodeFlagToStorage, merger)
}

internal fun decodeVariantFromStorage(storageValue: String): Variant? {
Expand Down

0 comments on commit 827ce6b

Please sign in to comment.