The slowest and most advanced connection, dealing with both multi-threaded access and configurations with multiple shards/servers.
In addition, this pool subclass also handles scheduling in-use connections to be removed from the pool when they are returned to it.
Methods
Public Class
Public Instance
Public Class methods
The following additional options are respected:
:servers |
A hash of servers to use. Keys should be symbols. If not present, will use a single :default server. |
:servers_hash |
The base hash to use for the servers. By default, |
# File lib/sequel/connection_pool/sharded_threaded.rb 18 def initialize(db, opts = OPTS) 19 super 20 @available_connections = {} 21 @connections_to_remove = [] 22 @connections_to_disconnect = [] 23 @servers = opts.fetch(:servers_hash, Hash.new(:default)) 24 remove_instance_variable(:@waiter) 25 @waiters = {} 26 27 add_servers([:default]) 28 add_servers(opts[:servers].keys) if opts[:servers] 29 end
Public Instance methods
Adds new servers to the connection pool. Allows for dynamic expansion of the potential replicas/shards at runtime. servers
argument should be an array of symbols.
# File lib/sequel/connection_pool/sharded_threaded.rb 33 def add_servers(servers) 34 sync do 35 servers.each do |server| 36 unless @servers.has_key?(server) 37 @servers[server] = server 38 @available_connections[server] = [] 39 @allocated[server] = {} 40 @waiters[server] = ConditionVariable.new 41 end 42 end 43 end 44 end
Yield all of the available connections, and the ones currently allocated to this thread. This will not yield connections currently allocated to other threads, as it is not safe to operate on them. This holds the mutex while it is yielding all of the connections, which means that until the method’s block returns, the pool is locked.
# File lib/sequel/connection_pool/sharded_threaded.rb 59 def all_connections 60 t = Sequel.current 61 sync do 62 @allocated.values.each do |threads| 63 threads.each do |thread, conn| 64 yield conn if t == thread 65 end 66 end 67 @available_connections.values.each{|v| v.each{|c| yield c}} 68 end 69 end
A hash of connections currently being used for the given server, key is the Thread, value is the connection. Nonexistent servers will return nil. Treat this as read only, do not modify the resulting object. The calling code should already have the mutex before calling this.
# File lib/sequel/connection_pool/sharded_threaded.rb 50 def allocated(server=:default) 51 @allocated[server] 52 end
An array of connections opened but not currently used, for the given server. Nonexistent servers will return nil. Treat this as read only, do not modify the resulting object. The calling code should already have the mutex before calling this.
# File lib/sequel/connection_pool/sharded_threaded.rb 75 def available_connections(server=:default) 76 @available_connections[server] 77 end
Removes all connections currently available on all servers, optionally yielding each connection to the given block. This method has the effect of disconnecting from the database, assuming that no connections are currently being used. If connections are being used, they are scheduled to be disconnected as soon as they are returned to the pool.
Once a connection is requested using hold
, the connection pool creates new connections to the database. Options:
:server |
Should be a symbol specifing the server to disconnect from, or an array of symbols to specify multiple servers. |
# File lib/sequel/connection_pool/sharded_threaded.rb 96 def disconnect(opts=OPTS) 97 (opts[:server] ? Array(opts[:server]) : sync{@servers.keys}).each do |s| 98 disconnect_connections(sync{disconnect_server_connections(s)}) 99 end 100 end
# File lib/sequel/connection_pool/sharded_threaded.rb 102 def freeze 103 @servers.freeze 104 super 105 end
Chooses the first available connection to the given server, 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/sharded_threaded.rb 120 def hold(server=:default) 121 server = pick_server(server) 122 t = Sequel.current 123 if conn = owned_connection(t, server) 124 return yield(conn) 125 end 126 begin 127 conn = acquire(t, server) 128 yield conn 129 rescue Sequel::DatabaseDisconnectError, *@error_classes => e 130 sync{@connections_to_remove << conn} if conn && disconnect_error?(e) 131 raise 132 ensure 133 sync{release(t, conn, server)} if conn 134 while dconn = sync{@connections_to_disconnect.shift} 135 disconnect_connection(dconn) 136 end 137 end 138 end
# File lib/sequel/connection_pool/sharded_threaded.rb 168 def pool_type 169 :sharded_threaded 170 end
Remove servers from the connection pool. Similar to disconnecting from all given servers, except that after it is used, future requests for the server will use the :default server instead.
# File lib/sequel/connection_pool/sharded_threaded.rb 143 def remove_servers(servers) 144 conns = nil 145 sync do 146 raise(Sequel::Error, "cannot remove default server") if servers.include?(:default) 147 servers.each do |server| 148 if @servers.include?(server) 149 conns = disconnect_server_connections(server) 150 @waiters.delete(server) 151 @available_connections.delete(server) 152 @allocated.delete(server) 153 @servers.delete(server) 154 end 155 end 156 end 157 158 if conns 159 disconnect_connections(conns) 160 end 161 end
Return an array of symbols for servers in the connection pool.
# File lib/sequel/connection_pool/sharded_threaded.rb 164 def servers 165 sync{@servers.keys} 166 end
The total number of connections opened for the given server. Nonexistent servers will return the created count of the default server. The calling code should NOT have the mutex before calling this.
# File lib/sequel/connection_pool/sharded_threaded.rb 82 def size(server=:default) 83 @mutex.synchronize{_size(server)} 84 end