Class: Stillwater::ConnectionPool
- Inherits:
-
Object
- Object
- Stillwater::ConnectionPool
- Defined in:
- lib/stillwater/connection_pool.rb
Instance Attribute Summary collapse
-
#reactivate_timeout ⇒ Object
Returns the value of attribute reactivate_timeout.
-
#retry_count ⇒ Object
Returns the value of attribute retry_count.
Instance Method Summary collapse
- #add(&builder) ⇒ Object
- #available_count ⇒ Object
- #checkin(conn) ⇒ Object
- #checkout ⇒ Object
- #deactivate(conn) ⇒ Object
- #in_use_count ⇒ Object
- #inactive_count ⇒ Object
-
#initialize ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
- #reactivate_all ⇒ Object
- #retry_connection_from(exception_class, &block) ⇒ Object
- #with_connection(&block) ⇒ Object
Constructor Details
#initialize ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
9 10 11 12 13 |
# File 'lib/stillwater/connection_pool.rb', line 9 def initialize @pool = [] @reactivate_timeout = 5 * 60 # 5 minutes @retry_count = 3 end |
Instance Attribute Details
#reactivate_timeout ⇒ Object
Returns the value of attribute reactivate_timeout.
7 8 9 |
# File 'lib/stillwater/connection_pool.rb', line 7 def reactivate_timeout @reactivate_timeout end |
#retry_count ⇒ Object
Returns the value of attribute retry_count.
7 8 9 |
# File 'lib/stillwater/connection_pool.rb', line 7 def retry_count @retry_count end |
Instance Method Details
#add(&builder) ⇒ Object
15 16 17 |
# File 'lib/stillwater/connection_pool.rb', line 15 def add(&builder) @pool << connection_info_from(builder) end |
#available_count ⇒ Object
81 82 83 |
# File 'lib/stillwater/connection_pool.rb', line 81 def available_count available.size end |
#checkin(conn) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/stillwater/connection_pool.rb', line 56 def checkin(conn) connection_info = in_use.detect { |info| info[:connection] == conn } connection_info[:state] = :available unless connection_info.nil? true end |
#checkout ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/stillwater/connection_pool.rb', line 48 def checkout connection_info = available.sample raise ConnectionNotAvailable if connection_info.nil? connection_info[:state] = :in_use connection_info[:connection] end |
#deactivate(conn) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/stillwater/connection_pool.rb', line 63 def deactivate(conn) connection_info = @pool.detect { |info| info[:connection] == conn } connection_info[:connection] = nil connection_info[:state] = :inactive start_polling true end |
#in_use_count ⇒ Object
89 90 91 |
# File 'lib/stillwater/connection_pool.rb', line 89 def in_use_count in_use.size end |
#inactive_count ⇒ Object
85 86 87 |
# File 'lib/stillwater/connection_pool.rb', line 85 def inactive_count inactive.size end |
#reactivate_all ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/stillwater/connection_pool.rb', line 72 def reactivate_all inactive.each do |info| info[:connection] = info[:builder].call info[:state] = :available end true end |
#retry_connection_from(exception_class, &block) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/stillwater/connection_pool.rb', line 35 def retry_connection_from(exception_class, &block) count = 0 conn = checkout yield conn rescue exception_class deactivate conn count += 1 raise if count >= @retry_count retry ensure checkin conn end |
#with_connection(&block) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/stillwater/connection_pool.rb', line 27 def with_connection(&block) conn = checkout result = yield conn checkin conn result end |