module Sequel::ConnectionValidator
Attributes
connection_validation_timeout[RW]
The number of seconds that need to pass since connection checkin before attempting to validate the connection when checking it out from the pool. Defaults to 3600 seconds (1 hour).
Public Class Methods
extended(pool)
click to toggle source
Initialize the data structures used by this extension.
# File lib/sequel/extensions/connection_validator.rb, line 62 def self.extended(pool) case pool.pool_type when :single, :sharded_single raise Error, "cannot load connection_validator extension if using single or sharded_single connection pool" end pool.instance_exec do sync do @connection_timestamps ||= {} @connection_validation_timeout ||= 3600 end end # Make sure the valid connection SQL query is precached, # otherwise it's possible it will happen at runtime. While # it should work correctly at runtime, it's better to avoid # the possibility of failure altogether. pool.db.send(:valid_connection_sql) end
Private Instance Methods
acquire(*a)
click to toggle source
When acquiring a connection, if it has been idle for longer than the connection validation timeout, test the connection for validity. If it is not valid, disconnect the connection, and retry with a new connection.
Calls superclass method
# File lib/sequel/extensions/connection_validator.rb, line 101 def acquire(*a) conn = nil 1.times do if (conn = super) && (timer = sync{@connection_timestamps.delete(conn)}) && Sequel.elapsed_seconds_since(timer) > @connection_validation_timeout && !db.valid_connection?(conn) case pool_type when :sharded_threaded, :sharded_timed_queue sync{@allocated[a.last].delete(Sequel.current)} else sync{@allocated.delete(Sequel.current)} end disconnect_connection(conn) redo end end conn end
checkin_connection(*)
click to toggle source
Record the time the connection was checked back into the pool.
Calls superclass method
# File lib/sequel/extensions/connection_validator.rb, line 85 def checkin_connection(*) conn = super @connection_timestamps[conn] = Sequel.start_timer conn end
disconnect_connection(conn)
click to toggle source
Clean up timestamps during disconnect.
Calls superclass method
# File lib/sequel/extensions/connection_validator.rb, line 92 def disconnect_connection(conn) sync{@connection_timestamps.delete(conn)} super end