Class: Spider::Model::Storage::ConnectionPool
- Defined in:
- lib/spiderfw/model/storage/connection_pool.rb
Instance Attribute Summary collapse
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
-
#retry ⇒ Object
Returns the value of attribute retry.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #checkout ⇒ Object
- #clear ⇒ Object
- #free_size ⇒ Object
- #get_connection ⇒ Object
-
#initialize(connection_params, provider) ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
- #release(conn) ⇒ Object
- #remove(conn) ⇒ Object
- #size ⇒ Object
- #storage_type ⇒ Object
Constructor Details
#initialize(connection_params, provider) ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 9 def initialize(connection_params, provider) @connection_params = connection_params @provider = provider @connection_mutex = Monitor.new @queue = @connection_mutex.new_cond @max_size = Spider.conf.get('storage.pool.size') @max_size = provider.max_connections if provider.max_connections && provider.max_connections < @max_size @timeout = Spider.conf.get('storage.pool.timeout') @retry = Spider.conf.get('storage.pool.retry') @connections = [] @free_connections = [] @thread_connections = {} end |
Instance Attribute Details
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
6 7 8 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 6 def max_size @max_size end |
#retry ⇒ Object
Returns the value of attribute retry.
7 8 9 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 7 def retry @retry end |
#timeout ⇒ Object
Returns the value of attribute timeout.
7 8 9 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 7 def timeout @timeout end |
Instance Method Details
#checkout ⇒ Object
57 58 59 60 61 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 57 def checkout @connection_mutex.synchronize do _checkout end end |
#clear ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 82 def clear @connections.each do |c| @provider.disconnect(c) end @connections = [] @free_connections = [] end |
#free_size ⇒ Object
27 28 29 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 27 def free_size @free_connections.length end |
#get_connection ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 35 def get_connection if Spider.conf.get('storage.shared_connection') @shared_conn ||= _checkout return @shared_conn end Thread.current[:storage_connections] ||= {} Thread.current[:storage_connections][storage_type] ||= {} @connection_mutex.synchronize do #Spider.logger.debug("DB Pool (#{Thread.current}): trying to get connection") if conn = Thread.current[:storage_connections][storage_type][@connection_params] #Spider.logger.debug("DB Pool (#{Thread.current}): returning thread connection #{conn}") @free_connections.delete(conn) conn else conn = _checkout Thread.current[:storage_connections][storage_type][@connection_params] = conn @thread_connections[Thread.current.object_id] = [conn, Time.now] conn end end end |
#release(conn) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 63 def release(conn) if Spider.conf.get('storage.shared_connection') return end @connection_mutex.synchronize do #Spider.logger.debug("DB Pool (#{Thread.current}): releasing #{conn}") @free_connections << conn Thread.current[:storage_connections][storage_type].delete(@connection_params) @thread_connections.delete(Thread.current.object_id) @queue.signal end end |
#remove(conn) ⇒ Object
76 77 78 79 80 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 76 def remove(conn) @connection_mutex.synchronize do remove_connection(conn) end end |
#size ⇒ Object
23 24 25 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 23 def size @connections.length end |
#storage_type ⇒ Object
31 32 33 |
# File 'lib/spiderfw/model/storage/connection_pool.rb', line 31 def storage_type @provider.storage_type end |