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

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.
  • :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.

[Source]

    # 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

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 26
26:   def add_servers(servers)
27:     servers.each{|s| @servers[s] = s}
28:   end

Yield all of the currently established connections

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 31
31:   def all_connections
32:     @conns.values.each{|c| yield c}
33:   end

The connection for the given server.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 36
36:   def conn(server=:default)
37:     @conns[@servers[server]]
38:   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 44
44:   def disconnect(opts={}, &block)
45:     block ||= @disconnection_proc
46:     (opts[:server] ? Array(opts[:server]) : servers).each{|s| disconnect_server(s, &block)}
47:   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 51
51:   def hold(server=:default)
52:     begin
53:       server = pick_server(server)
54:       yield(@conns[server] ||= make_new(server))
55:     rescue Sequel::DatabaseDisconnectError
56:       disconnect_server(server, &@disconnection_proc)
57:       raise
58:     end
59:   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 65
65:   def remove_servers(servers)
66:     raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
67:     servers.each do |server|
68:       disconnect_server(server, &@disconnection_proc)
69:       @servers.delete(server)
70:     end
71:   end

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

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 74
74:   def servers
75:     @servers.keys
76:   end

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

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 79
79:   def size
80:     @conns.length
81:   end

[Validate]