Class | Sequel::ShardedSingleConnectionPool |
In: |
lib/sequel/connection_pool/sharded_single.rb
|
Parent: | Sequel::ConnectionPool |
A ShardedSingleConnectionPool is a single threaded connection pool that works with multiple shards/servers.
The single threaded pool takes the following options:
: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_single.rb, line 14 14: def initialize(db, opts=OPTS) 15: super 16: @conns = {} 17: @servers = opts.fetch(:servers_hash, Hash.new(:default)) 18: add_servers([:default]) 19: add_servers(opts[:servers].keys) if opts[:servers] 20: 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_single.rb, line 25 25: def add_servers(servers) 26: servers.each{|s| @servers[s] = s} 27: end
Yield all of the currently established connections
# File lib/sequel/connection_pool/sharded_single.rb, line 30 30: def all_connections 31: @conns.values.each{|c| yield c} 32: end
The connection for the given server.
# File lib/sequel/connection_pool/sharded_single.rb, line 35 35: def conn(server=:default) 36: @conns[@servers[server]] 37: end
Disconnects from the database. Once a connection is requested using hold, the connection is reestablished. 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_single.rb, line 43 43: def disconnect(opts=OPTS) 44: (opts[:server] ? Array(opts[:server]) : servers).each{|s| disconnect_server(s)} 45: end
Yields the connection to the supplied block for the given server. This method simulates the ConnectionPool#hold API.
# File lib/sequel/connection_pool/sharded_single.rb, line 49 49: def hold(server=:default) 50: begin 51: server = pick_server(server) 52: yield(@conns[server] ||= make_new(server)) 53: rescue Sequel::DatabaseDisconnectError, *@error_classes => e 54: disconnect_server(server) if disconnect_error?(e) 55: raise 56: end 57: end
The ShardedSingleConnectionPool always has a maximum size of 1.
# File lib/sequel/connection_pool/sharded_single.rb, line 60 60: def max_size 61: 1 62: end
# File lib/sequel/connection_pool/sharded_single.rb, line 86 86: def pool_type 87: :sharded_single 88: 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_single.rb, line 68 68: def remove_servers(servers) 69: raise(Sequel::Error, "cannot remove default server") if servers.include?(:default) 70: servers.each do |server| 71: disconnect_server(server) 72: @servers.delete(server) 73: end 74: end