lib/sequel/extensions/connection_expiration.rb in sequel-5.7.1 vs lib/sequel/extensions/connection_expiration.rb in sequel-5.8.0

- old
+ new

@@ -36,16 +36,21 @@ # The number of seconds that need to pass since # connection creation before expiring a connection. # Defaults to 14400 seconds (4 hours). attr_accessor :connection_expiration_timeout + # The maximum number of seconds that will be added as a random delay to the expiration timeout + # Defaults to 0 seconds (no random delay). + attr_accessor :connection_expiration_random_delay + # Initialize the data structures used by this extension. def self.extended(pool) pool.instance_exec do sync do @connection_expiration_timestamps ||= {} @connection_expiration_timeout ||= 14400 + @connection_expiration_random_delay ||= 0 end end end private @@ -57,21 +62,21 @@ end # Record the time the connection was created. def make_new(*) conn = super - @connection_expiration_timestamps[conn] = Sequel.start_timer + @connection_expiration_timestamps[conn] = [Sequel.start_timer, @connection_expiration_timeout + (rand * @connection_expiration_random_delay)].freeze conn end # When acquiring a connection, check if the connection is expired. # If it is expired, disconnect the connection, and retry with a new # connection. def acquire(*a) begin if (conn = super) && - (timer = sync{@connection_expiration_timestamps[conn]}) && - Sequel.elapsed_seconds_since(timer) > @connection_expiration_timeout + (cet = sync{@connection_expiration_timestamps[conn]}) && + Sequel.elapsed_seconds_since(cet[0]) > cet[1] if pool_type == :sharded_threaded sync{allocated(a.last).delete(Thread.current)} else sync{@allocated.delete(Thread.current)}