FlippingStrategy
Defines a strategy for determining whether a feature should be enabled.
A flipping strategy encapsulates the logic that controls feature activation based on runtime conditions, configuration parameters, and execution context. Common use cases include:
Gradual rollout: Enable features for a percentage of users
Region-based: Enable features only in specific geographical regions
Time-based: Enable features during specific time windows
User-based: Enable features for specific user segments or roles
Custom logic: Any business-specific activation rules
Example implementations:
// Percentage-based rollout
class PercentageStrategy(override val initParams: Map<String, String>) : FlippingStrategy {
override suspend fun evaluate(
featureId: String,
store: FeatureStore?,
context: FlippingExecutionContext
): Boolean {
val percentage = initParams["percentage"]?.toInt() ?: 0
val userId = context.get<String>(ContextKeys.USER_ID) ?: return false
return userId.hashCode() % 100 < percentage
}
}
// Region-based strategy
class RegionStrategy(override val initParams: Map<String, String>) : FlippingStrategy {
override suspend fun evaluate(
featureId: String,
store: FeatureStore?,
context: FlippingExecutionContext
): Boolean {
val allowedRegions = initParams["regions"]?.split(",") ?: emptyList()
val userRegion = context.get<String>(ContextKeys.REGION) ?: return false
return userRegion in allowedRegions
}
}Inheritors
Functions
Combines this strategy with other using logical AND.
Evaluates whether the feature should be enabled based on the execution context.
Negates this strategy.
Combines this strategy with other using logical OR.