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
63 def self.extended(pool)
64   case pool.pool_type
65   when :single, :sharded_single
66     raise Error, "cannot load connection_validator extension if using single or sharded_single connection pool"
67   end
68 
69   pool.instance_exec do
70     sync do
71       @connection_timestamps ||= {}
72       @connection_validation_timeout ||= 3600
73     end
74   end
75 
76   # Make sure the valid connection SQL query is precached,
77   # otherwise it's possible it will happen at runtime. While
78   # it should work correctly at runtime, it's better to avoid
79   # the possibility of failure altogether.
80   pool.db.send(:valid_connection_sql)
81 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
102 def acquire(*a)
103   conn = nil
104 
105   1.times do
106     if (conn = super) &&
107        (timer = sync{@connection_timestamps.delete(conn)}) &&
108        Sequel.elapsed_seconds_since(timer) > @connection_validation_timeout &&
109        !db.valid_connection?(conn)
110 
111       case pool_type
112       when :sharded_threaded, :sharded_timed_queue
113         sync{@allocated[a.last].delete(Sequel.current)}
114       else
115         sync{@allocated.delete(Sequel.current)}
116       end
117 
118       disconnect_connection(conn)
119       redo
120     end
121   end
122 
123   conn
124 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
86 def checkin_connection(*)
87   conn = super
88   @connection_timestamps[conn] = Sequel.start_timer
89   conn
90 end
disconnect_connection(conn) click to toggle source

Clean up timestamps during disconnect.

Calls superclass method
   # File lib/sequel/extensions/connection_validator.rb
93 def disconnect_connection(conn)
94   sync{@connection_timestamps.delete(conn)}
95   super
96 end