- Very short & simple
- Compatible with its PHP counterpart: php-toggles
- Calculates strategies only once per browser session and then caches the result in a session cookie
Gradual
rollout strategy does not depend on user ID per se. Instead, it may be given any user-related textual ID.Whitelist
andBlacklist
depend on the same user ID.- Provides
AnonymousOn
(enables a feature for anonymous users) andAnonymousOff
(disables a feature for anonymous users)
Init:
import com.osinka.play.toggles._
object Toggles extends ToggleRegistry {
private val authReqPf: PartialFunction[RequestHeader,String] = {
case ar: AuthenticatedRequest[_, _] => ar.user.toString
}
private object UserStrategies extends UserStrategies(authReqPf.lift)
val socialBlock = toggle("socialBlock")(
Strategies.internalNet,
UserStrategies.anonymousOff,
UserStrategies.whitelist
)
val testedAndOn = toggle("testedAndOn")(
Strategies.internalNet,
UserStrategies.anonymousOff,
)
val inDevelopment = toggle("inDevelopment")(
Strategies.internalNet,
UserStrategies.anonymousOff,
)
}
You would need to bind ToggleRegistry
to Toggles
implementation:
class Module extends AbstractModule {
def configure(): Unit = {
bind(classOf[ToggleRegistry]).to(Toggles)
}
}
In conf/application.conf
:
toggles {
socialBlock {
whitelist = ["123456", "98765"]
}
testedAndOn.enabled = true
inDevelopment.enabled = false
}
Strategies will be calculated only if enabled
is not defined in the configuration. The order of checks is as follows:
- if the feature is enabled in the configuration, it is active
- if the feature is in a session cookie, it is active
- if the feature is disabled in the configuration, it is active
- then the strategies get checked one by one. The first that gives a definite result wins.
In order to check the toggles, either a filter should be used:
object Filter @Inject() (toggles: ToggleFilter) extends DefaultHttpFilters(toggles)
or wrap specific actions into ToggleAction(Toggles)
Then check the toggle anywhere (depends on implicit requestHeader
):
if (Toggles.socialBlock.active) {
}