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 as reaping (stale connection detection

Every frequency seconds, the validator will perform connection validation on a pool it operates.

and removal) is part of the validation process and will only likely slow things down as all pool connection updates needs to be synchronized.

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.

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(self, 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

#frequencyObject (readonly)

Returns the value of attribute frequency.



31
32
33
# File 'lib/active_record/bogacs/validator.rb', line 31

def frequency
  @frequency
end

#poolObject (readonly)

Returns the value of attribute pool.



31
32
33
# File 'lib/active_record/bogacs/validator.rb', line 31

def pool
  @pool
end

#timeoutObject (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

#runObject



52
53
54
55
# File 'lib/active_record/bogacs/validator.rb', line 52

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

#running?Boolean

Returns:

  • (Boolean)


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

def running?; @running end

#startObject



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

#validateObject



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