module Sequel::ConnectionExpiration

Attributes

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, line 42
def self.extended(pool)
  pool.instance_eval do
    sync do
      @connection_expiration_timestamps ||= {}
      @connection_expiration_timeout ||= 14400
    end
  end
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, line 69
def acquire(*a)
  begin
    if (conn = super) &&
       (t = sync{@connection_expiration_timestamps[conn]}) &&
       Time.now - t > @connection_expiration_timeout

      if pool_type == :sharded_threaded
        sync{allocated(a.last).delete(Thread.current)}
      else
        sync{@allocated.delete(Thread.current)}
      end

      disconnect_connection(conn)
      raise Retry
    end
  rescue Retry
    retry
  end

  conn
end
disconnect_connection(conn) click to toggle source

Clean up expiration timestamps during disconnect.

Calls superclass method
# File lib/sequel/extensions/connection_expiration.rb, line 54
def disconnect_connection(conn)
  sync{@connection_expiration_timestamps.delete(conn)}
  super
end
make_new(*) click to toggle source

Record the time the connection was created.

Calls superclass method
# File lib/sequel/extensions/connection_expiration.rb, line 60
def make_new(*)
  conn = super
  @connection_expiration_timestamps[conn] = Time.now
  conn
end