The following example is a possible alternative to the Rack::Session.

def new_session(user_id)
Thread.new{Thread.current[:name] = user_id; puts 'hi'; sleep 40; puts 'beep'}
end

def renew_session(user_id)
Thread.list.detect {|x| x[:name] == user_id}.kill
new_session(user_id)
end

# initialise a memory store for user g234
# ...
# then start the timer
new_session('g234')

# initialise a memory store for user g238
# ...
# then start the timer
new_session('g238')

# ... 30 seconds later the user g234 makes another request which means
# we need to keep this session alive
renew_session('g234')

# user has already disconnected which means their session will
# automatically timeout and their memory store will erased.


Note: The 'puts "beep"' would be replaced by the storage clean-up code.

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/FH4i7F6sAuk/11151