class Sequel::ShardedThreadedConnectionPool

  1. lib/sequel/connection_pool/sharded_threaded.rb

The slowest and most advanced connection pool, dealing with both multi-threaded access and configurations with multiple shards/servers.

In addition, this pool subclass also handles scheduling in-use connections to be removed from the pool when they are returned to it.

Public Class methods

new(db, opts = OPTS)

The following additional options are respected:

: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_threaded.rb
20 def initialize(db, opts = OPTS)
21   super
22   @available_connections = {}
23   @connections_to_remove = []
24   @connections_to_disconnect = []
25   @servers = opts.fetch(:servers_hash, Hash.new(:default))
26   remove_instance_variable(:@waiter)
27   remove_instance_variable(:@allocated)
28   @allocated = {}
29   @waiters = {}
30 
31   add_servers([:default])
32   add_servers(opts[:servers].keys) if opts[:servers]
33 end

Public Instance methods

add_servers(servers)

Adds new servers to the connection pool. 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_threaded.rb
37 def add_servers(servers)
38   sync do
39     servers.each do |server|
40       unless @servers.has_key?(server)
41         @servers[server] = server
42         @available_connections[server] = []
43         allocated = {}
44         allocated.compare_by_identity
45         @allocated[server] = allocated
46         @waiters[server] = ConditionVariable.new
47       end
48     end
49   end
50 end
all_connections()

Yield all of the available connections, and the ones currently allocated to this thread. This will not yield connections currently allocated to other threads, as it is not safe to operate on them. This holds the mutex while it is yielding all of the connections, which means that until the method’s block returns, the pool is locked.

[show source]
   # File lib/sequel/connection_pool/sharded_threaded.rb
65 def all_connections
66   t = Sequel.current
67   sync do
68     @allocated.values.each do |threads|
69       threads.each do |thread, conn|
70         yield conn if t == thread
71       end
72     end
73     @available_connections.values.each{|v| v.each{|c| yield c}}
74   end
75 end
allocated(server=:default)

A hash of connections currently being used for the given server, key is the Thread, value is the connection. Nonexistent servers will return nil. Treat this as read only, do not modify the resulting object. The calling code should already have the mutex before calling this.

[show source]
   # File lib/sequel/connection_pool/sharded_threaded.rb
56 def allocated(server=:default)
57   @allocated[server]
58 end
available_connections(server=:default)

An array of connections opened but not currently used, for the given server. Nonexistent servers will return nil. Treat this as read only, do not modify the resulting object. The calling code should already have the mutex before calling this.

[show source]
   # File lib/sequel/connection_pool/sharded_threaded.rb
81 def available_connections(server=:default)
82   @available_connections[server]
83 end
disconnect(opts=OPTS)

Removes all connections currently available on all servers, optionally yielding each connection to the given block. This method has the effect of disconnecting from the database, assuming that no connections are currently being used. If connections are being used, they are scheduled to be disconnected as soon as they are returned to the pool.

Once a connection is requested using hold, the connection pool creates new connections to the database. Options:

:server

Should be a symbol specifying the server to disconnect from, or an array of symbols to specify multiple servers.

[show source]
    # File lib/sequel/connection_pool/sharded_threaded.rb
102 def disconnect(opts=OPTS)
103   (opts[:server] ? Array(opts[:server]) : sync{@servers.keys}).each do |s|
104     disconnect_connections(sync{disconnect_server_connections(s)})
105   end
106 end
freeze()
[show source]
    # File lib/sequel/connection_pool/sharded_threaded.rb
108 def freeze
109   @servers.freeze
110   super
111 end
hold(server=:default)

Chooses the first available connection to the given server, or if none are available, creates a new connection. Passes the connection to the supplied block:

pool.hold(:server1) {|conn| conn.execute('DROP TABLE posts')}

Pool#hold is re-entrant, meaning it can be called recursively in the same thread without blocking.

If no connection is immediately available and the pool is already using the maximum number of connections, Pool#hold will block until a connection is available or the timeout expires. If the timeout expires before a connection can be acquired, a Sequel::PoolTimeout is raised.

[show source]
    # File lib/sequel/connection_pool/sharded_threaded.rb
126 def hold(server=:default)
127   server = pick_server(server)
128   t = Sequel.current
129   if conn = owned_connection(t, server)
130     return yield(conn)
131   end
132   begin
133     conn = acquire(t, server)
134     yield conn
135   rescue Sequel::DatabaseDisconnectError, *@error_classes => e
136     sync{@connections_to_remove << conn} if conn && disconnect_error?(e)
137     raise
138   ensure
139     sync{release(t, conn, server)} if conn
140     while dconn = sync{@connections_to_disconnect.shift}
141       disconnect_connection(dconn)
142     end
143   end
144 end
pool_type()
[show source]
    # File lib/sequel/connection_pool/sharded_threaded.rb
175 def pool_type
176   :sharded_threaded
177 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_threaded.rb
149 def remove_servers(servers)
150   conns = []
151   raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
152 
153   sync do
154     servers.each do |server|
155       if @servers.include?(server)
156         conns.concat(disconnect_server_connections(server))
157         @waiters.delete(server)
158         @available_connections.delete(server)
159         @allocated.delete(server)
160         @servers.delete(server)
161       end
162     end
163   end
164 
165   nil
166 ensure
167   disconnect_connections(conns)
168 end
servers()

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

[show source]
    # File lib/sequel/connection_pool/sharded_threaded.rb
171 def servers
172   sync{@servers.keys}
173 end
size(server=:default)

The total number of connections opened for the given server. Nonexistent servers will return the created count of the default server. The calling code should NOT have the mutex before calling this.

[show source]
   # File lib/sequel/connection_pool/sharded_threaded.rb
88 def size(server=:default)
89   @mutex.synchronize{_size(server)}
90 end