Class | Sequel::ShardedThreadedConnectionPool
In: | lib/sequel/connection_pool/sharded_threaded.rb
Parent: | Sequel::ThreadedConnectionPool

The slowest and most advanced connection, 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.

### Methods

add_servers allocated available_connections disconnect hold new remove_servers servers size

### Public Class methods

new(opts = {}, &block)

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

[Source]

        # File lib/sequel/connection_pool/sharded_threaded.rb, line 17
    17:   def initialize(opts = {}, &block)
    18:     super
    19:     @available_connections = {}
    20:     @connections_to_remove = []
    21:     @servers = opts.fetch(:servers_hash, Hash.new(:default))
    22:     add_servers([:default])
    23:     add_servers(opts[:servers].keys) if opts[:servers]
    24:   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.

[Source]

        # File lib/sequel/connection_pool/sharded_threaded.rb, line 29
    29:   def add_servers(servers)
    30:     sync do
    31:       servers.each do |server|
    32:         unless @servers.has_key?(server)
    33:           @servers[server] = server
    34:           @available_connections[server] = []
    35:           @allocated[server] = {}
    36:         end
    37:       end
    38:     end
    39:   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.

[Source]

        # File lib/sequel/connection_pool/sharded_threaded.rb, line 44
    44:   def allocated(server=:default)
    45:     @allocated[server]
    46:   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.

[Source]

        # File lib/sequel/connection_pool/sharded_threaded.rb, line 51
    51:   def available_connections(server=:default)
    52:     @available_connections[server]
    53:   end

disconnect(opts={}, &block)

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 specifing the server to disconnect from, or an array of symbols to specify multiple servers.

[Source]

        # File lib/sequel/connection_pool/sharded_threaded.rb, line 73
    73:   def disconnect(opts={}, &block)
    74:     block ||= @disconnection_proc
    75:     sync do
    76:       (opts[:server] ? Array(opts[:server]) : @servers.keys).each do |s|
    77:         disconnect_server(s, &block)
    78:       end
    79:     end
    80:   end

hold(server=:default) {|conn| ...}

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 {|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.

[Source]

         # File lib/sequel/connection_pool/sharded_threaded.rb, line 96
     96:   def hold(server=:default)
     97:     sync{server = @servers[server]}
     98:     t = Thread.current
     99:     if conn = owned_connection(t, server)
    100:       return yield(conn)
    101:     end
    102:     begin
    103:       unless conn = acquire(t, server)
    104:         time = Time.now
    105:         timeout = time + @timeout
    106:         sleep_time = @sleep_time
    107:         sleep sleep_time
    108:         until conn = acquire(t, server)
    109:           raise(::Sequel::PoolTimeout) if Time.now > timeout
    110:           sleep sleep_time
    111:         end
    112:       end
    113:       yield conn
    114:     rescue Sequel::DatabaseDisconnectError
    115:       sync{@connections_to_remove << conn} if conn
    116:       raise
    117:     ensure
    118:       sync{release(t, conn, server)} if conn
    119:     end
    120:   end

remove_servers(servers)

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.

[Source]

         # File lib/sequel/connection_pool/sharded_threaded.rb, line 126
    126:   def remove_servers(servers)
    127:     sync do
    128:       raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
    129:       servers.each do |server|
    130:         if @servers.include?(server)
    131:           disconnect_server(server, &@disconnection_proc)
    132:           @available_connections.delete(server)
    133:           @allocated.delete(server)
    134:           @servers.delete(server)
    135:         end
    136:       end
    137:     end
    138:   end

servers()

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

[Source]

         # File lib/sequel/connection_pool/sharded_threaded.rb, line 141
    141:   def servers
    142:     sync{@servers.keys}
    143:   end

size(server=:default)

The total number of connections opened for the given server, should be equal to available_connections.length + allocated.length. Nonexistent servers will return the created count of the default server.

[Source]

        # File lib/sequel/connection_pool/sharded_threaded.rb, line 58
    58:   def size(server=:default)
    59:     server = @servers[server]
    60:     @allocated[server].length + @available_connections[server].length
    61:   end

[Validate]