| 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.
Initializes the instance with the supplied block as the connection_proc.
The single threaded pool takes the following options:
# File lib/sequel/connection_pool/sharded_single.rb, line 15
15: def initialize(opts={}, &block)
16: super
17: @conns = {}
18: @servers = opts.fetch(:servers_hash, Hash.new(:default))
19: add_servers([:default])
20: add_servers(opts[:servers].keys) if opts[:servers]
21: 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 26
26: def add_servers(servers)
27: servers.each{|s| @servers[s] = s}
28: end
The connection for the given server.
# File lib/sequel/connection_pool/sharded_single.rb, line 31
31: def conn(server=:default)
32: @conns[@servers[server]]
33: end
Disconnects from the database. Once a connection is requested using hold, the connection is reestablished. Options:
# File lib/sequel/connection_pool/sharded_single.rb, line 39
39: def disconnect(opts={}, &block)
40: block ||= @disconnection_proc
41: (opts[:server] ? Array(opts[:server]) : servers).each{|s| disconnect_server(s, &block)}
42: 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 46
46: def hold(server=:default)
47: begin
48: server = @servers[server]
49: yield(@conns[server] ||= make_new(server))
50: rescue Sequel::DatabaseDisconnectError
51: disconnect_server(server, &@disconnection_proc)
52: raise
53: end
54: 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 60
60: def remove_servers(servers)
61: raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
62: servers.each do |server|
63: disconnect_server(server, &@disconnection_proc)
64: @servers.delete(server)
65: end
66: end