class Aws::SessionStore::DynamoDB::Locking::Base

Handles session management.

Public Class Methods

new(cfg) click to toggle source

@param [Aws::SessionStore::DynamoDB::Configuration] cfg

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 7
def initialize(cfg)
  @config = cfg
end

Public Instance Methods

delete_session(env, sid) click to toggle source

Deletes session based on id

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 29
def delete_session(env, sid)
  handle_error(env) do
    @config.dynamo_db_client.delete_item(delete_opts(sid))
  end
end
get_session_data(env, sid) click to toggle source

Retrieves session data based on id

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 24
def get_session_data(env, sid)
  raise NotImplementedError
end
set_session_data(env, sid, session, options = {}) click to toggle source

Updates session in database

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 12
def set_session_data(env, sid, session, options = {})
  return false if session.empty?

  packed_session = pack_data(session)
  handle_error(env) do
    save_opts = update_opts(env, sid, packed_session, options)
    @config.dynamo_db_client.update_item(save_opts)
    sid
  end
end

Private Instance Methods

attr_opts() click to toggle source

Attributes to be retrieved via client

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 146
def attr_opts
  { attributes_to_get: ['data'],
    consistent_read: @config.consistent_read }
end
attr_updts(env, session, add_attrs = {}) click to toggle source

Attributes to update via client.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 93
def attr_updts(env, session, add_attrs = {})
  data = data_unchanged?(env, session) ? {} : data_attr(session)
  {
    attribute_updates: merge_all(updated_attr, data, add_attrs, expire_attr),
    return_values: 'UPDATED_NEW'
  }
end
created_attr() click to toggle source

Attribute for creation of session.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 107
def created_attr
  { 'created_at' => updated_at }
end
data_attr(session) click to toggle source
# File lib/aws/session_store/dynamo_db/locking/base.rb, line 129
def data_attr(session)
  { 'data' => { value: session, action: 'PUT' } }
end
data_unchanged?(env, session) click to toggle source

Determine if data has been manipulated

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 134
def data_unchanged?(env, session)
  return false unless env['rack.initial_data']

  env['rack.initial_data'] == session
end
delete_opts(sid) click to toggle source

@return [Hash] Options for deleting session.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 47
def delete_opts(sid)
  table_opts(sid)
end
expected_attributes(sid) click to toggle source

Expected attributes

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 141
def expected_attributes(sid)
  { expected: { @config.table_key => { value: sid, exists: true } } }
end
expire_at() click to toggle source

Update client with current time + max_stale.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 112
def expire_at
  max_stale = @config.max_stale || 0
  { value: (Time.now + max_stale).to_i, action: 'PUT' }
end
expire_attr() click to toggle source

Attribute for TTL expiration of session.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 118
def expire_attr
  { 'expire_at' => expire_at }
end
handle_error(env = nil) { || ... } click to toggle source

Each database operation is placed in this rescue wrapper. This wrapper will call the method, rescue any exceptions and then pass exceptions to the configured error handler.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 40
def handle_error(env = nil)
  yield
rescue Aws::DynamoDB::Errors::ServiceError => e
  @config.error_handler.handle_error(e, env)
end
merge_all(*hashes) click to toggle source

@return [Hash] merged hash of all hashes passed in.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 152
def merge_all(*hashes)
  new_hash = {}
  hashes.each { |hash| new_hash.merge!(hash) }
  new_hash
end
pack_data(data) click to toggle source

Marshal the data.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 75
def pack_data(data)
  [Marshal.dump(data)].pack('m*')
end
save_exists_opts(env, sid, session, options = {}) click to toggle source

@return [Hash] Options for saving an existing sesison in the database.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 67
def save_exists_opts(env, sid, session, options = {})
  add_attr = options[:add_attrs] || {}
  expected = options[:expect_attr] || {}
  attribute_opts = merge_all(attr_updts(env, session, add_attr), expected)
  merge_all(table_opts(sid), attribute_opts)
end
save_new_opts(env, sid, session) click to toggle source

@return [Hash] Options for saving a new session in database.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 61
def save_new_opts(env, sid, session)
  attribute_opts = attr_updts(env, session, created_attr)
  merge_all(table_opts(sid), attribute_opts)
end
table_opts(sid) click to toggle source

Table options for client.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 85
def table_opts(sid)
  {
    table_name: @config.table_name,
    key: { @config.table_key => sid }
  }
end
unpack_data(packed_data) click to toggle source

Unmarshal the data.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 80
def unpack_data(packed_data)
  Marshal.load(packed_data.unpack1('m*'))
end
update_opts(env, sid, session, options = {}) click to toggle source

@return [Hash] Options for updating item in Session table.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 52
def update_opts(env, sid, session, options = {})
  if env['dynamo_db.new_session']
    save_new_opts(env, sid, session)
  else
    save_exists_opts(env, sid, session, options)
  end
end
updated_at() click to toggle source

Update client with current time attribute.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 102
def updated_at
  { value: Time.now.to_f.to_s, action: 'PUT' }
end
updated_attr() click to toggle source

Attribute for updating session.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 123
def updated_attr
  {
    'updated_at' => updated_at
  }
end