Class: Roma::Messaging::ConPool
- Inherits:
-
Object
- Object
- Roma::Messaging::ConPool
- Includes:
- Singleton
- Defined in:
- lib/roma/messaging/con_pool.rb
Instance Attribute Summary collapse
-
#maxlength ⇒ Object
Returns the value of attribute maxlength.
Instance Method Summary collapse
- #close_all ⇒ Object
- #close_at(ap) ⇒ Object
- #close_same_host(ap) ⇒ Object
- #create_connection(ap) ⇒ Object
- #delete_connection(ap) ⇒ Object
- #get_connection(ap) ⇒ Object
-
#initialize(maxlength = 10) ⇒ ConPool
constructor
A new instance of ConPool.
- #return_connection(ap, con) ⇒ Object
Constructor Details
#initialize(maxlength = 10) ⇒ ConPool
Returns a new instance of ConPool.
13 14 15 16 17 |
# File 'lib/roma/messaging/con_pool.rb', line 13 def initialize(maxlength = 10) @pool = {} @maxlength = maxlength @lock = Mutex.new end |
Instance Attribute Details
#maxlength ⇒ Object
Returns the value of attribute maxlength.
11 12 13 |
# File 'lib/roma/messaging/con_pool.rb', line 11 def maxlength @maxlength end |
Instance Method Details
#close_all ⇒ Object
49 50 51 |
# File 'lib/roma/messaging/con_pool.rb', line 49 def close_all @pool.each_key{|ap| close_at(ap) } end |
#close_at(ap) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/roma/messaging/con_pool.rb', line 60 def close_at(ap) return unless @pool.key?(ap) @lock.synchronize { while(@pool[ap].length > 0) begin @pool[ap].shift.close rescue =>e Roma::Logging::RLogger.instance.error("#{__FILE__}:#{__LINE__}:#{e}") end end @pool.delete(ap) } end |
#close_same_host(ap) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/roma/messaging/con_pool.rb', line 53 def close_same_host(ap) host,port = ap.split(/[:_]/) @pool.each_key{|eap| close_at(eap) if eap.split(/[:_]/)[0] == host } end |
#create_connection(ap) ⇒ Object
40 41 42 43 |
# File 'lib/roma/messaging/con_pool.rb', line 40 def create_connection(ap) addr, port = ap.split(/[:_]/) TCPSocket.new(addr, port) end |
#delete_connection(ap) ⇒ Object
45 46 47 |
# File 'lib/roma/messaging/con_pool.rb', line 45 def delete_connection(ap) @pool.delete(ap) end |
#get_connection(ap) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/roma/messaging/con_pool.rb', line 19 def get_connection(ap) ret = @pool[ap].shift if @pool.key?(ap) && @pool[ap].length > 0 ret = create_connection(ap) unless ret ret rescue nil end |
#return_connection(ap, con) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/roma/messaging/con_pool.rb', line 27 def return_connection(ap, con) if @pool.key?(ap) && @pool[ap].length > 0 if @pool[ap].length > @maxlength con.close else @pool[ap] << con end else @pool[ap] = [con] end rescue end |