rust
42 lines · 7 steps
Aggregating metrics across threads in Rust
Worker threads tally HTTP statuses into one shared, mutex-guarded struct, then the main thread reclaims it.
Explained by
highlit
1use std::collections::HashMap;
2use std::sync::{Arc, Mutex};
3use std::thread;
4
5#[derive(Default)]
6struct Metrics {
7 requests: u64,
8 errors: u64,
9 status_counts: HashMap<u16, u64>,
10}
11
12fn aggregate_responses(statuses: Vec<u16>, workers: usize) -> Metrics {
13 let metrics = Arc::new(Mutex::new(Metrics::default()));
14 let chunk_size = (statuses.len() + workers - 1) / workers;
15
16 let mut handles = Vec::with_capacity(workers);
17 for chunk in statuses.chunks(chunk_size) {
18 let metrics = Arc::clone(&metrics);
19 let chunk = chunk.to_vec();
20
21 handles.push(thread::spawn(move || {
22 for status in chunk {
23 let mut m = metrics.lock().unwrap();
24 m.requests += 1;
25 if status >= 400 {
26 m.errors += 1;
27 }
28 *m.status_counts.entry(status).or_insert(0) += 1;
29 }
30 }));
31 }
32
33 for handle in handles {
34 handle.join().unwrap();
35 }
36
37 Arc::try_unwrap(metrics)
38 .ok()
39 .expect("all worker threads have finished")
40 .into_inner()
41 .unwrap()
42}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Arc plus Mutex is the standard pattern for safely sharing mutable state across threads.
- 2Splitting work into chunks lets you parallelize a flat list across a fixed number of workers.
- 3Once all threads join, Arc::try_unwrap recovers sole ownership so you can unwrap the inner value.
Related explainers
rust
use serde::Deserialize; #[derive(Debug, Deserialize)] #[serde(untagged)]
Parsing flexible JSON shapes with serde
deserialization
enums
json
Intermediate
6 steps
rust
use std::cmp::{Ordering, Reverse}; use std::collections::BinaryHeap; use std::fs::File; use std::io::{self, BufRead, BufReader, BufWriter, Lines, Write};
K-way merge of sorted logs in Rust
binary-heap
k-way-merge
streaming-io
Intermediate
8 steps
rust
use axum::{ extract::{Path, State}, response::sse::{Event, KeepAlive, Sse}, };
Streaming import progress with SSE in Axum
server-sent-events
streams
watch-channel
Advanced
7 steps
rust
use std::f64::consts::PI; #[derive(Debug, Clone, Copy)] pub struct LatLng {
Building geographic bounding boxes in Rust
geospatial
value-types
option
Intermediate
7 steps
go
func (w *Watcher) resetDebounce(d time.Duration) { if !w.timer.Stop() { select { case <-w.timer.C:
Debouncing a stream of events in Go
debounce
timers
channels
Advanced
7 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/aggregating-metrics-across-threads-in-rust-explained-rust-4a20/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.