Class: WithConnection::RangedConnectionPool
- Inherits:
-
Object
- Object
- WithConnection::RangedConnectionPool
show all
- Defined in:
- lib/with_connection/ranged_connection_pool.rb
Defined Under Namespace
Classes: BasicRange, Item
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(ranges_and_pools, default_pool, key_algo) ⇒ RangedConnectionPool
Returns a new instance of RangedConnectionPool.
5
6
7
8
9
|
# File 'lib/with_connection/ranged_connection_pool.rb', line 5
def initialize(ranges_and_pools, default_pool, key_algo)
@list = ranges_and_pools.map { |range, pool| Item.new(range, pool) }
@default_pool = default_pool
@key_algo = key_algo
end
|
Instance Attribute Details
#default_pool ⇒ Object
Returns the value of attribute default_pool.
3
4
5
|
# File 'lib/with_connection/ranged_connection_pool.rb', line 3
def default_pool
@default_pool
end
|
#key_algo ⇒ Object
Returns the value of attribute key_algo.
3
4
5
|
# File 'lib/with_connection/ranged_connection_pool.rb', line 3
def key_algo
@key_algo
end
|
#list ⇒ Object
Returns the value of attribute list.
3
4
5
|
# File 'lib/with_connection/ranged_connection_pool.rb', line 3
def list
@list
end
|
Instance Method Details
#connection ⇒ Object
18
19
20
|
# File 'lib/with_connection/ranged_connection_pool.rb', line 18
def connection
local_current[:with_connection_ranged_connection_pool].try(:connection) || @default_pool.connection
end
|
#create_all_connections ⇒ Object
33
34
35
36
|
# File 'lib/with_connection/ranged_connection_pool.rb', line 33
def create_all_connections
@default_pool.create_all_connections
@list.each { |item| item.pool.create_all_connections }
end
|
#disconnect! ⇒ Object
38
39
40
41
|
# File 'lib/with_connection/ranged_connection_pool.rb', line 38
def disconnect!
@default_pool.disconnect!
@list.each { |item| item.pool.disconnect! }
end
|
#local_current ⇒ Object
29
30
31
|
# File 'lib/with_connection/ranged_connection_pool.rb', line 29
def local_current
defined?(EM) && EM.reactor_running? ? Fiber.current : Thread.current
end
|
#pool_for_key(key, read_write) ⇒ Object
22
23
24
25
26
27
|
# File 'lib/with_connection/ranged_connection_pool.rb', line 22
def pool_for_key(key, read_write)
key = self.key_algo.call(key) if key
key.nil? ?
@default_pool :
(@list.detect { |item| item.include?(key) }.try(:pool) || @default_pool)
end
|
#with_connection(key = nil, read_write = nil, &block) ⇒ Object
11
12
13
14
15
16
|
# File 'lib/with_connection/ranged_connection_pool.rb', line 11
def with_connection(key=nil, read_write=nil, &block)
local_current[:with_connection_ranged_connection_pool] = pool_for_key(key, read_write)
local_current[:with_connection_ranged_connection_pool].with_connection(key, read_write, &block)
ensure
local_current[:with_connection_ranged_connection_pool] = nil
end
|