class Sequel::ShardedSingleConnectionPool

  1. lib/sequel/connection_pool/sharded_single.rb
Superclass: ConnectionPool

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

Public Class methods

new (db, opts=OPTS)

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.

[show source]
   # 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)

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.

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

Yield all of the currently established connections

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

The connection for the given server.

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

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.

[show source]
   # File lib/sequel/connection_pool/sharded_single.rb
43 def disconnect(opts=OPTS)
44   (opts[:server] ? Array(opts[:server]) : servers).each{|s| disconnect_server(s)}
45 end
freeze ()
[show source]
   # File lib/sequel/connection_pool/sharded_single.rb
47 def freeze
48   @servers.freeze
49   super
50 end
hold (server=:default)

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

[show source]
   # File lib/sequel/connection_pool/sharded_single.rb
54 def hold(server=:default)
55   begin
56     server = pick_server(server)
57     yield(@conns[server] ||= make_new(server))
58   rescue Sequel::DatabaseDisconnectError, *@error_classes => e
59     disconnect_server(server) if disconnect_error?(e)
60     raise
61   end
62 end
max_size ()

The ShardedSingleConnectionPool always has a maximum size of 1.

[show source]
   # File lib/sequel/connection_pool/sharded_single.rb
65 def max_size
66   1
67 end
pool_type ()
[show source]
   # File lib/sequel/connection_pool/sharded_single.rb
90 def pool_type
91   :sharded_single
92 end
remove_servers (servers)

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.

[show source]
   # File lib/sequel/connection_pool/sharded_single.rb
72 def remove_servers(servers)
73   raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
74   servers.each do |server|
75     disconnect_server(server)
76     @servers.delete(server)
77   end
78 end
servers ()

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

[show source]
   # File lib/sequel/connection_pool/sharded_single.rb
81 def servers
82   @servers.keys
83 end
size ()

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

[show source]
   # File lib/sequel/connection_pool/sharded_single.rb
86 def size
87   @conns.length
88 end