Class: ActiveRecord::Bogacs::Validator
- Inherits:
-
Object
- Object
- ActiveRecord::Bogacs::Validator
- Defined in:
- lib/active_record/bogacs/validator.rb
Overview
Do not use a reaper with the validator!
Every frequency seconds, the validator will perform connection validation on a pool it operates.
Configure the frequency by setting ‘:validate_frequency` in your AR configuration.
We recommend not setting values too low as that would drain the pool’s performance under heavy concurrent connection retrieval. Connections are also validated upon checkout - the validator is intended to detect long idle pooled connections “ahead of time” instead of upon retrieval.
Reaping (stale connection detection and removal) is part of the validation process and will only slow things down as all pool connection updates needs to be synchronized.
Defined Under Namespace
Modules: PoolAdaptor
Constant Summary collapse
- TimerTask =
::Concurrent::TimerTask
Instance Attribute Summary collapse
-
#frequency ⇒ Object
readonly
Returns the value of attribute frequency.
-
#pool ⇒ Object
readonly
Returns the value of attribute pool.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#initialize(pool, frequency = 60, timeout = nil) ⇒ Validator
constructor
‘Validator.new(pool, spec.config).run`.
- #run ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
- #validate ⇒ Object
Constructor Details
#initialize(pool, frequency = 60, timeout = nil) ⇒ Validator
‘Validator.new(pool, spec.config).run`
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_record/bogacs/validator.rb', line 35 def initialize(pool, frequency = 60, timeout = nil) @pool = pool; PoolAdaptor.adapt! pool if frequency # validate every 60s by default frequency = frequency.to_f @frequency = frequency > 0.0 ? frequency : false else @frequency = nil end if timeout timeout = timeout.to_f @timeout = timeout > 0.0 ? timeout : 0 else @timeout = @frequency end @running = nil end |
Instance Attribute Details
#frequency ⇒ Object (readonly)
Returns the value of attribute frequency.
31 32 33 |
# File 'lib/active_record/bogacs/validator.rb', line 31 def frequency @frequency end |
#pool ⇒ Object (readonly)
Returns the value of attribute pool.
31 32 33 |
# File 'lib/active_record/bogacs/validator.rb', line 31 def pool @pool end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
31 32 33 |
# File 'lib/active_record/bogacs/validator.rb', line 31 def timeout @timeout end |
Instance Method Details
#run ⇒ Object
52 53 54 55 |
# File 'lib/active_record/bogacs/validator.rb', line 52 def run return unless frequency @running = true; start end |
#running? ⇒ Boolean
66 |
# File 'lib/active_record/bogacs/validator.rb', line 66 def running?; @running end |
#start ⇒ Object
60 61 62 63 64 |
# File 'lib/active_record/bogacs/validator.rb', line 60 def start TimerTask.new(:execution_interval => frequency, :timeout_interval => timeout) do validate_connections end end |
#validate ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/active_record/bogacs/validator.rb', line 68 def validate start = Time.now conns = connections logger && logger.debug("[validator] found #{conns.size} candidates to validate") invalid = 0 conns.each { |connection| invalid += 1 if validate_connection(connection) == false } logger && logger.info("[validator] validated pool in #{Time.now - start}s (removed #{invalid} connections from pool)") invalid end |