module Sequel::ConnectionExpiration
Attributes
connection_expiration_random_delay[RW]
The maximum number of seconds that will be added as a random delay to the expiration timeout Defaults to 0 seconds (no random delay).
connection_expiration_timeout[RW]
The number of seconds that need to pass since connection creation before expiring a connection. Defaults to 14400 seconds (4 hours).
Public Class Methods
extended(pool)
click to toggle source
Initialize the data structures used by this extension.
# File lib/sequel/extensions/connection_expiration.rb 47 def self.extended(pool) 48 pool.instance_exec do 49 sync do 50 @connection_expiration_timestamps ||= {} 51 @connection_expiration_timeout ||= 14400 52 @connection_expiration_random_delay ||= 0 53 end 54 end 55 end
Private Instance Methods
acquire(*a)
click to toggle source
When acquiring a connection, check if the connection is expired. If it is expired, disconnect the connection, and retry with a new connection.
Calls superclass method
# File lib/sequel/extensions/connection_expiration.rb 75 def acquire(*a) 76 conn = nil 77 1.times do 78 if (conn = super) && 79 (cet = sync{@connection_expiration_timestamps[conn]}) && 80 Sequel.elapsed_seconds_since(cet[0]) > cet[1] 81 82 if pool_type == :sharded_threaded 83 sync{allocated(a.last).delete(Thread.current)} 84 else 85 sync{@allocated.delete(Thread.current)} 86 end 87 88 disconnect_connection(conn) 89 redo 90 end 91 end 92 93 conn 94 end
disconnect_connection(conn)
click to toggle source
Clean up expiration timestamps during disconnect.
Calls superclass method
# File lib/sequel/extensions/connection_expiration.rb 60 def disconnect_connection(conn) 61 sync{@connection_expiration_timestamps.delete(conn)} 62 super 63 end
make_new(*)
click to toggle source
Record the time the connection was created.
Calls superclass method
# File lib/sequel/extensions/connection_expiration.rb 66 def make_new(*) 67 conn = super 68 @connection_expiration_timestamps[conn] = [Sequel.start_timer, @connection_expiration_timeout + (rand * @connection_expiration_random_delay)].freeze 69 conn 70 end