Class: Rack::Session::Pool

Inherits:
Abstract::PersistedSecure show all
Defined in:
lib/rack/session/pool.rb

Overview

Rack::Session::Pool provides simple cookie based session management. Session data is stored in a hash held by @pool. In the context of a multithreaded environment, sessions being committed to the pool is done in a merging manner.

The :drop option is available in rack.session.options if you wish to explicitly remove the session from the session cache.

Example:

myapp = MyRackApp.new
sessioned = Rack::Session::Pool.new(myapp,
  :domain => 'foo.com',
  :expire_after => 2592000
)
Rack::Handler::WEBrick.run sessioned

Constant Summary collapse

DEFAULT_OPTIONS =
Abstract::ID::DEFAULT_OPTIONS.merge(drop: false, allow_fallback: true)

Instance Attribute Summary collapse

Attributes inherited from Abstract::Persisted

#default_options, #key, #same_site, #sid_secure

Instance Method Summary collapse

Methods inherited from Abstract::PersistedSecure

#extract_session_id

Methods inherited from Abstract::Persisted

#call, #commit_session, #context

Constructor Details

#initialize(app, options = {}) ⇒ Pool

Returns a new instance of Pool.



30
31
32
33
34
35
# File 'lib/rack/session/pool.rb', line 30

def initialize(app, options = {})
  super
  @pool = Hash.new
  @mutex = Mutex.new
  @allow_fallback = @default_options.delete(:allow_fallback)
end

Instance Attribute Details

#mutexObject (readonly)

Returns the value of attribute mutex.



27
28
29
# File 'lib/rack/session/pool.rb', line 27

def mutex
  @mutex
end

#poolObject (readonly)

Returns the value of attribute pool.



27
28
29
# File 'lib/rack/session/pool.rb', line 27

def pool
  @pool
end

Instance Method Details

#delete_session(req, session_id, options) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/rack/session/pool.rb', line 61

def delete_session(req, session_id, options)
  @mutex.synchronize do
    @pool.delete(session_id.public_id)
    @pool.delete(session_id.private_id)
    generate_sid(use_mutex: false) unless options[:drop]
  end
end

#find_session(req, sid) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/rack/session/pool.rb', line 44

def find_session(req, sid)
  @mutex.synchronize do
    unless sid and session = get_session_with_fallback(sid)
      sid, session = generate_sid(use_mutex: false), {}
      @pool.store sid.private_id, session
    end
    [sid, session]
  end
end

#generate_sid(*args, use_mutex: true) ⇒ Object



37
38
39
40
41
42
# File 'lib/rack/session/pool.rb', line 37

def generate_sid(*args, use_mutex: true)
  loop do
    sid = super(*args)
    break sid unless use_mutex ? @mutex.synchronize { @pool.key? sid.private_id } : @pool.key?(sid.private_id)
  end
end

#write_session(req, session_id, new_session, options) ⇒ Object



54
55
56
57
58
59
# File 'lib/rack/session/pool.rb', line 54

def write_session(req, session_id, new_session, options)
  @mutex.synchronize do
    @pool.store session_id.private_id, new_session
    session_id
  end
end