Skip to content

Lottery

Utilities

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. A 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, outOf) and call Draw, which returns true when the draw wins. Draw takes a context, such as the request r, so that a test can decide the draw's result. 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:

1package dashboard
2 
3import (
4 "go.laravel.com/inertia"
5 "go.laravel.com/lottery"
6 "go.laravel.com/web"
7)
8 
9func Show(r *web.Request) *web.Response {
10 return inertia.Render("Dashboard", inertia.Props{
11 "survey": lottery.Odds(1, 20).Draw(r),
12 })
13}
1package dashboard
2 
3import (
4 "go.laravel.com/inertia"
5 "go.laravel.com/lottery"
6 "go.laravel.com/web"
7)
8 
9func Show(r *web.Request) *web.Response {
10 return inertia.Render("Dashboard", inertia.Props{
11 "survey": lottery.Odds(1, 20).Draw(r),
12 })
13}

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.

Session cleanup odds

The default odds of 2 in 100 suit most applications. If you'd like Laravel to clean up expired sessions more or less often, set cfg.Session.Lottery in your application's configure function in bootstrap/app.go:

1func configure(cfg *config.Config, env *config.Env) {
2 cfg.Session.Lottery = lottery.Odds(1, 50)
3}
1func configure(cfg *config.Config, env *config.Env) {
2 cfg.Session.Lottery = lottery.Odds(1, 50)
3}

Testing

A random result makes a test flaky, so you may decide the result of every draw in a test. Pass laraveltest.Lottery when you build the test's client. This test makes every draw win, so the dashboard always asks for feedback:

1package feature_test
2 
3import (
4 "testing"
5 
6 "go.laravel.com/laraveltest"
7 "go.laravel.com/lottery"
8 
9 "app/database/factories"
10 "app/tests"
11)
12 
13func TestTheDashboardAsksForFeedback(t *testing.T) {
14 client := tests.New(t, laraveltest.Lottery(lottery.AlwaysWin))
15 user := client.Create(factories.User)
16 
17 client.ActingAs(&user).
18 Get("/dashboard").
19 AssertInertia("Dashboard").
20 AssertProp("survey", true)
21}
1package feature_test
2 
3import (
4 "testing"
5 
6 "go.laravel.com/laraveltest"
7 "go.laravel.com/lottery"
8 
9 "app/database/factories"
10 "app/tests"
11)
12 
13func TestTheDashboardAsksForFeedback(t *testing.T) {
14 client := tests.New(t, laraveltest.Lottery(lottery.AlwaysWin))
15 user := client.Create(factories.User)
16 
17 client.ActingAs(&user).
18 Get("/dashboard").
19 AssertInertia("Dashboard").
20 AssertProp("survey", true)
21}

laraveltest.Lottery takes one of these results, which 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...
2laraveltest.Lottery(lottery.AlwaysWin)
3 
4// Every draw loses, whatever its odds...
5laraveltest.Lottery(lottery.AlwaysLose)
6 
7// The next draw wins, then one loses, then draws are random again...
8laraveltest.Lottery(lottery.Fix(true, false))
1// Every draw wins, whatever its odds...
2laraveltest.Lottery(lottery.AlwaysWin)
3 
4// Every draw loses, whatever its odds...
5laraveltest.Lottery(lottery.AlwaysLose)
6 
7// The next draw wins, then one loses, then draws are random again...
8laraveltest.Lottery(lottery.Fix(true, false))

Each test builds its own application, so passing AlwaysWin to 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, the session middleware may draw for cleanup before your handler runs, and a share function that shares features with your pages may draw as well. To control a handler's draw reliably, use AlwaysWin or AlwaysLose, or use feature.Fake to pin a specific feature flag.