class Roma::Client::ConPool
Attributes
expire_time[RW]
maxlength[RW]
Public Class Methods
new(maxlength = 10, expire_time = 60)
click to toggle source
# File lib/roma/client/con_pool.rb 14 def initialize(maxlength = 10, expire_time = 60) 15 @pool = {} 16 @maxlength = maxlength 17 @expire_time = expire_time 18 @lock = Mutex.new 19 end
Public Instance Methods
close_all()
click to toggle source
# File lib/roma/client/con_pool.rb 55 def close_all 56 @pool.each_key{|ap| close_at(ap) } 57 end
close_at(ap)
click to toggle source
# File lib/roma/client/con_pool.rb 66 def close_at(ap) 67 return unless @pool.key?(ap) 68 @lock.synchronize { 69 while(@pool[ap].length > 0) 70 begin 71 @pool[ap].shift.close 72 rescue =>e 73 end 74 end 75 @pool.delete(ap) 76 } 77 end
close_same_host(ap)
click to toggle source
# File lib/roma/client/con_pool.rb 59 def close_same_host(ap) 60 host,port = ap.split(/[:_]/) 61 @pool.each_key{|eap| 62 close_at(eap) if eap.split(/[:_]/)[0] == host 63 } 64 end
create_connection(ap)
click to toggle source
# File lib/roma/client/con_pool.rb 46 def create_connection(ap) 47 addr, port = ap.split(/[:_]/) 48 TCPSocket.new(addr, port) 49 end
delete_connection(ap)
click to toggle source
# File lib/roma/client/con_pool.rb 51 def delete_connection(ap) 52 @pool.delete(ap) 53 end
get_connection(ap)
click to toggle source
# File lib/roma/client/con_pool.rb 21 def get_connection(ap) 22 ret,last = @pool[ap].shift if @pool.key?(ap) && @pool[ap].length > 0 23 if ret && last < Time.now - @expire_time 24 ret.close 25 ret = nil 26 end 27 ret = create_connection(ap) unless ret 28 ret 29 rescue 30 nil 31 end
return_connection(ap, con)
click to toggle source
# File lib/roma/client/con_pool.rb 33 def return_connection(ap, con) 34 if @pool.key?(ap) && @pool[ap].length > 0 35 if @pool[ap].length > @maxlength 36 con.close 37 else 38 @pool[ap] << [con, Time.now] 39 end 40 else 41 @pool[ap] = [[con, Time.now]] 42 end 43 rescue 44 end