class Sequel::TimedQueueConnectionPool
A connection pool allowing multi-threaded access to a pool of connections, using a timed queue (only available in Ruby 3.2+).
Attributes
The maximum number of connections this pool will create.
Public Class Methods
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
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)
Sequel::ConnectionPool::new
Public Instance Methods
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 = 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
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.
Source
# 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
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.
Source
# 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
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.
Source
# File lib/sequel/connection_pool/timed_queue.rb 108 def num_waiting(_server=:default) 109 @queue.num_waiting 110 end
The number of threads waiting to check out a connection.
Source
# File lib/sequel/connection_pool/timed_queue.rb 112 def pool_type 113 :timed_queue 114 end
Source
# File lib/sequel/connection_pool/timed_queue.rb 117 def size 118 sync{@size[0]} 119 end
The total number of connections in the pool.
Private Instance Methods
Source
# File lib/sequel/connection_pool/timed_queue.rb 227 def acquire(thread) 228 if conn = available || try_make_new || wait_until_available 229 sync{@allocated[thread] = conn} 230 else 231 name = db.opts[:name] 232 raise ::Sequel::PoolTimeout, "timeout: #{@timeout}#{", database name: #{name}" if name}" 233 end 234 end
Assigns a connection to the supplied thread, if one is available.
This should return a connection is one is available within the timeout, or raise PoolTimeout if a connection could not be acquired within the timeout.
Calling code should not have the mutex when calling this.
Source
# File lib/sequel/connection_pool/timed_queue.rb 238 def available 239 @queue.pop(timeout: 0) 240 end
Return the next connection in the pool if there is one available. Returns nil if no connection is currently available.
Source
# File lib/sequel/connection_pool/timed_queue.rb 181 def can_make_new?(current_size) 182 if @max_size > current_size 183 @size[0] += 1 184 end 185 end
Whether the given size is less than the maximum size of the pool. In that case, the pool’s current size is incremented. If this method returns true, space in the pool for the connection is preallocated, and preallocated_make_new should be called to create the connection.
Calling code should have the mutex when calling this.
Source
# File lib/sequel/connection_pool/timed_queue.rb 282 def checkin_connection(conn) 283 @queue.push(conn) 284 conn 285 end
Adds a connection to the queue of available connections, returns the connection.
Source
# File lib/sequel/connection_pool/timed_queue.rb 145 def disconnect_connection(conn) 146 sync{@size[0] -= 1} 147 super 148 end
Decrement the current size of the pool when disconnecting connections.
Calling code should not have the mutex when calling this.
Sequel::ConnectionPool#disconnect_connection
Source
# File lib/sequel/connection_pool/timed_queue.rb 164 def fill_queue(nconns) 165 if nconns > 0 && @queue.num_waiting > 0 166 Thread.new do 167 while nconns > 0 && @queue.num_waiting > 0 && (conn = try_make_new) 168 @queue.push(conn) 169 end 170 end 171 end 172 end
If there are any threads waiting on the queue, try to create new connections in a separate thread if the pool is not yet at the maximum size.
The reason for this method is to handle cases where acquire could not retrieve a connection immediately, and the pool was already at the maximum size. In that case, the acquire will wait on the queue until the timeout. This method is called after disconnecting to potentially add new connections to the pool, so the threads that are currently waiting for connections do not timeout after the pool is no longer full.
nconns specifies the maximum number of connections to add, which should be the number of connections that were disconnected.
Source
# File lib/sequel/connection_pool/timed_queue.rb 251 def owned_connection(thread) 252 sync{@allocated[thread]} 253 end
Returns the connection owned by the supplied thread, if any. The calling code should NOT already have the mutex before calling this.
Source
# File lib/sequel/connection_pool/timed_queue.rb 135 def preallocated_make_new 136 make_new(:default) 137 rescue Exception 138 sync{@size[0] -= 1} 139 raise 140 end
Create a new connection, after the pool’s current size has already been updated to account for the new connection. If there is an exception when creating the connection, decrement the current size.
This should only be called after can_make_new?. If there is an exception between when can_make_new? is called and when preallocated_make_new is called, it has the effect of reducing the maximum size of the connection pool by 1, since the current size of the pool will show a higher number than the number of connections allocated or in the queue.
Calling code should not have the mutex when calling this.
Source
# File lib/sequel/connection_pool/timed_queue.rb 259 def preconnect(concurrent = false) 260 if concurrent 261 if times = sync{@max_size > (size = @size[0]) ? @max_size - size : false} 262 Array.new(times){Thread.new{if conn = try_make_new; @queue.push(conn) end}}.map(&:value) 263 end 264 else 265 while conn = try_make_new 266 @queue.push(conn) 267 end 268 end 269 270 nil 271 end
Create the maximum number of connections immediately. This should not be called with a true argument unless no code is currently operating on the database.
Calling code should not have the mutex when calling this.
Source
# File lib/sequel/connection_pool/timed_queue.rb 276 def release(thread) 277 checkin_connection(sync{@allocated.delete(thread)}) 278 nil 279 end
Releases the connection assigned to the supplied thread back to the pool.
Calling code should not have the mutex when calling this.
Source
# File lib/sequel/connection_pool/timed_queue.rb 290 def sync 291 @mutex.synchronize{yield} 292 end
Yield to the block while inside the mutex.
Calling code should not have the mutex when calling this.
Source
# File lib/sequel/connection_pool/timed_queue.rb 192 def try_make_new 193 return preallocated_make_new if sync{can_make_new?(@size[0])} 194 195 to_disconnect = nil 196 do_make_new = false 197 198 sync do 199 current_size = @size[0] 200 @allocated.keys.each do |t| 201 unless t.alive? 202 (to_disconnect ||= []) << @allocated.delete(t) 203 current_size -= 1 204 end 205 end 206 207 do_make_new = true if can_make_new?(current_size) 208 end 209 210 begin 211 preallocated_make_new if do_make_new 212 ensure 213 if to_disconnect 214 to_disconnect.each{|conn| disconnect_connection(conn)} 215 fill_queue(to_disconnect.size) 216 end 217 end 218 end
Try to make a new connection if there is space in the pool. If the pool is already full, look for dead threads/fibers and disconnect the related connections.
Calling code should not have the mutex when calling this.
Source
# File lib/sequel/connection_pool/timed_queue.rb 245 def wait_until_available 246 @queue.pop(timeout: @timeout) 247 end
Return the next connection in the pool if there is one available. If not, wait until the timeout for a connection to become available. If there is still no available connection, return nil.