class Sequel::ShardedSingleConnectionPool

A ShardedSingleConnectionPool is a single threaded connection pool that works with multiple shards/servers.

Public Class Methods

new(db, opts=OPTS) click to toggle source

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.

Calls superclass method Sequel::ConnectionPool::new
   # File lib/sequel/connection_pool/sharded_single.rb
14 def initialize(db, opts=OPTS)
15   super
16   @conns = {}
17   @servers = opts.fetch(:servers_hash, Hash.new(:default))
18   add_servers([:default])
19   add_servers(opts[:servers].keys) if opts[:servers]
20 end

Public Instance Methods

add_servers(servers) click to toggle source

Adds new servers to the connection pool. Primarily used in conjunction with primary/replica or sharded configurations. Allows for dynamic expansion of the potential replicas/shards at runtime. servers argument should be an array of symbols.

   # File lib/sequel/connection_pool/sharded_single.rb
25 def add_servers(servers)
26   servers.each{|s| @servers[s] = s}
27 end
all_connections() { |c| ... } click to toggle source

Yield all of the currently established connections

   # File lib/sequel/connection_pool/sharded_single.rb
30 def all_connections
31   @conns.values.each{|c| yield c}
32 end
conn(server=:default) click to toggle source

The connection for the given server.

   # File lib/sequel/connection_pool/sharded_single.rb
35 def conn(server=:default)
36   @conns[@servers[server]]
37 end
disconnect(opts=OPTS) click to toggle source

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.

   # File lib/sequel/connection_pool/sharded_single.rb
43 def disconnect(opts=OPTS)
44   (opts[:server] ? Array(opts[:server]) : servers).each do |s|
45     raise Sequel::Error, "invalid server: #{s}" unless @servers.has_key?(s)
46     disconnect_server(s)
47   end
48 end
freeze() click to toggle source
Calls superclass method
   # File lib/sequel/connection_pool/sharded_single.rb
50 def freeze
51   @servers.freeze
52   super
53 end
hold(server=:default) { |conns ||= make_new(server)| ... } click to toggle source

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
57 def hold(server=:default)
58   server = pick_server(server)
59   yield(@conns[server] ||= make_new(server))
60 rescue Sequel::DatabaseDisconnectError, *@error_classes => e
61   disconnect_server(server) if disconnect_error?(e)
62   raise
63 end
max_size() click to toggle source

The ShardedSingleConnectionPool always has a maximum size of 1.

   # File lib/sequel/connection_pool/sharded_single.rb
66 def max_size
67   1
68 end
pool_type() click to toggle source
   # File lib/sequel/connection_pool/sharded_single.rb
91 def pool_type
92   :sharded_single
93 end
remove_servers(servers) click to toggle source

Remove servers from the connection pool. 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
73 def remove_servers(servers)
74   raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
75   servers.each do |server|
76     disconnect_server(server)
77     @servers.delete(server)
78   end
79 end
servers() click to toggle source

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

   # File lib/sequel/connection_pool/sharded_single.rb
82 def servers
83   @servers.keys
84 end
size() click to toggle source

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

   # File lib/sequel/connection_pool/sharded_single.rb
87 def size
88   @conns.length
89 end

Private Instance Methods

disconnect_server(server) click to toggle source

Disconnect from the given server, if connected.

    # File lib/sequel/connection_pool/sharded_single.rb
 98 def disconnect_server(server)
 99   if conn = @conns.delete(server)
100     disconnect_connection(conn)
101   end
102 end
pick_server(server) click to toggle source

If the server given is in the hash, return it, otherwise, return the default server.

    # File lib/sequel/connection_pool/sharded_single.rb
105 def pick_server(server)
106   @servers[server]
107 end
preconnect(concurrent = nil) click to toggle source

Make sure there is a valid connection for each server.

    # File lib/sequel/connection_pool/sharded_single.rb
110 def preconnect(concurrent = nil)
111   servers.each{|s| hold(s){}}
112 end