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: @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
Adds new servers to the connection pool. 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 33 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, line 58 58: def all_connections 59: t = Thread.current 60: sync do 61: @allocated.values.each do |threads| 62: threads.each do |thread, conn| 63: yield conn if t == thread 64: end 65: end 66: @available_connections.values.each{|v| v.each{|c| yield c}} 67: end 68: 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 49 49: def allocated(server=:default) 50: @allocated[server] 51: 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 73 73: def available_connections(server=:default) 74: @available_connections[server] 75: 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 94 94: def disconnect(opts=OPTS) 95: (opts[:server] ? Array(opts[:server]) : sync{@servers.keys}).each do |s| 96: if conns = sync{disconnect_server_connections(s)} 97: disconnect_connections(conns) 98: end 99: end 100: end
# File lib/sequel/connection_pool/sharded_threaded.rb, line 102 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, 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: 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, line 168 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, line 143 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
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, line 80 80: def size(server=:default) 81: @mutex.synchronize{_size(server)} 82: end