I'm writing http session manager (gen_server based). That server creates and removes session from in-memory storage. I need to remove expired session on timeout. I have two solutions:
First solution locks server while all sessions are not processed (lock issue). Second solution takes process for each session (memory issue).
Question is which solution is right?
Thank you!
Use timer:send_after
, timer:exit_after
or timer:kill_after
. timer
module uses ets
for store timers and there is only one gen_server
for whole VM. Store timer reference in each session record for timer restarting or so. It is simple and clean solution.
A low frequency event should be handle by a low frequency process IMO. You don't want to be "spending" too much resources on something that's not "generating" value.
The "clean-up" activity does not appear to require "locking-up" the server. Maybe you need to expand on this point.
Why do you need to "lock" something in your solution #1? What are your concerns here? Please detail your concerns a bit more so I can provide more suggestions.
This is how I handle sessions in my pet "web framework".
Worker processes directly look up existing sessions and create new sessions into an ets table (without any server intervention). Also worker processes check after successful lookup if the session is dead. If so, it creates a new session, and deletes the old one. As the ets table need not be ordered, write concurrency can be enabled.
The role of the "session server" is to own the session table, and to spawn a cleanup process every now and then. This is a low prio process that goes through the ets table with ets:next() calls, and deletes expired sessions.
Note that there are no timers involved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With