Rust · Chapter 24 of 26
Async in practice
By the end of this chapter you can pass work between tasks with tokio's channels, limit how many uploads run at once with a Semaphore, give up on a slow call with timeout, wait for the first of several things with select!, stop every task cleanly with a watch channel, and retry a failed upload with backoff. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Concurrency and services
- 4 tasks
- Rust
- In the all labs plan
The first 2 chapters of Rust are free. This one comes with the all labs plan.
Why you need this
Chapter 23 sent all 120 mornings at once: 120 uploads in flight on one thread, in about 40 ms. The plant's office saw it too, and asked for a limit: no more than 8 uploads from DoodhSetu at the same moment, because their server shares its database with three other dairies. Lakhan has his own list: a village line drops for a few seconds, and an upload must be tried again, not lost; a plant that does not answer must not hold a box's task for ever; and at 10 pm, when the service is restarted for an update, every task must finish the upload it is in the middle of and then stop, without starting another.
Chapter 23's tools cannot say any of this. A JoinSet starts everything at once, with no limit. An .await waits for as long as the future takes, even for ever. And there is no way yet to tell a task to stop, or to wait for "an upload or a signal, whichever comes first". This chapter adds tokio's tools for exactly that.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
bounded.rs: every morning uploaded, at most limit at once
The plant's office asked for no more than 8 uploads from DoodhSetu at the same moment. Write the async function upload_bounded(plant: Arc<Plant>, shifts: Vec<(String, Vec<Collection>)>, limit: usize) -> usize. It starts one task for each society, each calling plant.upload_async with that society's id and pourings, but a Semaphore with limit permits lets at most limit uploads run at the same time. It returns how many uploads the plant stored (a failed upload is not stored).
The Check runs all 120 mornings with limit 8, then the first 30 with limit 3, with S014's line down. Each time: one upload for each society, never more than limit in flight at once but more than one, well under 700 ms for the 120 and well under 450 ms for the 30, and 119 and 29 stored. bounded.rs must use a Semaphore.
- Task 2
retry.rs: an upload with a time limit and two retries
Write the async function send_with_retry(plant: &Plant, society: &str, pourings: &[Collection], limit: Duration) -> Result<Receipt, SendError>. It uploads with plant.upload_async, giving each try at most limit, after which that try is given up. It returns the receipt as soon as a try succeeds. When a try fails, it waits and tries again: 10 ms before the second try, 20 ms before the third. After three failed tries it returns the last try's error: SendError::TimedOut for a try that ran out of time, SendError::Plant(the plant's error) for a try the plant refused. An unknown society is never tried again: PlantError::UnknownSociety returns Err(SendError::Plant(PlantError::UnknownSociety(..))) after the first try.
The Check tries S002 on a working plant (its receipt for 35 pourings, one call to the plant), S027 with its line down (Err(SendError::Plant(PlantError::Down("S027"))), 3 calls, at least 90 ms), a plant that takes 200 ms with a limit of 50 ms (Err(SendError::TimedOut), 3 calls, well under 400 ms) and S999 (Err(SendError::Plant(PlantError::UnknownSociety("S999"))), 1 call).
- Task 3
worker.rs: a worker that stops cleanly on a shutdown signal
Write the async function run_worker(plant: Arc<Plant>, jobs: mpsc::Receiver<(String, Vec<Collection>)>, shutdown: watch::Receiver<bool>) -> usize. In a loop it waits with select! for the next job or the shutdown signal (true sent on the watch channel), whichever comes first. A job is a society's id and pourings, uploaded with plant.upload_async. It returns how many uploads the plant stored, when the signal comes or when the job channel is closed and empty. An upload in progress when the signal comes finishes first, and after the signal no new upload starts, even with jobs waiting in the channel.
The Check sends 6 jobs (S001, S002, S014, S003, S004, S005, with S014's line down) and drops the sender: 6 uploads, and run_worker returns 5. It sends 2 jobs, then the signal: run_worker returns 2 within 500 ms of it. Four times, it sends the signal while a 150 ms upload is in progress (S008 the first time), with another job waiting: that upload is stored in full, no other upload starts, and run_worker returns 1.
- Task 4 · interview question
Interview: progress.rs, the upload that never ended
The box shows a dot on its screen every 5 ms while an upload runs. Lakhan's async function upload_with_progress(plant: &Plant, society: &str, pourings: &[Collection]) -> (Result<Receipt, PlantError>, u32) races the upload against a 5 ms tick with select!, in a loop, counting the ticks. With a plant that answers in 20 ms it never returns, and the plant is asked again and again.
Fix it: the plant is asked exactly once, and it returns the plant's answer and the number of ticks it counted while waiting. The Check uploads S002's morning to a plant that answers in 20 ms: within 1 second it must return the receipt for 35 pourings, with exactly one call to the plant and at least 2 ticks.
Farhan will ask: what happens to the future that select! did not pick?
What you will learn
- tokio::sync::mpsc: a channel between tasks, whose send and receive wait without blocking.
- Semaphore: a number of permits; at most that many tasks run a part at once.
- timeout: give a future a time limit.
- select!: wait for the first of several futures; the others are dropped.
- Cancellation: a dropped future stops where it was, and why a select! in a loop needs tokio::pin!.
- watch: one value, such as a shutdown signal, that every task can wait on.
- Retry with backoff: try again, waiting longer each time.
More in Concurrency and services
Build "Async in practice" yourself
DoodhSetu runs the milk collection boxes of 120 village societies around Anand. The old box program crashed on an empty line, and you rebuild it in Rust. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Rust are free. This one comes with the all labs plan.