class Sequel::TimedQueueConnectionPool

  1. lib/sequel/connection_pool/timed_queue.rb
Superclass: ConnectionPool

A connection pool allowing multi-threaded access to a pool of connections, using a timed queue (only available in Ruby 3.2+).

Methods

Public Class

  1. new

Public Instance

  1. all_connections
  2. disconnect
  3. hold
  4. max_size
  5. pool_type
  6. size

Attributes

max_size [R]

The maximum number of connections this pool will create.

Public Class methods

new(db, opts = OPTS)

The following additional options are respected:

:max_connections

The maximum number of connections the connection pool will open (default 4)

:pool_timeout

The amount of seconds to wait to acquire a connection before raising a PoolTimeout (default 5)

[show source]
   # File lib/sequel/connection_pool/timed_queue.rb
18 def initialize(db, opts = OPTS)
19   super
20   @max_size = Integer(opts[:max_connections] || 4)
21   raise(Sequel::Error, ':max_connections must be positive') if @max_size < 1
22   @mutex = Mutex.new  
23   # Size inside array so this still works while the pool is frozen.
24   @size = [0]
25   @allocated = {}
26   @allocated.compare_by_identity
27   @timeout = Float(opts[:pool_timeout] || 5)
28   @queue = Queue.new
29 end

Public Instance methods

all_connections()

Yield all of the available connections, and the one currently allocated to this thread. This will not yield connections currently allocated to other threads, as it is not safe to operate on them.

[show source]
   # File lib/sequel/connection_pool/timed_queue.rb
34 def all_connections
35   hold do |conn|
36     yield conn
37 
38     # Use a hash to record all connections already seen.  As soon as we
39     # come across a connection we've already seen, we stop the loop.
40     conns = {}
41     conns.compare_by_identity
42     while true
43       conn = nil
44       begin
45         break unless (conn = @queue.pop(timeout: 0)) && !conns[conn]
46         conns[conn] = true
47         yield conn
48       ensure
49         @queue.push(conn) if conn
50       end
51     end
52   end
53 end
disconnect(opts=OPTS)

Removes all connections currently in the pool’s queue. This method has the effect of disconnecting from the database, assuming that no connections are currently being used.

Once a connection is requested using hold, the connection pool creates new connections to the database.

[show source]
   # File lib/sequel/connection_pool/timed_queue.rb
61 def disconnect(opts=OPTS)
62   while conn = @queue.pop(timeout: 0)
63     disconnect_connection(conn)
64   end
65   fill_queue
66   nil
67 end
hold(server=nil)

Chooses the first available connection, or if none are available, creates a new connection. Passes the connection to the supplied block:

pool.hold {|conn| conn.execute('DROP TABLE posts')}

Pool#hold is re-entrant, meaning it can be called recursively in the same thread without blocking.

If no connection is immediately available and the pool is already using the maximum number of connections, Pool#hold will block until a connection is available or the timeout expires. If the timeout expires before a connection can be acquired, a Sequel::PoolTimeout is raised.

[show source]
    # File lib/sequel/connection_pool/timed_queue.rb
 82 def hold(server=nil)
 83   t = Sequel.current
 84   if conn = owned_connection(t)
 85     return yield(conn)
 86   end
 87 
 88   begin
 89     conn = acquire(t)
 90     yield conn
 91   rescue Sequel::DatabaseDisconnectError, *@error_classes => e
 92     if disconnect_error?(e)
 93       oconn = conn
 94       conn = nil
 95       disconnect_connection(oconn) if oconn
 96       sync{@allocated.delete(t)}
 97       fill_queue
 98     end
 99     raise
100   ensure
101     release(t) if conn
102   end
103 end
pool_type()
[show source]
    # File lib/sequel/connection_pool/timed_queue.rb
105 def pool_type
106   :timed_queue
107 end
size()

The total number of connections in the pool.

[show source]
    # File lib/sequel/connection_pool/timed_queue.rb
110 def size
111   sync{@size[0]}
112 end