dust

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. dust-framework 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:

rust
use dust::http::Json;use dust::lottery::Lottery;use serde_json::{Value, json};pub async fn show() -> Json<Value> {    let survey = Lottery::odds(1, 20).draw();    Json(json!({ "survey": survey }))}

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. dust-framework stores the flag's result for each user.

How the odds work

Lottery::odds(wins, out_of) wins when a random number from 0 to out_of - 1 is less than wins. The odds are exact in the long run, not per user or per session: 1 in 20 does not mean every twentieth visitor, and a small sample can look nothing like the configured rate.

Odds Behavior
odds(1, 100) Wins about 1% of draws.
odds(0, 100) Never wins.
odds(100, 100) Always wins.
odds(150, 100) Always wins; wins at or above out_of is certain.
odds(1, 0) Never wins; out_of zero is not a division by zero.

The draw uses the operating system's random source, so it is not tied to the request ID, the user, or the database. Two draws in the same request are independent unless you cache the result yourself.

Drawing once per user

A survey that appears on every visit is annoying, and a feature roll-out that changes its mind per request is worse. When the decision belongs to a user, store it. A feature flag does this for you:

rust
use dust::prelude::*;use crate::features;if user.features().active(features::new_dashboard).await? {    return Ok(Redirect::to("/dashboard/v2").into_response());}

The feature flags guide shows how to define a flag with a lottery, and how the stored result is scoped to each user. Use a raw lottery for work that is genuinely per event, such as sampling a request for extra diagnostics.

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:

rust
use dust::lottery::Lottery;use dust::testing::prelude::*;#[dust::test]async fn the_dashboard_asks_for_feedback(app: Application) {    Lottery::always_win();    app.get("/dashboard")        .await        .assert_ok()        .assert_json_path("survey", true);}

These functions decide every draw in the test's application, including the session cleanup draw and the draws in your feature flags:

rust
// Every draw wins, whatever its odds...Lottery::always_win();// Every draw loses, whatever its odds...Lottery::always_lose();// The next draw wins, then one loses, then draws are random again...Lottery::fix([true, false]);// Draws are random again...Lottery::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.