Class: Rack::Session::AbstractDBM

Inherits:
Abstract::ID
  • Object
show all
Defined in:
lib/rack/session/abstract_dbm.rb

Direct Known Subclasses

GDBM, SDBM

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, path = nil, options = {}) ⇒ AbstractDBM

Returns a new instance of AbstractDBM.



8
9
10
11
12
13
14
15
16
# File 'lib/rack/session/abstract_dbm.rb', line 8

def initialize(app, path=nil, options={})
  super(app, options)

  @mutex = Mutex.new
  db_path = path || (defined?(RACK_ROOT) ? ::File.join(RACK_ROOT, 'tmp', default_session_file) : @default_options[:dbm_path])
  opts = @default_options

  @pool = dbklass.new(db_path)
end

Instance Attribute Details

#mutexObject (readonly)

Returns the value of attribute mutex.



6
7
8
# File 'lib/rack/session/abstract_dbm.rb', line 6

def mutex
  @mutex
end

#poolObject (readonly)

Returns the value of attribute pool.



6
7
8
# File 'lib/rack/session/abstract_dbm.rb', line 6

def pool
  @pool
end

Instance Method Details

#destroy_session(env, session_id, options) ⇒ Object



46
47
48
49
50
51
# File 'lib/rack/session/abstract_dbm.rb', line 46

def destroy_session(env, session_id, options)
  with_lock(env) do
    @pool.delete(session_id)
    generate_sid unless options[:drop]
  end
end

#generate_sidObject



18
19
20
21
22
23
# File 'lib/rack/session/abstract_dbm.rb', line 18

def generate_sid
  loop do
    sid = super
    break sid unless @pool[sid]
  end
end

#get_session(env, sid) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rack/session/abstract_dbm.rb', line 25

def get_session(env, sid)
  with_lock(env, [nil, {}]) do
    unless sid and session = Marshal.load(@pool[sid] || "\x04\b0")
      sid, session = generate_sid, {}
      begin
        @pool[sid] = Marshal.dump(session)
      rescue
        raise "Session collision on '#{sid.inspect}'"
      end
    end
    [sid, session]
  end
end

#set_session(env, session_id, new_session, options) ⇒ Object



39
40
41
42
43
44
# File 'lib/rack/session/abstract_dbm.rb', line 39

def set_session(env, session_id, new_session, options)
  with_lock(env, false) do
    @pool[session_id] = Marshal.dump(new_session)
    session_id
  end
end

#with_lock(env, default = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rack/session/abstract_dbm.rb', line 53

def with_lock(env, default=nil)
  @mutex.lock if env['rack.multithread']
  yield
rescue StandardError
  if $VERBOSE
    warn "#{self} is unable to write to dbm."
    warn $!.inspect
  end
  default
ensure
  @mutex.unlock if @mutex.locked?
end