Class: Sequel::ShardedSingleConnectionPool

Inherits:
ConnectionPool show all
Defined in:
lib/sequel/connection_pool/sharded_single.rb

Overview

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

Constant Summary

Constants inherited from ConnectionPool

ConnectionPool::CONNECTION_POOL_MAP, ConnectionPool::DEFAULT_SERVER

Instance Method Summary collapse

Methods inherited from ConnectionPool

#created_count

Methods included from ConnectionPool::ClassMethods

#get_pool

Constructor Details

#initialize(opts = {}, &block) ⇒ ShardedSingleConnectionPool

Initializes the instance with the supplied block as the connection_proc.

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. The server name symbol will be passed to the connection_proc.



11
12
13
14
15
16
17
# File 'lib/sequel/connection_pool/sharded_single.rb', line 11

def initialize(opts={}, &block)
  super
  @conns = {}
  @servers = Hash.new(:default)
  add_servers([:default])
  add_servers(opts[:servers].keys) if opts[:servers]
end

Instance Method Details

#add_servers(servers) ⇒ Object

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.



22
23
24
# File 'lib/sequel/connection_pool/sharded_single.rb', line 22

def add_servers(servers)
  servers.each{|s| @servers[s] = s}
end

#conn(server = :default) ⇒ Object

The connection for the given server.



27
28
29
# File 'lib/sequel/connection_pool/sharded_single.rb', line 27

def conn(server=:default)
  @conns[@servers[server]]
end

#disconnect(opts = {}, &block) ⇒ Object

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.



35
36
37
38
# File 'lib/sequel/connection_pool/sharded_single.rb', line 35

def disconnect(opts={}, &block)
  block ||= @disconnection_proc
  (opts[:server] ? Array(opts[:server]) : servers).each{|s| disconnect_server(s, &block)}
end

#hold(server = :default) ⇒ Object

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



42
43
44
45
46
47
48
49
50
# File 'lib/sequel/connection_pool/sharded_single.rb', line 42

def hold(server=:default)
  begin
    server = @servers[server]
    yield(@conns[server] ||= make_new(server))
  rescue Sequel::DatabaseDisconnectError
    disconnect_server(server, &@disconnection_proc)
    raise
  end
end

#remove_servers(servers) ⇒ Object

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.

Raises:



56
57
58
59
60
61
62
# File 'lib/sequel/connection_pool/sharded_single.rb', line 56

def remove_servers(servers)
  raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
  servers.each do |server|
    disconnect_server(server, &@disconnection_proc)
    @servers.delete(server)
  end
end

#serversObject

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



65
66
67
# File 'lib/sequel/connection_pool/sharded_single.rb', line 65

def servers
  @servers.keys
end

#sizeObject

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



70
71
72
# File 'lib/sequel/connection_pool/sharded_single.rb', line 70

def size
  @conns.length
end