Skip to content

fix: clear cache if there are no active flags server side #246

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

Merged
merged 7 commits into from
May 27, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 36 additions & 24 deletions posthog/src/main/java/com/posthog/internal/PostHogRemoteConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,23 @@ internal class PostHogRemoteConfig(

val hasFlags = it.hasFeatureFlags ?: false

if (hasFlags && config.preloadFeatureFlags) {
if (distinctId.isNotBlank()) {
// do not process session recording from decide API
// since its already cached via the remote config API
executeFeatureFlags(distinctId, anonymousId, groups, onFeatureFlags, calledFromRemoteConfig = true)
} else {
config.logger.log("Feature flags not loaded, distinctId is invalid: $distinctId")
if (hasFlags) {
if (config.preloadFeatureFlags) {
if (distinctId.isNotBlank()) {
// do not process session recording from decide API
// since its already cached via the remote config API
executeFeatureFlags(distinctId, anonymousId, groups, onFeatureFlags, calledFromRemoteConfig = true)
} else {
config.logger.log("Feature flags not loaded, distinctId is invalid: $distinctId")
}
}
} else {
// clear cache since there are no active flags on the server side
synchronized(featureFlagsLock) {
// we didn't call the API but we should there are no active flags
// because remote config API returned hasFeatureFlags=false
isFeatureFlagsLoaded = true
clearFlags()
}
}

Expand Down Expand Up @@ -207,14 +217,7 @@ internal class PostHogRemoteConfig(
"""Feature flags are quota limited, clearing existing flags.
Learn more about billing limits at https://posthog.com/docs/billing/limits-alerts""",
)
this.featureFlags = null
this.featureFlagPayloads = null
this.flags = null
config.cachePreferences?.let { preferences ->
preferences.remove(FLAGS)
preferences.remove(FEATURE_FLAGS)
preferences.remove(FEATURE_FLAGS_PAYLOAD)
}
clearFlags()
return@let
}

Expand Down Expand Up @@ -450,20 +453,29 @@ internal class PostHogRemoteConfig(
}
}

private fun clearFlags() {
// call this method after synchronized(featureFlagsLock)
this.featureFlags = null
this.featureFlagPayloads = null
this.flags = null
this.requestId = null

config.cachePreferences?.let { preferences ->
preferences.remove(FLAGS)
preferences.remove(FEATURE_FLAGS)
preferences.remove(FEATURE_FLAGS_PAYLOAD)
preferences.remove(FEATURE_FLAG_REQUEST_ID)
}
}

fun clear() {
synchronized(featureFlagsLock) {
featureFlags = null
featureFlagPayloads = null
sessionReplayFlagActive = false
isFeatureFlagsLoaded = false
requestId = null

config.cachePreferences?.let { preferences ->
preferences.remove(FEATURE_FLAGS)
preferences.remove(FEATURE_FLAGS_PAYLOAD)
preferences.remove(SESSION_REPLAY)
preferences.remove(FEATURE_FLAG_REQUEST_ID)
}
clearFlags()

config.cachePreferences?.remove(SESSION_REPLAY)
}

synchronized(remoteConfigLock) {
Expand Down
Loading