Class | Sequel::ShardedThreadedConnectionPool |
In: |
lib/sequel/connection_pool/sharded_threaded.rb
|
Parent: | Sequel::ThreadedConnectionPool |
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.
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, Sequel uses Hash.new(:default). You can use a hash with a default proc that raises an error if you want to catch all cases where a nonexistent server is used. |
# File lib/sequel/connection_pool/sharded_threaded.rb, line 18 18: def initialize(db, opts = OPTS) 19: super 20: @available_connections = {} 21: @connections_to_remove = [] 22: @servers = opts.fetch(:servers_hash, Hash.new(:default)) 23: 24: if USE_WAITER 25: @waiter = nil 26: @waiters = {} 27: end 28: 29: add_servers([:default]) 30: add_servers(opts[:servers].keys) if opts[:servers] 31: end
Adds new servers to the connection pool. Primarily used in conjunction with master/slave or shard configurations. Allows for dynamic expansion of the potential slaves/shards at runtime. servers argument should be an array of symbols.
# File lib/sequel/connection_pool/sharded_threaded.rb, line 36 36: def add_servers(servers) 37: sync do 38: servers.each do |server| 39: unless @servers.has_key?(server) 40: @servers[server] = server 41: @available_connections[server] = [] 42: @allocated[server] = {} 43: @waiters[server] = ConditionVariable.new if USE_WAITER 44: end 45: end 46: end 47: 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, line 61 61: def all_connections 62: t = Thread.current 63: sync do 64: @allocated.values.each do |threads| 65: threads.each do |thread, conn| 66: yield conn if t == thread 67: end 68: end 69: @available_connections.values.each{|v| v.each{|c| yield c}} 70: end 71: 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.
# File lib/sequel/connection_pool/sharded_threaded.rb, line 52 52: def allocated(server=:default) 53: @allocated[server] 54: 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.
# File lib/sequel/connection_pool/sharded_threaded.rb, line 76 76: def available_connections(server=:default) 77: @available_connections[server] 78: 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, line 98 98: def disconnect(opts=OPTS) 99: (opts[:server] ? Array(opts[:server]) : sync{@servers.keys}).each do |s| 100: if conns = sync{disconnect_server_connections(s)} 101: disconnect_connections(conns) 102: end 103: end 104: 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, line 120 120: def hold(server=:default) 121: server = pick_server(server) 122: t = Thread.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: end 135: end
# File lib/sequel/connection_pool/sharded_threaded.rb, line 166 166: def pool_type 167: :sharded_threaded 168: end
Remove servers from the connection pool. Primarily used in conjunction with master/slave or shard configurations. 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, line 141 141: def remove_servers(servers) 142: conns = nil 143: sync do 144: raise(Sequel::Error, "cannot remove default server") if servers.include?(:default) 145: servers.each do |server| 146: if @servers.include?(server) 147: conns = disconnect_server_connections(server) 148: @waiters.delete(server) if USE_WAITER 149: @available_connections.delete(server) 150: @allocated.delete(server) 151: @servers.delete(server) 152: end 153: end 154: end 155: 156: if conns 157: disconnect_connections(conns) 158: end 159: end
The total number of connections opened for the given server, should be equal to available_connections.length + allocated.length. Nonexistent servers will return the created count of the default server.
# File lib/sequel/connection_pool/sharded_threaded.rb, line 83 83: def size(server=:default) 84: server = @servers[server] 85: @allocated[server].length + @available_connections[server].length 86: end