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

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.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 12
12:   def initialize(db, opts=OPTS)
13:     super
14:     @conns = {}
15:     @servers = opts.fetch(:servers_hash, Hash.new(:default))
16:     add_servers([:default])
17:     add_servers(opts[:servers].keys) if opts[:servers]
18:   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 23
23:   def add_servers(servers)
24:     servers.each{|s| @servers[s] = s}
25:   end

Yield all of the currently established connections

[Source]

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

The connection for the given server.

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 33
33:   def conn(server=:default)
34:     @conns[@servers[server]]
35:   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 41
41:   def disconnect(opts=OPTS)
42:     (opts[:server] ? Array(opts[:server]) : servers).each{|s| disconnect_server(s)}
43:   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 47
47:   def hold(server=:default)
48:     begin
49:       server = pick_server(server)
50:       yield(@conns[server] ||= make_new(server))
51:     rescue Sequel::DatabaseDisconnectError
52:       disconnect_server(server)
53:       raise
54:     end
55:   end

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 79
79:   def pool_type
80:     :sharded_single
81:   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 61
61:   def remove_servers(servers)
62:     raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
63:     servers.each do |server|
64:       disconnect_server(server)
65:       @servers.delete(server)
66:     end
67:   end

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

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 70
70:   def servers
71:     @servers.keys
72:   end

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

[Source]

    # File lib/sequel/connection_pool/sharded_single.rb, line 75
75:   def size
76:     @conns.length
77:   end

[Validate]