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.

Methods

add_servers   conn   disconnect   hold   new   remove_servers   servers   size  

Public Class methods

Initializes the instance with the supplied block as the connection_proc.

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. The server name symbol will be passed to the connection_proc.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 11
11:   def initialize(opts={}, &block)
12:     super
13:     @conns = {}
14:     @servers = Hash.new(:default)
15:     add_servers([:default])
16:     add_servers(opts[:servers].keys) if opts[:servers]
17:   end

Public Instance methods

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.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 22
22:   def add_servers(servers)
23:     servers.each{|s| @servers[s] = s}
24:   end

The connection for the given server.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 27
27:   def conn(server=:default)
28:     @conns[@servers[server]]
29:   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.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 35
35:   def disconnect(opts={}, &block)
36:     block ||= @disconnection_proc
37:     (opts[:server] ? Array(opts[:server]) : servers).each{|s| disconnect_server(s, &block)}
38:   end

Yields the connection to the supplied block for the given server. This method simulates the ConnectionPool#hold API.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 42
42:   def hold(server=:default)
43:     begin
44:       server = @servers[server]
45:       yield(@conns[server] ||= make_new(server))
46:     rescue Sequel::DatabaseDisconnectError
47:       disconnect_server(server, &@disconnection_proc)
48:       raise
49:     end
50:   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.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 56
56:   def remove_servers(servers)
57:     raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
58:     servers.each do |server|
59:       disconnect_server(server, &@disconnection_proc)
60:       @servers.delete(server)
61:     end
62:   end

Return an array of symbols for servers in the connection pool.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 65
65:   def servers
66:     @servers.keys
67:   end

The number of different shards/servers this pool is connected to.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 70
70:   def size
71:     @conns.length
72:   end

[Validate]