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.

[Source]

        # File lib/sequel/connection_pool/sharded_threaded.rb, line 13
    13:   def initialize(opts = {}, &block)
    14:     super
    15:     @available_connections = {}
    16:     @connections_to_remove = []
    17:     @servers = Hash.new(:default)
    18:     add_servers([:default])
    19:     add_servers(opts[:servers].keys) if opts[:servers]
    20:   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 25
    25:   def add_servers(servers)
    26:     sync do
    27:       servers.each do |server|
    28:         unless @servers.has_key?(server)
    29:           @servers[server] = server
    30:           @available_connections[server] = []
    31:           @allocated[server] = {}
    32:         end
    33:       end
    34:     end
    35:   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 40
    40:   def allocated(server=:default)
    41:     @allocated[server]
    42:   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 47
    47:   def available_connections(server=:default)
    48:     @available_connections[server]
    49:   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 69
    69:   def disconnect(opts={}, &block)
    70:     block ||= @disconnection_proc
    71:     sync do
    72:       (opts[:server] ? Array(opts[:server]) : @servers.keys).each do |s|
    73:         disconnect_server(s, &block)
    74:       end
    75:     end
    76:   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 92
     92:   def hold(server=:default)
     93:     sync{server = @servers[server]}
     94:     t = Thread.current
     95:     if conn = owned_connection(t, server)
     96:       return yield(conn)
     97:     end
     98:     begin
     99:       unless conn = acquire(t, server)
    100:         time = Time.now
    101:         timeout = time + @timeout
    102:         sleep_time = @sleep_time
    103:         sleep sleep_time
    104:         until conn = acquire(t, server)
    105:           raise(::Sequel::PoolTimeout) if Time.now > timeout
    106:           sleep sleep_time
    107:         end
    108:       end
    109:       yield conn
    110:     rescue Sequel::DatabaseDisconnectError
    111:       sync{@connections_to_remove << conn} if conn
    112:       raise
    113:     ensure
    114:       sync{release(t, conn, server)} if conn
    115:     end
    116:   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 122
    122:   def remove_servers(servers)
    123:     sync do
    124:       raise(Sequel::Error, "cannot remove default server") if servers.include?(:default)
    125:       servers.each do |server|
    126:         if @servers.include?(server)
    127:           disconnect_server(server, &@disconnection_proc)
    128:           @available_connections.delete(server)
    129:           @allocated.delete(server)
    130:           @servers.delete(server)
    131:         end
    132:       end
    133:     end
    134:   end

servers()

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

[Source]

         # File lib/sequel/connection_pool/sharded_threaded.rb, line 137
    137:   def servers
    138:     sync{@servers.keys}
    139:   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 54
    54:   def size(server=:default)
    55:     server = @servers[server]
    56:     @allocated[server].length + @available_connections[server].length
    57:   end

[Validate]