跳至正文

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:

go
package dashboard

import (
	"go.laravel.com/inertia"
	"go.laravel.com/lottery"
	"go.laravel.com/web"
)

func Show(r *web.Request) *web.Response {
	return inertia.Render("Dashboard", inertia.Props{
		"survey": lottery.Odds(1, 20).Draw(r),
	})
}

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:

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

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:

go
package feature_test

import (
	"testing"

	"go.laravel.com/laraveltest"
	"go.laravel.com/lottery"

	"app/database/factories"
	"app/tests"
)

func TestTheDashboardAsksForFeedback(t *testing.T) {
	client := tests.New(t, laraveltest.Lottery(lottery.AlwaysWin))
	user := client.Create(factories.User)

	client.ActingAs(&user).
		Get("/dashboard").
		AssertInertia("Dashboard").
		AssertProp("survey", true)
}

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:

go
// Every draw wins, whatever its odds...
laraveltest.Lottery(lottery.AlwaysWin)

// Every draw loses, whatever its odds...
laraveltest.Lottery(lottery.AlwaysLose)

// The next draw wins, then one loses, then draws are random again...
laraveltest.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.