Lottery
Introduction
Sometimes you want code to run on only a fraction of your requests, such as logging extra diagnostics for 1 request in 100 or showing a feedback survey to 1 visitor in 20. Lottery decides whether an action runs based on the odds you specify. Laravel uses a lottery internally to prune expired sessions on 2 out of every 100 requests.
Drawing
Define a lottery with Lottery::odds(wins, out_of) and call draw(), which returns true when the draw wins. You may draw anywhere in your application, such as inside a handler or middleware. For example, this dashboard handler shows a survey on 1 visit in 20:
1use laravel::inertia::{self, Page, props};2use laravel::lottery::Lottery;34pub async fn show(user: User) -> Page {5 let survey = Lottery::odds(1, 20).draw();67 inertia::render("Dashboard", props! { user, survey })8}1use laravel::inertia::{self, Page, props};2use laravel::lottery::Lottery;34pub async fn show(user: User) -> Page {5 let survey = Lottery::odds(1, 20).draw();67 inertia::render("Dashboard", props! { user, survey })8}
Every call to draw is a new draw, so the same user sees the survey on some visits and not on others. When each user should get one draw and keep its result, such as when you roll a new feature out to 1 user in 10, draw inside a feature flag instead. Laravel stores the flag's result for each user.
Testing
A random result makes a test flaky, so you may decide the result of every draw in a test. This test makes every draw win, so the dashboard always asks for feedback:
1use laravel::lottery::Lottery;2use laravel::model::create;3use laravel::testing::prelude::*;45#[laravel::test]6async fn the_dashboard_asks_for_feedback(app: Application) -> laravel::Result<()> {7 let user = create!(User {8 name: "Taylor",10 password: "password",11 })12 .await?;1314 Lottery::always_win();1516 app.get("/dashboard")17 .acting_as(&user)18 .await19 .assert_inertia()20 .equals("survey", true);2122 Ok(())23}1use laravel::lottery::Lottery;2use laravel::model::create;3use laravel::testing::prelude::*;45#[laravel::test]6async fn the_dashboard_asks_for_feedback(app: Application) -> laravel::Result<()> {7 let user = create!(User {8 name: "Taylor",10 password: "password",11 })12 .await?;1314 Lottery::always_win();1516 app.get("/dashboard")17 .acting_as(&user)18 .await19 .assert_inertia()20 .equals("survey", true);2122 Ok(())23}
These functions decide every draw in the test's application, including the session cleanup draw and the draws in your feature flags:
1// Every draw wins, whatever its odds...2Lottery::always_win();34// Every draw loses, whatever its odds...5Lottery::always_lose();67// The next draw wins, then one loses, then draws are random again...8Lottery::fix([true, false]);910// Draws are random again...11Lottery::determine_results_normally();1// Every draw wins, whatever its odds...2Lottery::always_win();34// Every draw loses, whatever its odds...5Lottery::always_lose();67// The next draw wins, then one loses, then draws are random again...8Lottery::fix([true, false]);910// Draws are random again...11Lottery::determine_results_normally();
Each test runs with an isolated application, so calling always_win in one test never affects draws in tests running in parallel.
Lottery::fix supplies predetermined results to draws in the order they occur across the application. In an HTTP test, session middleware draws for cleanup before your handler runs, and middleware such as ShareFeatures may draw as well. To control a handler's draw reliably, use always_win or always_lose, or use feature::fake to pin a specific feature flag.