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, line 14
def initialize(db, opts=OPTS)
  super
  @conns = {}
  @servers = opts.fetch(:servers_hash, Hash.new(:default))
  add_servers([:default])
  add_servers(opts[:servers].keys) if opts[:servers]
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, line 25
def add_servers(servers)
  servers.each{|s| @servers[s] = s}
end
all_connections() { |c| ... } click to toggle source

Yield all of the currently established connections

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

The connection for the given server.

# File lib/sequel/connection_pool/sharded_single.rb, line 35
def conn(server=:default)
  @conns[@servers[server]]
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, line 43
def disconnect(opts=OPTS)
  (opts[:server] ? Array(opts[:server]) : servers).each{|s| disconnect_server(s)}
end
freeze() click to toggle source
Calls superclass method
# File lib/sequel/connection_pool/sharded_single.rb, line 47
def freeze
  @servers.freeze
  super
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, line 54
def hold(server=:default)
  begin
    server = pick_server(server)
    yield(@conns[server] ||= make_new(server))
  rescue Sequel::DatabaseDisconnectError, *@error_classes => e
    disconnect_server(server) if disconnect_error?(e)
    raise
  end
end
max_size() click to toggle source

The ShardedSingleConnectionPool always has a maximum size of 1.

# File lib/sequel/connection_pool/sharded_single.rb, line 65
def max_size
  1
end
pool_type() click to toggle source
# File lib/sequel/connection_pool/sharded_single.rb, line 90
def pool_type
  :sharded_single
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, line 72
def remove_servers(servers)
  raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
  servers.each do |server|
    disconnect_server(server)
    @servers.delete(server)
  end
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, line 81
def servers
  @servers.keys
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, line 86
def size
  @conns.length
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, line 97
def disconnect_server(server)
  if conn = @conns.delete(server)
    disconnect_connection(conn)
  end
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, line 104
def pick_server(server)
  @servers[server]
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, line 109
def preconnect(concurrent = nil)
  servers.each{|s| hold(s){}}
end