# class Rack::Session::Pool

`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

### Constants

DEFAULT_OPTIONS

### Attributes

mutex[R]
pool[R]

### Public Class Methods

new(app, options = {}) click to toggle source
Calls superclass method `Rack::Session::Abstract::Persisted::new`

    # File lib/rack/session/pool.rb, line 33
    def initialize(app, options = {})
      super
      @pool = Hash.new
      @mutex = Mutex.new
    end

### Public Instance Methods

delete_session(req, session_id, options) click to toggle source

    # File lib/rack/session/pool.rb, line 64
    def delete_session(req, session_id, options)
      with_lock(req) do
        @pool.delete(session_id.public_id)
        @pool.delete(session_id.private_id)
        unless options[:drop]
          sid = generate_sid
          @pool.store(sid.private_id, {})
          sid
        end
      end
    end

find_session(req, sid) click to toggle source

    # File lib/rack/session/pool.rb, line 46
    def find_session(req, sid)
      with_lock(req) do
        unless sid and session = get_session_with_fallback(sid)
          sid, session = generate_sid, {}
          @pool.store sid.private_id, session
        end
        [sid, session]
      end
    end

generate_sid() click to toggle source
Calls superclass method `Rack::Session::Abstract::PersistedSecure#generate_sid`

    # File lib/rack/session/pool.rb, line 39
    def generate_sid
      loop do
        sid = super
        break sid unless @pool.key? sid.private_id
      end
    end

with_lock(req) { || ... } click to toggle source

    # File lib/rack/session/pool.rb, line 76
    def with_lock(req)
      @mutex.lock if req.multithread?
      yield
    ensure
      @mutex.unlock if @mutex.locked?
    end

write_session(req, session_id, new_session, options) click to toggle source

    # File lib/rack/session/pool.rb, line 56
    def write_session(req, session_id, new_session, options)
      with_lock(req) do
        return false unless get_session_with_fallback(session_id)
        @pool.store session_id.private_id, new_session
        session_id
      end
    end

### Private Instance Methods

get_session_with_fallback(sid) click to toggle source

    # File lib/rack/session/pool.rb, line 85
    def get_session_with_fallback(sid)
      @pool[sid.private_id] || @pool[sid.public_id]
    end