Make session loading lazy #101
Replies: 5 comments
-
|
cc @bikeshedder, this might be of interest to you. 🙂 |
Beta Was this translation helpful? Give feedback.
-
|
I've put together an implementation on this branch. Some things to note:
One side effect of this approach is the middleware itself is much simpler. Of course, the tradeoff here is that the session itself has become more complex (shuffling state between a store and memory is tricky and there's a good deal of necessary nuance). Regarding the second point: it might be nice if instead of this being a generic parameter we were able to leverage Regarding the third point: I think this is an okay tradeoff. There's more syntax required now. Methods have to be awaited and there's no way to e.g. just grab the value and ignore the result. That said when you know your store cannot fail, you can of course unwrap the result. As for async, this crate is entirely built around async and will be expected to be running within an async context so I think the await syntax is reasonable change. Finally I think something like this design is more or less required for scale. At a certain point, it's not practical to load the session from a store, even if that store can be made very fast, on every middleware request. It's also worth pointing out this is how other established frameworks implement sessions. (In fact, this design very closely mirrors the Django approach we've based our semantics on.) @czocher I'd love your thoughts on this direction. |
Beta Was this translation helpful? Give feedback.
-
|
I looked at integrating the above branch with Given that, I decided to try another approach, where we provide a concrete type for session store errors. That's now in this new branch. Note that this branch has been updated such that the examples and tests should all function; please feel free to give it a try. It's a bit sad to lean on erasing the error type, but if we wanted to continue to use an associated type, that too would require a generic over the session which would leak into e.g. Unless there are objections to this design or thoughts about other ways we might achieve lazy sessions, I'll plan to start migrating the crate to this implementation. |
Beta Was this translation helpful? Give feedback.
-
|
Expanding a bit on the concrete error type, I've iterated just slightly here to define an error enum of three concrete errors. What this means is stores will need to convert their errors to this enum. I think I prefer this over the |
Beta Was this translation helpful? Give feedback.
-
|
I've opened a PR and would appreciate feedback there as well. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Ideally we would not load sessions from stores until the moment we actually want to use them.
Currently we pay an overhead price of loading the session from the backing store on each request where this middleware is installed. This can be mitigated with caching techniques, for example by using
Moka. That said, it's overhead that could be avoided altogether.Consider that many applications will install this middleware at some lower point in their route graph where only a subset of those routes will use the session--this is an example of overhead we should strive to eliminate altogether.
To facilitate this, I'm working on strategy for loading sessions on demand. The current approach involves making the session generic over a store. This would allow us to defer the store load until we interact with the session in some way, say by explicitly loading or saving it.
These methods will be abstracted away such that as a user the interface remains the same, with the exception that the session takes a generic parameter. Internally, the session will manage a kind of cache which is hydrated lazily and sent back to the store when modifications have happened.
This should also simplify the design measurably and perhaps even enable other optimizations, such as a custom future type (i.e. eliminating the need for a boxed future).
Beta Was this translation helpful? Give feedback.
All reactions