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
Public Instance
Attributes
| max_size | [R] |
The maximum number of connections this pool will create. |
Public Class methods
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) |
# 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
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.
# 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 = available) && !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
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.
# File lib/sequel/connection_pool/timed_queue.rb 61 def disconnect(opts=OPTS) 62 nconns = 0 63 while conn = available 64 nconns += 1 65 disconnect_connection(conn) 66 end 67 fill_queue(nconns) 68 nil 69 end
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.
# File lib/sequel/connection_pool/timed_queue.rb 84 def hold(server=nil) 85 t = Sequel.current 86 if conn = owned_connection(t) 87 return yield(conn) 88 end 89 90 begin 91 conn = acquire(t) 92 yield conn 93 rescue Sequel::DatabaseDisconnectError, *@error_classes => e 94 if disconnect_error?(e) 95 oconn = conn 96 conn = nil 97 disconnect_connection(oconn) if oconn 98 sync{@allocated.delete(t)} 99 fill_queue(1) 100 end 101 raise 102 ensure 103 release(t) if conn 104 end 105 end
The number of threads waiting to check out a connection.
# File lib/sequel/connection_pool/timed_queue.rb 108 def num_waiting(_server=:default) 109 @queue.num_waiting 110 end
# File lib/sequel/connection_pool/timed_queue.rb 112 def pool_type 113 :timed_queue 114 end
The total number of connections in the pool.
# File lib/sequel/connection_pool/timed_queue.rb 117 def size 118 sync{@size[0]} 119 end