Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Erlang: Session management and timeout

Tags:

erlang

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:

  1. Create one timer to remove expired sessions from manager
  2. Create timer for each session

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!

like image 349
Kirill Trofimov Avatar asked Nov 09 '09 13:11

Kirill Trofimov


3 Answers

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.

like image 62
Hynek -Pichi- Vychodil Avatar answered Oct 17 '22 22:10

Hynek -Pichi- Vychodil


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.

like image 34
jldupont Avatar answered Oct 17 '22 21:10

jldupont


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.

like image 1
Zed Avatar answered Oct 17 '22 23:10

Zed