class Aws::SessionStore::DynamoDB::Configuration

This class provides a Configuration object for all DynamoDB transactions by pulling configuration options from Runtime, a YAML file, the ENV and default settings.

Environment Variables

The Configuration object can load default values from your environment. An example of setting and environment variable is below:

export DYNAMO_DB_SESSION_TABLE_NAME='Sessions'

Handling Errors

There are two configurable options for error handling: :raise_errors and :error_handler.

If you would like to use the Default Error Handler, you can decide to set :raise_errors to true or false depending on whether you want all errors, regadless of class, to be raised up the stack and essentially throw a 500.

If you decide to use your own Error Handler. You may pass it in for the value of the key :error_handler as a cofniguration object. You must implement the BaseErrorHandler class. @see BaseHandler Interface for Error Handling for DynamoDB Session Store.

Locking Strategy

By default, locking is not implemented for the session store. You must trigger the locking strategy through the configuration of the session store. Pessimistic locking, in this case, means that only one read can be made on a session at once. While the session is being read by the process with the lock, other processes may try to obtain a lock on the same session but will be blocked. See the accessors with lock in their name for how to configure the pessimistic locking strategy to your needs.

DynamoDB Specific Options

You may configure the table name and table hash key value of your session table with the :table_name and :table_key options. You may also configure performance options for your table with the :consistent_read, :read_capacity, write_capacity. For more information about these configurations see CreateTable method for Amazon DynamoDB.

Constants

DEFAULTS

Default configuration options

Attributes

config_file[R]

@return [String,Pathname]

consistent_read[R]

@return [true] If a strongly consistent read is used @return [false] If an eventually consistent read is used. See AWS DynamoDB documentation for table consistent_read for more information on this setting.

dynamo_db_client[R]

@return [DynamoDB Client] DynamoDB client.

enable_locking[R]

@return [true] Pessimistic locking strategy will be implemented for

all session accesses.

@return [false] No locking strategy will be implemented for

all session accesses.
error_handler[R]

@return [Error Handler] An error handling object that handles all exceptions

thrown during execution of the AWS DynamoDB Session Store Rack Middleware.
For more information see the Handling Errors Section.
lock_expiry_time[R]

@return [Integer] Time in milleseconds after which lock will expire.

lock_max_wait_time[R]

@return [Integer] Maximum time in seconds to wait to acquire lock

before giving up.
lock_retry_delay[R]

@return [Integer] Time in milleseconds to wait before retrying to obtain

lock once an attempt to obtain lock has been made and has failed.
max_age[R]

@return [Integer] Maximum number of seconds earlier

from the current time that a session was created.
max_stale[R]

@return [Integer] Maximum number of seconds

before the current time that the session was last accessed.
raise_errors[R]

@return [true] All errors are raised up the stack when default ErrorHandler

is used.

@return [false] Only specified errors are raised up the stack when default

ErrorHandler is used.
read_capacity[R]

@return [Integer] Maximum number of reads consumed per second before

DynamoDB returns a ThrottlingException. See AWS DynamoDB documentation
for table read_capacity for more information on this setting.
secret_key[R]

@return [String] The secret key for HMAC encryption.

table_key[R]

@return [String] Session table hash key name.

table_name[R]

@return [String] Session table name.

write_capacity[R]

@return [Integer] Maximum number of writes consumed per second before

DynamoDB returns a ThrottlingException. See AWS DynamoDB documentation
for table write_capacity for more information on this setting.

Public Class Methods

new(options = {}) click to toggle source

Provides configuration object that allows access to options defined during Runtime, in a YAML file, in the ENV and by default.

@option options [String] :table_name (“Sessions”) Name of the session

table.

@option options [String] :table_key (“id”) The hash key of the sesison

table.

@option options [Boolean] :consistent_read (true) If true, a strongly

consistent read is used. If false, an eventually consistent read is
used.

@option options [Integer] :read_capacity (10) The maximum number of

strongly consistent reads consumed per second before
DynamoDB raises a ThrottlingException. See AWS DynamoDB documentation
for table read_capacity for more information on this setting.

@option options [Integer] :write_capacity (5) The maximum number of writes

consumed per second before DynamoDB returns a ThrottlingException.
See AWS DynamoDB documentation for table write_capacity for more
information on this setting.

@option options [DynamoDB Client] :dynamo_db_client

(Aws::DynamoDB::Client) DynamoDB client used to perform database
operations inside of middleware application.

@option options [Boolean] :raise_errors (false) If true, all errors are

raised up the stack when default ErrorHandler. If false, Only specified
errors are raised up the stack when default ErrorHandler is used.

@option options [Error Handler] :error_handler (DefaultErrorHandler)

An error handling object that handles all exceptions thrown during
execution of the AWS DynamoDB Session Store Rack Middleware.
For more information see the Handling Errors Section.

@option options [Integer] :max_age (nil) Maximum number of seconds earlier

from the current time that a session was created.

@option options [Integer] :max_stale (nil) Maximum number of seconds

before current time that session was last accessed.

@option options [String] :secret_key (nil) Secret key for HMAC encription. @option options [Integer] :enable_locking (false) If true, a pessimistic

locking strategy will be implemented for all session accesses.
If false, no locking strategy will be implemented for all session
accesses.

@option options [Integer] :lock_expiry_time (500) Time in milliseconds

after which lock expires on session.

@option options [Integer] :lock_retry_delay (500) Time in milleseconds to

wait before retrying to obtain lock once an attempt to obtain lock
has been made and has failed.

@option options [Integer] :lock_max_wait_time (500) Maximum time

in seconds to wait to acquire lock before giving up.

@option options [String] :secret_key (SecureRandom.hex(64))

Secret key for HMAC encription.
# File lib/aws/session_store/dynamo_db/configuration.rb, line 176
def initialize(options = {})
  @options = default_options.merge(
    env_options.merge(
      file_options(options).merge(symbolize_keys(options))
    )
  )
  @options = client_error.merge(@options)
  set_attributes(@options)
end

Public Instance Methods

to_hash() click to toggle source

@return [Hash] The merged configuration hash.

# File lib/aws/session_store/dynamo_db/configuration.rb, line 187
def to_hash
  @options.dup
end

Private Instance Methods

client_error() click to toggle source

@return [Hash] Client and error objects in hash.

# File lib/aws/session_store/dynamo_db/configuration.rb, line 210
def client_error
  gen_error_handler.merge(gen_dynamo_db_client)
end
config_file_path(options) click to toggle source

@return [String] Configuration path found in environment or YAML file.

# File lib/aws/session_store/dynamo_db/configuration.rb, line 246
def config_file_path(options)
  options[:config_file] || ENV["DYNAMO_DB_SESSION_CONFIG_FILE"]
end
default_options() click to toggle source

@return [Hash] Default Session table options.

# File lib/aws/session_store/dynamo_db/configuration.rb, line 215
def default_options
  DEFAULTS
end
env_options() click to toggle source

@return [Hash] Environment options that are useful for Session Handler.

# File lib/aws/session_store/dynamo_db/configuration.rb, line 220
def env_options
  default_options.keys.inject({}) do |opts, opt_name|
    env_var = "DYNAMO_DB_SESSION_#{opt_name.to_s.upcase}"
    opts[opt_name] = ENV[env_var] if ENV.key?(env_var)
    opts
  end
end
file_options(options = {}) click to toggle source

@return [Hash] File options.

# File lib/aws/session_store/dynamo_db/configuration.rb, line 229
def file_options(options = {})
  file_path = config_file_path(options)
  if file_path
    load_from_file(file_path)
  else
    {}
  end
end
gen_dynamo_db_client() click to toggle source

@return [Hash] DDB client.

# File lib/aws/session_store/dynamo_db/configuration.rb, line 194
def gen_dynamo_db_client
  client_opts = { user_agent_suffix: " aws-sessionstore/#{VERSION}" }
  client = Aws::DynamoDB::Client
  dynamo_db_client = @options[:dynamo_db_client] || client.new(client_opts)
  {:dynamo_db_client => dynamo_db_client}
end
gen_error_handler() click to toggle source

@return [Hash] Default Error Handler

# File lib/aws/session_store/dynamo_db/configuration.rb, line 202
def gen_error_handler
  default_handler = Aws::SessionStore::DynamoDB::Errors::DefaultHandler
  error_handler = @options[:error_handler] ||
                        default_handler.new(@options[:raise_errors])
  {:error_handler => error_handler}
end
load_from_file(file_path) click to toggle source

Load options from YAML file

# File lib/aws/session_store/dynamo_db/configuration.rb, line 239
def load_from_file(file_path)
  require "erb"
  opts = YAML.load(ERB.new(File.read(file_path)).result) || {}
  symbolize_keys(opts)
end
set_attributes(options) click to toggle source

Set accessible attributes after merged options.

# File lib/aws/session_store/dynamo_db/configuration.rb, line 251
def set_attributes(options)
  @options.keys.each do |opt_name|
    instance_variable_set("@#{opt_name}", options[opt_name])
  end
end
symbolize_keys(options) click to toggle source

@return [Hash] Hash with all symbolized keys.

# File lib/aws/session_store/dynamo_db/configuration.rb, line 258
def symbolize_keys(options)
  options.inject({}) do |opts, (opt_name, opt_value)|
    opts[opt_name.to_sym] = opt_value
    opts
  end
end