lib/advanced_connection/config.rb in advanced_connection-0.5.6 vs lib/advanced_connection/config.rb in advanced_connection-0.5.7

- old
+ new

@@ -18,10 +18,11 @@ # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # require 'singleton' +require 'monitor' module AdvancedConnection class Config include Singleton @@ -97,21 +98,28 @@ alias_method :loaded?, :loaded def initialize @loaded = false @config = DEFAULT_CONFIG.deep_dup + @mutex = Monitor.new end + def synchronize + @mutex.synchronize { yield } + end + private :synchronize + def can_enable? - # don't enable if we're running rake tasks, in particular db: or assets: tasks + # 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 def loaded! - @loaded = true + synchronize { @loaded = true } end def [](key) @config[key.to_sym] end @@ -138,30 +146,30 @@ def enable_without_connection=(value) if enable_statement_pooling && !!value raise Error::ConfigError, "WithoutConnection blocks conflict with Statement Pooling feature" end - @config[:enable_without_connection] = !!value + synchronize { @config[:enable_without_connection] = !!value } end def enable_statement_pooling @config[:enable_statement_pooling] end def enable_statement_pooling=(value) if enable_without_connection && !!value raise Error::ConfigError, "Statement Pooling conflicts with WithoutConnection feature" end - @config[:enable_statement_pooling] = !!value + synchronize { @config[:enable_statement_pooling] = !!value } end def enable_idle_connection_manager @config[:enable_idle_connection_manager] end def enable_idle_connection_manager=(value) - @config[:enable_idle_connection_manager] = !!value + synchronize { @config[:enable_idle_connection_manager] = !!value } end def warmup_connections @config[:warmup_connections] end @@ -170,11 +178,11 @@ unless !!value || value.is_a?(Fixnum) || value =~ /^\d+$/ fail Error::ConfigError, 'Expected warmup_connections to be nil, false ' \ "or a valid positive integer, but found `#{value.inspect}`" end - @config[:warmup_connections] = value.to_s =~ /^\d+$/ ? value.to_i : false + synchronize { @config[:warmup_connections] = value.to_s =~ /^\d+$/ ? value.to_i : false } end def min_idle_connections @config[:min_idle_connections] end @@ -182,11 +190,11 @@ def min_idle_connections=(value) unless value.is_a?(Numeric) || value =~ /^\d+$/ fail Error::ConfigError, 'Expected min_idle_connections to be ' \ "a valid integer value, but found `#{value.inspect}`" end - @config[:min_idle_connections] = value.to_i + synchronize { @config[:min_idle_connections] = value.to_i } end def max_idle_connections @config[:max_idle_connections] end @@ -194,16 +202,18 @@ 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 - @config[:max_idle_connections] = begin - value.to_i - rescue FloatDomainError - raise unless $!.message =~ /infinity/i - ::Float::INFINITY - end + synchronize { + @config[:max_idle_connections] = begin + value.to_i + rescue FloatDomainError + raise unless $!.message =~ /infinity/i + ::Float::INFINITY + end + } end def max_idle_time @config[:max_idle_time] end @@ -211,11 +221,11 @@ 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 - @config[:max_idle_time] = value.to_i + synchronize { @config[:max_idle_time] = value.to_i } end def idle_check_interval @config[:idle_check_interval] end @@ -223,11 +233,11 @@ 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 - @config[:idle_check_interval] = value.to_i + synchronize { @config[:idle_check_interval] = value.to_i } end def connection_pool_queue_type @config[:connection_pool_queue_type] end @@ -241,9 +251,9 @@ 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 - @config[:connection_pool_queue_type] = value + synchronize { @config[:connection_pool_queue_type] = value } end end end