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: remove_instance_variable(:@waiter) 24: @waiters = {} 25: 26: add_servers([:default]) 27: add_servers(opts[:servers].keys) if opts[:servers] 28: 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 32 32: def add_servers(servers) 33: sync do 34: servers.each do |server| 35: unless @servers.has_key?(server) 36: @servers[server] = server 37: @available_connections[server] = [] 38: @allocated[server] = {} 39: @waiters[server] = ConditionVariable.new 40: end 41: end 42: end 43: 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 57 57: def all_connections 58: t = Thread.current 59: sync do 60: @allocated.values.each do |threads| 61: threads.each do |thread, conn| 62: yield conn if t == thread 63: end 64: end 65: @available_connections.values.each{|v| v.each{|c| yield c}} 66: end 67: 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 48 48: def allocated(server=:default) 49: @allocated[server] 50: 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 72 72: def available_connections(server=:default) 73: @available_connections[server] 74: 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 93 93: def disconnect(opts=OPTS) 94: (opts[:server] ? Array(opts[:server]) : sync{@servers.keys}).each do |s| 95: if conns = sync{disconnect_server_connections(s)} 96: disconnect_connections(conns) 97: end 98: end 99: end
# File lib/sequel/connection_pool/sharded_threaded.rb, line 101 101: def freeze 102: @servers.freeze 103: super 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 119 119: def hold(server=:default) 120: server = pick_server(server) 121: t = Thread.current 122: if conn = owned_connection(t, server) 123: return yield(conn) 124: end 125: begin 126: conn = acquire(t, server) 127: yield conn 128: rescue Sequel::DatabaseDisconnectError, *@error_classes => e 129: sync{@connections_to_remove << conn} if conn && disconnect_error?(e) 130: raise 131: ensure 132: sync{release(t, conn, server)} if conn 133: end 134: end
# File lib/sequel/connection_pool/sharded_threaded.rb, line 164 164: def pool_type 165: :sharded_threaded 166: 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 139 139: def remove_servers(servers) 140: conns = nil 141: sync do 142: raise(Sequel::Error, "cannot remove default server") if servers.include?(:default) 143: servers.each do |server| 144: if @servers.include?(server) 145: conns = disconnect_server_connections(server) 146: @waiters.delete(server) 147: @available_connections.delete(server) 148: @allocated.delete(server) 149: @servers.delete(server) 150: end 151: end 152: end 153: 154: if conns 155: disconnect_connections(conns) 156: end 157: 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 79 79: def size(server=:default) 80: @mutex.synchronize{_size(server)} 81: end