class SBSM::SessionStore

SessionStore manages session, which are kept in memory Sessions are limited and expire after some time The maximal amount of concurrent session is given via CAP_MAX_THRESHOLD

Constants

CAP_MAX_THRESHOLD
CLEANING_INTERVAL
MAX_SESSIONS
RUN_CLEANER
SESSION
UNKNOWN_USER
VALIDATOR

Attributes

cleaner[R]
persistence_layer[R]
updater[R]

Public Class Methods

clear() click to toggle source
# File lib/sbsm/session_store.rb, line 121
def SessionStore.clear
  @@mutex.synchronize do
    @@sessions.each_value { |sess| sess.__checkout }
    @@sessions.clear
  end
end
new(app:, persistence_layer: nil, trans_handler: nil, session_class: nil, validator: nil, cookie_name: nil, unknown_user: UNKNOWN_USER.new, multi_threaded: nil) click to toggle source
# File lib/sbsm/session_store.rb, line 55
def initialize(app:,
               persistence_layer: nil,
               trans_handler: nil,
               session_class: nil,
               validator: nil,
               cookie_name: nil,
               unknown_user: UNKNOWN_USER.new,
               multi_threaded: nil)
  fail "You must specify an app!" unless app
  @@sessions = {}
  @cleaner = run_cleaner if(self.class.const_get(:RUN_CLEANER))
  @app = app
  @system = persistence_layer
  @persistence_layer = persistence_layer
  @cookie_name = cookie_name
  @trans_handler = trans_handler
  @trans_handler ||= TransHandler.instance
  @session_class = session_class
  @session_class ||= SBSM::Session
  @unknown_user = unknown_user.is_a?(Class) ? unknown_user.new : unknown_user
  @unknown_user ||= UnknownUser.new
  @validator = validator
end
sessions() click to toggle source
# File lib/sbsm/session_store.rb, line 117
def SessionStore.sessions
  @@sessions
end

Public Instance Methods

[](key) click to toggle source
# File lib/sbsm/session_store.rb, line 150
def [](key)
  @@mutex.synchronize do
    unless((s = @@sessions[key]) && !s.expired?)
      s = @@sessions[key] = @session_class.new(app: @app, cookie_name: @cookie_name, trans_handler: @trans_handler, validator: @validator, unknown_user: @unknown_user)
    end
    s.key=key
    s.reset()
    s.touch()
    s
  end
end
cap_max_sessions(now = Time.now) click to toggle source
# File lib/sbsm/session_store.rb, line 78
def cap_max_sessions(now = Time.now)
    if(@@sessions.size > self::class::CAP_MAX_THRESHOLD)
      SBSM.info "too many sessions! Keeping only #{self::class::MAX_SESSIONS}"
      sess = nil
      @@sessions.values.sort[0...(-self::class::MAX_SESSIONS)].each { |sess|
        sess.__checkout
        @@sessions.delete(sess.key)
      }
      if(sess)
        age = sess.age(now)
        SBSM.info sprintf("deleted all sessions that had not been accessed for more than %im %is", age / 60, age % 60)
      end
    end
  seconds = (Time.now.to_i - now.to_i)
  SBSM.warn sprintf("cap_max_sessions to #{self::class::CAP_MAX_THRESHOLD}. took %d seconds", seconds)
end
clean() click to toggle source
# File lib/sbsm/session_store.rb, line 94
def clean
  now = Time.now
  old_size = @@sessions.size
  @@sessions.delete_if do |key, s|
    begin
      if s.respond_to?(:expired?)
        if s.expired?(now)
          s.__checkout
          true
        else
          s.cap_max_states
          false
        end
      else
        true
      end
    rescue
      true
    end
  end
  seconds = (Time.now.to_i - now.to_i)
  # SBSM.warn sprintf("Cleaned #{old_size - @@sessions.size} sessions. Took %d seconds", seconds)
end
delete_session(key) click to toggle source
# File lib/sbsm/session_store.rb, line 127
def delete_session(key)
  if(sess = @@sessions.delete(key))
    sess.__checkout
  end
end
reset() click to toggle source
# File lib/sbsm/session_store.rb, line 132
def reset
  @@mutex.synchronize {
    @@sessions.clear
  }
end
run_cleaner() click to toggle source
# File lib/sbsm/session_store.rb, line 137
def run_cleaner
  # puts "running cleaner thread"
  Thread.new do
    Thread.current.abort_on_exception = true
    #Thread.current.priority = 1
    loop do
      sleep self::class::CLEANING_INTERVAL
      @@mutex.synchronize do
        clean()
      end
    end
  end
end