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

This class provides a framework for implementing locking strategies.

Public Class Methods

new(cfg) click to toggle source

Creates configuration object.

# 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 33
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

Gets session data.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 28
def get_session_data(env, sid)
  raise NotImplementedError
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 42
def handle_error(env = nil, &block)
  begin
    yield
  rescue Aws::DynamoDB::Errors::ServiceError => e
    @config.error_handler.handle_error(e, env)
  end
end
pack_data(data) click to toggle source

Packs session data.

# File lib/aws/session_store/dynamo_db/locking/base.rb, line 23
def pack_data(data)
  [Marshal.dump(data)].pack("m*")
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 147
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 95
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 109
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 131
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 136
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 53
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 142
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 114
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 120
def expire_attr
  { 'expire_at' => expire_at }
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 153
def merge_all(*hashes)
  new_hash = {}
  hashes.each{|hash| new_hash.merge!(hash)}
  new_hash
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 74
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 68
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 87
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 82
def unpack_data(packed_data)
  Marshal.load(packed_data.unpack("m*").first)
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 58
def update_opts(env, sid, session, options = {})
  if env['dynamo_db.new_session']
    updt_options = save_new_opts(env, sid, session)
  else
    updt_options = save_exists_opts(env, sid, session, options)
  end
  updt_options
end
updated_at() click to toggle source

Update client with current time attribute.

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

Attribute for updating session.

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