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 case pool.pool_type 49 when :single, :sharded_single 50 raise Error, "cannot load connection_expiration extension if using single or sharded_single connection pool" 51 end 52 53 pool.instance_exec do 54 sync do 55 @connection_expiration_timestamps ||= {} 56 @connection_expiration_timeout ||= 14400 57 @connection_expiration_random_delay ||= 0 58 end 59 end 60 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 80 def acquire(*a) 81 conn = nil 82 1.times do 83 if (conn = super) && 84 (cet = sync{@connection_expiration_timestamps[conn]}) && 85 Sequel.elapsed_seconds_since(cet[0]) > cet[1] 86 87 case pool_type 88 when :sharded_threaded, :sharded_timed_queue 89 sync{@allocated[a.last].delete(Sequel.current)} 90 else 91 sync{@allocated.delete(Sequel.current)} 92 end 93 94 disconnect_connection(conn) 95 redo 96 end 97 end 98 99 conn 100 end
disconnect_connection(conn)
click to toggle source
Clean up expiration timestamps during disconnect.
Calls superclass method
# File lib/sequel/extensions/connection_expiration.rb 65 def disconnect_connection(conn) 66 sync{@connection_expiration_timestamps.delete(conn)} 67 super 68 end
make_new(*)
click to toggle source
Record the time the connection was created.
Calls superclass method
# File lib/sequel/extensions/connection_expiration.rb 71 def make_new(*) 72 conn = super 73 @connection_expiration_timestamps[conn] = [Sequel.start_timer, @connection_expiration_timeout + (rand * @connection_expiration_random_delay)].freeze 74 conn 75 end