Class: Sequel::ShardedSingleConnectionPool
- Inherits:
-
ConnectionPool
- Object
- ConnectionPool
- Sequel::ShardedSingleConnectionPool
- Defined in:
- lib/sequel/connection_pool/sharded_single.rb
Overview
A ShardedSingleConnectionPool is a single threaded connection pool that works with multiple shards/servers.
Constant Summary
Constants inherited from ConnectionPool
ConnectionPool::CONNECTION_POOL_MAP, ConnectionPool::DEFAULT_SERVER, ConnectionPool::OPTS
Instance Attribute Summary
Attributes inherited from ConnectionPool
Instance Method Summary collapse
-
#add_servers(servers) ⇒ Object
Adds new servers to the connection pool.
-
#all_connections ⇒ Object
Yield all of the currently established connections.
-
#conn(server = :default) ⇒ Object
The connection for the given server.
-
#disconnect(opts = OPTS) ⇒ Object
Disconnects from the database.
-
#hold(server = :default) ⇒ Object
Yields the connection to the supplied block for the given server.
-
#initialize(db, opts = OPTS) ⇒ ShardedSingleConnectionPool
constructor
The single threaded pool takes the following options:.
-
#max_size ⇒ Object
The ShardedSingleConnectionPool always has a maximum size of 1.
- #pool_type ⇒ Object
-
#remove_servers(servers) ⇒ Object
Remove servers from the connection pool.
-
#servers ⇒ Object
Return an array of symbols for servers in the connection pool.
-
#size ⇒ Object
The number of different shards/servers this pool is connected to.
Methods inherited from ConnectionPool
Methods included from ConnectionPool::ClassMethods
Constructor Details
#initialize(db, opts = OPTS) ⇒ ShardedSingleConnectionPool
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.
14 15 16 17 18 19 20 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 14 def initialize(db, opts=OPTS) super @conns = {} @servers = opts.fetch(:servers_hash, Hash.new(:default)) add_servers([:default]) add_servers(opts[:servers].keys) if opts[:servers] end |
Instance Method Details
#add_servers(servers) ⇒ Object
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.
25 26 27 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 25 def add_servers(servers) servers.each{|s| @servers[s] = s} end |
#all_connections ⇒ Object
Yield all of the currently established connections
30 31 32 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 30 def all_connections @conns.values.each{|c| yield c} end |
#conn(server = :default) ⇒ Object
The connection for the given server.
35 36 37 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 35 def conn(server=:default) @conns[@servers[server]] end |
#disconnect(opts = OPTS) ⇒ Object
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.
43 44 45 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 43 def disconnect(opts=OPTS) (opts[:server] ? Array(opts[:server]) : servers).each{|s| disconnect_server(s)} end |
#hold(server = :default) ⇒ Object
Yields the connection to the supplied block for the given server. This method simulates the ConnectionPool#hold API.
49 50 51 52 53 54 55 56 57 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 49 def hold(server=:default) begin server = pick_server(server) yield(@conns[server] ||= make_new(server)) rescue Sequel::DatabaseDisconnectError, *@error_classes => e disconnect_server(server) if disconnect_error?(e) raise end end |
#max_size ⇒ Object
The ShardedSingleConnectionPool always has a maximum size of 1.
60 61 62 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 60 def max_size 1 end |
#pool_type ⇒ Object
86 87 88 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 86 def pool_type :sharded_single end |
#remove_servers(servers) ⇒ Object
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.
68 69 70 71 72 73 74 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 68 def remove_servers(servers) raise(Sequel::Error, "cannot remove default server") if servers.include?(:default) servers.each do |server| disconnect_server(server) @servers.delete(server) end end |
#servers ⇒ Object
Return an array of symbols for servers in the connection pool.
77 78 79 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 77 def servers @servers.keys end |
#size ⇒ Object
The number of different shards/servers this pool is connected to.
82 83 84 |
# File 'lib/sequel/connection_pool/sharded_single.rb', line 82 def size @conns.length end |