Class: ActiveRecord::Bogacs::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/bogacs/validator.rb

Overview

Note:

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

Instance Method Summary collapse

Constructor Details

#initialize(pool, frequency = 60, timeout = nil) ⇒ Validator

‘Validator.new(pool, spec.config).run`



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_record/bogacs/validator.rb', line 30

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

#frequencyObject (readonly)

Returns the value of attribute frequency.



26
27
28
# File 'lib/active_record/bogacs/validator.rb', line 26

def frequency
  @frequency
end

#poolObject (readonly)

Returns the value of attribute pool.



26
27
28
# File 'lib/active_record/bogacs/validator.rb', line 26

def pool
  @pool
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



26
27
28
# File 'lib/active_record/bogacs/validator.rb', line 26

def timeout
  @timeout
end

Instance Method Details

#runObject



47
48
49
50
# File 'lib/active_record/bogacs/validator.rb', line 47

def run
  return unless frequency
  @running = true; start
end

#running?Boolean

Returns:

  • (Boolean)


61
# File 'lib/active_record/bogacs/validator.rb', line 61

def running?; @running end

#startObject



55
56
57
58
59
# File 'lib/active_record/bogacs/validator.rb', line 55

def start
  TimerTask.new(:execution_interval => frequency, :timeout_interval => timeout) do
    validate_connections
  end
end

#validateObject



63
64
65
66
67
68
69
70
71
# File 'lib/active_record/bogacs/validator.rb', line 63

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