Class: AdvancedConnection::Config

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin, Singleton
Defined in:
lib/advanced_connection/config.rb

Constant Summary collapse

VALID_QUEUE_TYPES =
[
  :fifo, :lifo, :stack, :prefer_younger, :prefer_older
].freeze
CALLBACK_TYPES =
ActiveSupport::OrderedOptions.new.merge(
  before: nil,
  around: nil,
  after: nil
).freeze
DEFAULT_CONFIG =
ActiveSupport::OrderedOptions.new.merge(
  idle_manager_log_level:         ::Logger::INFO,
  enable_without_connection:      false,
  enable_statement_pooling:       false,
  enable_idle_connection_manager: false,
  connection_pool_queue_type:     :fifo,
  warmup_connections:             nil,
  min_idle_connections:           nil,
  max_idle_connections:           ::Float::INFINITY,
  max_idle_time:                  0,
  idle_check_interval:            nil,
  callbacks:                      ActiveSupport::OrderedOptions.new
).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



103
104
105
106
107
# File 'lib/advanced_connection/config.rb', line 103

def initialize
  @loaded = false
  @config = DEFAULT_CONFIG.deep_dup
  @mutex  = Monitor.new
end

Instance Attribute Details

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



100
101
102
# File 'lib/advanced_connection/config.rb', line 100

def loaded
  @loaded
end

Class Method Details

.add_callback(*names) ⇒ Object Also known as: add_callbacks



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/advanced_connection/config.rb', line 69

def add_callback(*names)
  Array(names).flatten.each { |name|
    class_eval(<<-EOS, __FILE__, __LINE__ + 1)
      def #{name}_callbacks
        @config.callbacks.#{name} ||= CALLBACK_TYPES.dup
      end

      def #{name}_callbacks=(value)
        if not value.is_a? Hash
          fail Error::ConfigError, "#{name} callbacks must be a hash"
        elsif (bad_options = (value.keys.collect(&:to_sym) - CALLBACK_TYPES.keys)).size > 0
          plural = bad_options .size > 1 ? 's' : ''
          fail Error::ConfigError, "Unexpected callback option\#{plural}: " \
                                   " `\#{bad_options.join('`, `')}`"
        elsif (uncallable = value.select { |k,v| !v.respond_to? :call }).present?
          plural = uncallable.size > 1 ? 's' : ''
          fail Error::ConfigError, "Expected #{name} callback\#{plural}" \
                                   " `\#{uncallable.keys.join('`, `')}` to be callable"
        end

        @config.callbacks.#{name} = CALLBACK_TYPES.merge(value)
      end
    EOS
    DEFAULT_CONFIG.callbacks[name.to_sym] = CALLBACK_TYPES.dup
  }
end

.include?(key) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/advanced_connection/config.rb', line 65

def include?(key)
  instance.include?(key) || super
end

.method_missing(method, *args, &block) ⇒ Object



56
57
58
59
# File 'lib/advanced_connection/config.rb', line 56

def method_missing(method, *args, &block)
  return super unless instance.include?(method) || instance.respond_to?(method)
  instance.public_send(method, *args, &block)
end

.respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/advanced_connection/config.rb', line 61

def respond_to_missing?(method, include_private = false)
  instance.respond_to?(method, include_private) || super
end

Instance Method Details

#[](key) ⇒ Object



126
127
128
# File 'lib/advanced_connection/config.rb', line 126

def [](key)
  @config[key.to_sym]
end

#[]=(key, value) ⇒ Object



130
131
132
# File 'lib/advanced_connection/config.rb', line 130

def []=(key, value)
  public_send("#{key}=".to_sym, value)
end

#callbacksObject



142
143
144
# File 'lib/advanced_connection/config.rb', line 142

def callbacks
  @config.callbacks
end

#can_enable?Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
# File 'lib/advanced_connection/config.rb', line 114

def can_enable?
  # don't enable if we're running rake tasks generally,
  # and specifically, if it's db: or assets: tasks
  return false if $0.include? 'rake'
  return false if ARGV.grep(/^(assets|db):/).any?
  true
end

#connection_pool_queue_typeObject



252
253
254
# File 'lib/advanced_connection/config.rb', line 252

def connection_pool_queue_type
  @config[:connection_pool_queue_type]
end

#connection_pool_queue_type=(value) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/advanced_connection/config.rb', line 256

def connection_pool_queue_type=(value)
  unless value.is_a?(String) || value.is_a?(Symbol)
    fail Error::ConfigError, 'Expected String or Symbol for connection_pool_queue_type ' \
                       "but found `#{value.class.name}`"
  end

  unless VALID_QUEUE_TYPES.include? value.to_sym
    fail Error::ConfigError, 'Expected connection_pool_queue_type to be one of ' \
                       ':fifo, :lifo, :stack, :prefer_younger, or :prefer_older ' \
                       "but found `#{value.inspect}`"
  end
  synchronize { @config[:connection_pool_queue_type] = value }
end

#enable_idle_connection_managerObject



183
184
185
# File 'lib/advanced_connection/config.rb', line 183

def enable_idle_connection_manager
  @config[:enable_idle_connection_manager]
end

#enable_idle_connection_manager=(value) ⇒ Object



187
188
189
# File 'lib/advanced_connection/config.rb', line 187

def enable_idle_connection_manager=(value)
  synchronize { @config[:enable_idle_connection_manager] = !!value }
end

#enable_statement_poolingObject



172
173
174
# File 'lib/advanced_connection/config.rb', line 172

def enable_statement_pooling
  @config[:enable_statement_pooling]
end

#enable_statement_pooling=(value) ⇒ Object



176
177
178
179
180
181
# File 'lib/advanced_connection/config.rb', line 176

def enable_statement_pooling=(value)
  if enable_without_connection && !!value
    raise Error::ConfigError, "Statement Pooling conflicts with WithoutConnection feature"
  end
  synchronize { @config[:enable_statement_pooling] = !!value }
end

#enable_without_connectionObject



161
162
163
# File 'lib/advanced_connection/config.rb', line 161

def enable_without_connection
  @config[:enable_without_connection]
end

#enable_without_connection=(value) ⇒ Object



165
166
167
168
169
170
# File 'lib/advanced_connection/config.rb', line 165

def enable_without_connection=(value)
  if enable_statement_pooling && !!value
    raise Error::ConfigError, "WithoutConnection blocks conflict with Statement Pooling feature"
  end
  synchronize { @config[:enable_without_connection] = !!value }
end

#idle_check_intervalObject



240
241
242
# File 'lib/advanced_connection/config.rb', line 240

def idle_check_interval
  @config[:idle_check_interval]
end

#idle_check_interval=(value) ⇒ Object



244
245
246
247
248
249
250
# File 'lib/advanced_connection/config.rb', line 244

def idle_check_interval=(value)
  unless value.is_a?(Numeric) || value =~ /^\d+$/
    fail Error::ConfigError, 'Expected idle_check_interval to be ' \
                       "a valid integer value, but found `#{value.inspect}`"
  end
  synchronize { @config[:idle_check_interval] = value.to_i }
end

#idle_manager_log_levelObject



146
147
148
# File 'lib/advanced_connection/config.rb', line 146

def idle_manager_log_level
  @config[:log_level]
end

#idle_manager_log_level=(level) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/advanced_connection/config.rb', line 150

def idle_manager_log_level=(level)
  @config[:log_level] = case level
    when :debug, /debug/i, ::Logger::DEBUG then ::Logger::DEBUG
    when :info,  /info/i,  ::Logger::INFO  then ::Logger::INFO
    when :warn,  /warn/i,  ::Logger::WARN  then ::Logger::WARN
    when :error, /error/i, ::Logger::ERROR then ::Logger::ERROR
    when :fatal, /fatal/i, ::Logger::FATAL then ::Logger::FATAL
    else ::Logger::INFO
  end
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/advanced_connection/config.rb', line 134

def include?(key)
  @config.include? key.to_s.tr('=', '').to_sym
end

#loaded!Object



122
123
124
# File 'lib/advanced_connection/config.rb', line 122

def loaded!
  synchronize { @loaded = true }
end

#max_idle_connectionsObject



209
210
211
# File 'lib/advanced_connection/config.rb', line 209

def max_idle_connections
  @config[:max_idle_connections]
end

#max_idle_connections=(value) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/advanced_connection/config.rb', line 213

def max_idle_connections=(value)
  unless value.is_a?(Numeric) || value =~ /^\d+$/
    fail Error::ConfigError, 'Expected max_idle_connections to be ' \
                       "a valid integer value, but found `#{value.inspect}`"
  end
  synchronize {
    @config[:max_idle_connections] = begin
      value.to_i
    rescue FloatDomainError
      raise unless $!.message =~ /infinity/i
      ::Float::INFINITY
    end
  }
end

#max_idle_timeObject



228
229
230
# File 'lib/advanced_connection/config.rb', line 228

def max_idle_time
  @config[:max_idle_time]
end

#max_idle_time=(value) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/advanced_connection/config.rb', line 232

def max_idle_time=(value)
  unless value.is_a?(Numeric) || value =~ /^\d+$/
    fail Error::ConfigError, 'Expected max_idle_time to be ' \
                       "a valid integer value, but found `#{value.inspect}`"
  end
  synchronize { @config[:max_idle_time] = value.to_i }
end

#min_idle_connectionsObject



200
201
202
203
# File 'lib/advanced_connection/config.rb', line 200

def min_idle_connections
  # deprecated and not used
  0
end

#min_idle_connections=(value) ⇒ Object



205
206
207
# File 'lib/advanced_connection/config.rb', line 205

def min_idle_connections=(value)
  # deprecated and not used
end

#to_hObject



138
139
140
# File 'lib/advanced_connection/config.rb', line 138

def to_h
  @config.dup
end

#warmup_connectionsObject



191
192
193
194
# File 'lib/advanced_connection/config.rb', line 191

def warmup_connections
  # deprecated and not used
  0
end

#warmup_connections=(value) ⇒ Object



196
197
198
# File 'lib/advanced_connection/config.rb', line 196

def warmup_connections=(value)
  # deprecated and not used
end