FlippingStrategy

fun interface FlippingStrategy(source)

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

Link copied to clipboard

Combines this strategy with other using logical AND.

Link copied to clipboard
abstract suspend fun evaluate(featureId: String, store: FeatureStore?, context: FlippingExecutionContext): Boolean

Evaluates whether the feature should be enabled based on the execution context.

Link copied to clipboard

Negates this strategy.

Link copied to clipboard

Combines this strategy with other using logical OR.