Suppose you have a web server that receives requests via an async closure, like in hyper or warp. You want requests to modify a shared datastructure—e.g. a running count of all requests so far, or a list of the first byte of each request.
How do you do this?
Attempted outline (doesn't work):
#[tokio::main]
async fn main() {
let mut y = SharedDatastructure::new();
let hello = warp::path!("hello" / String)
.map(|name| {
// first, do some processing
let data = name + "!";
// then access a shared datastructure
y.update(data);
y.get_next_response()
});
warp::serve(hello)
.run(([127, 0, 0, 1], 8000))
.await;
}
The problem is that you can't update the SharedDatastructure
in multiple threads. I tried to pass an broadcast
, but you need to create a new producer for every request, and I can't do that either outside the closure—when you don't know how many producers there will be—or inside it.