-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
30ebddd
commit 2a2d767
Showing
9 changed files
with
180 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogApiError.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
package com.posthog.internal | ||
|
||
import okhttp3.ResponseBody | ||
import java.lang.RuntimeException | ||
|
||
// TODO: rete limit will be part of response in the future | ||
internal data class PostHogApiError(val statusCode: Int, override val message: String) : RuntimeException(message) | ||
internal class PostHogApiError( | ||
val statusCode: Int, | ||
override val message: String, | ||
body: ResponseBody? = null, | ||
) : RuntimeException(message) |
58 changes: 58 additions & 0 deletions
58
posthog-v3/posthog/src/main/java/com/posthog/internal/PostHogFeatureFlags.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.posthog.internal | ||
|
||
import com.posthog.PostHogConfig | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
internal class PostHogFeatureFlags(private val config: PostHogConfig, private val api: PostHogApi) { | ||
private val executor = Executors.newSingleThreadScheduledExecutor(PostHogThreadFactory("PostHogDecideThread")) | ||
|
||
private var isLoadingFeatureFlags = AtomicBoolean(false) | ||
|
||
private val featureFlagsLock = Any() | ||
|
||
private var featureFlags: Map<String, Any>? = null | ||
|
||
@Volatile | ||
private var isFeatureFlagsLoaded = false | ||
|
||
fun loadFeatureFlagsRequest(properties: Map<String, Any>) { | ||
executor.execute { | ||
if (isLoadingFeatureFlags.getAndSet(true)) { | ||
config.logger?.log("Feature flags are being loaded already.") | ||
return@execute | ||
} | ||
|
||
try { | ||
val featureFlags = api.decide(properties) | ||
|
||
synchronized(featureFlagsLock) { | ||
this.featureFlags = featureFlags | ||
} | ||
|
||
isFeatureFlagsLoaded = true | ||
} catch (e: Throwable) { | ||
config.logger?.log("Loading feature flags failed: $e") | ||
} | ||
|
||
isLoadingFeatureFlags.set(false) | ||
} | ||
} | ||
|
||
fun isFeatureEnabled(key: String, defaultValue: Boolean = false): Boolean { | ||
if (!isFeatureFlagsLoaded) { | ||
return defaultValue | ||
} | ||
val value: Any? | ||
|
||
synchronized(featureFlagsLock) { | ||
value = featureFlags?.get(key) | ||
} | ||
|
||
return if (value is Boolean) { | ||
value | ||
} else { | ||
defaultValue | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters