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 = Thread.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 if conns = sync{disconnect_server_connections(s)} 99 disconnect_connections(conns) 100 end 101 end 102 end
# File lib/sequel/connection_pool/sharded_threaded.rb 104 def freeze 105 @servers.freeze 106 super 107 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 122 def hold(server=:default) 123 server = pick_server(server) 124 t = Thread.current 125 if conn = owned_connection(t, server) 126 return yield(conn) 127 end 128 begin 129 conn = acquire(t, server) 130 yield conn 131 rescue Sequel::DatabaseDisconnectError, *@error_classes => e 132 sync{@connections_to_remove << conn} if conn && disconnect_error?(e) 133 raise 134 ensure 135 sync{release(t, conn, server)} if conn 136 while dconn = sync{@connections_to_disconnect.shift} 137 disconnect_connection(dconn) 138 end 139 end 140 end
# File lib/sequel/connection_pool/sharded_threaded.rb 170 def pool_type 171 :sharded_threaded 172 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 145 def remove_servers(servers) 146 conns = nil 147 sync do 148 raise(Sequel::Error, "cannot remove default server") if servers.include?(:default) 149 servers.each do |server| 150 if @servers.include?(server) 151 conns = disconnect_server_connections(server) 152 @waiters.delete(server) 153 @available_connections.delete(server) 154 @allocated.delete(server) 155 @servers.delete(server) 156 end 157 end 158 end 159 160 if conns 161 disconnect_connections(conns) 162 end 163 end
Return an array of symbols for servers in the connection pool.
# File lib/sequel/connection_pool/sharded_threaded.rb 166 def servers 167 sync{@servers.keys} 168 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