Module: Sequel::ConnectionExpiration

Defined in:
lib/sequel/extensions/connection_expiration.rb

Defined Under Namespace

Classes: Retry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#connection_expiration_random_delayObject

The maximum number of seconds that will be added as a random delay to the expiration timeout Defaults to 0 seconds (no random delay).



44
45
46
# File 'lib/sequel/extensions/connection_expiration.rb', line 44

def connection_expiration_random_delay
  @connection_expiration_random_delay
end

#connection_expiration_timeoutObject

The number of seconds that need to pass since connection creation before expiring a connection. Defaults to 14400 seconds (4 hours).



40
41
42
# File 'lib/sequel/extensions/connection_expiration.rb', line 40

def connection_expiration_timeout
  @connection_expiration_timeout
end

Class Method Details

.extended(pool) ⇒ Object

Initialize the data structures used by this extension.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sequel/extensions/connection_expiration.rb', line 47

def self.extended(pool)
  case pool.pool_type
  when :threaded, :sharded_threaded, :timed_queue, :sharded_timed_queue
    nil
  else
    raise Error, "cannot load connection_expiration extension if using single or sharded_single connection pool (or other unsupported connection pool)"
  end

  pool.instance_exec do
    sync do
      @connection_expiration_timestamps ||= {}
      @connection_expiration_timeout ||= 14400
      @connection_expiration_random_delay ||= 0

      # Record an expiration timestamp for any connections that already
      # exist in the pool, so that a connection opened before the extension
      # was loaded (e.g. via Sequel.connect) will eventually be expired.
      register = method(:register_connection_expiration_time)

      case pool_type
      when :timed_queue, :sharded_timed_queue
        register_queued_connections = lambda do |queue|
          conns = []
          while conn = queue.pop(timeout: 0)
            conns << conn
          end
          conns.each do |conn|
            queue.push(register_connection_expiration_time(conn))
          end
        end
      end

      case pool_type
      when :threaded
        @available_connections.each(&register)
        @allocated.each_value(&register)
      when :sharded_threaded
        @available_connections.each_value{|conns| conns.each(&register)}
        @allocated.each_value{|threads| threads.each_value(&register)}
      when :timed_queue
        register_queued_connections.call(@queue)
        @allocated.each_value(&register)
      else # when :sharded_timed_queue
        @queues.each_value(&register_queued_connections)
        @allocated.each_value{|threads| threads.each_value(&register)}
      end
    end
  end
end