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, line 13
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)

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.

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

Yield all of the currently established connections

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

The connection for the given server.

[show source]
# File lib/sequel/connection_pool/sharded_single.rb, line 34
def conn(server=:default)
  @conns[@servers[server]]
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, line 42
def disconnect(opts=OPTS)
  (opts[:server] ? Array(opts[:server]) : servers).each{|s| disconnect_server(s)}
end
freeze ()
[show source]
# File lib/sequel/connection_pool/sharded_single.rb, line 46
def freeze
  @servers.freeze
  super
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, line 53
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 ()

The ShardedSingleConnectionPool always has a maximum size of 1.

[show source]
# File lib/sequel/connection_pool/sharded_single.rb, line 64
def max_size
  1
end
pool_type ()
[show source]
# File lib/sequel/connection_pool/sharded_single.rb, line 89
def pool_type
  :sharded_single
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, line 71
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 ()

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

[show source]
# File lib/sequel/connection_pool/sharded_single.rb, line 80
def servers
  @servers.keys
end
size ()

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

[show source]
# File lib/sequel/connection_pool/sharded_single.rb, line 85
def size
  @conns.length
end