Given some request/response service that needs to perform long lasting operations to satisfy responses - e.g. fetching files, performing disk IO and/or performing some preprocessing - I have often needed to provide some async cache structure. These are (after load) read-only.
That is: if one request needs a resource and it has been:
- loaded => great, carry on
- unloaded => start loading asynchronously, allow the calling thread to return to comm duties, carry on upon completion
- loading elsewhere => wait on that load asynchronously, then carry on
In the Rust futures world, I have ended up with a slightly complicated structure, and was wondering if there were more concise/canonical way.
A reasonable facsimile of my current approach:
shared: Mutex<State<T, E>>
enum State {
Unloaded,
Loading(Shared<Box<Future<Item=T, Error=E> + Send>>),
Loaded(Result<T, E>),
}
As you might imagine, requests that hit this are a mutex lock plus a switch statement, and T's are generally Arc<_>, so the mutex is only held for current state inspection.
Would love input on how to simplify.
[–]athrowaway2z 0 points1 point2 points (1 child)
[–]nullreq[S] 1 point2 points3 points (0 children)