class Roma::Messaging::ConPool
Attributes
expire_time[RW]
maxlength[RW]
Public Class Methods
new(maxlength = 10, expire_time = 30)
click to toggle source
# File lib/roma/messaging/con_pool.rb 15 def initialize(maxlength = 10, expire_time = 30) 16 @pool = {} 17 @maxlength = maxlength 18 @expire_time = expire_time 19 @lock = Mutex.new 20 end
Public Instance Methods
check_connection(ap)
click to toggle source
# File lib/roma/messaging/con_pool.rb 36 def check_connection(ap) 37 unless @pool.key?(ap) 38 host, port = ap.split(/[:_]/) 39 addr = DNSCache.instance.resolve_name(host) 40 sock = TCPSocket.open(addr, port) 41 sock.close 42 end 43 true 44 rescue Errno::ECONNREFUSED => e 45 false 46 rescue => e 47 Logging::RLogger.instance.error("#{__FILE__}:#{__LINE__}:#{e}") 48 end
close_all()
click to toggle source
# File lib/roma/messaging/con_pool.rb 80 def close_all 81 @pool.each_key{|ap| close_at(ap) } 82 end
close_at(ap)
click to toggle source
# File lib/roma/messaging/con_pool.rb 91 def close_at(ap) 92 return unless @pool.key?(ap) 93 @lock.synchronize { 94 while(@pool[ap].length > 0) 95 begin 96 @pool[ap].shift[0].close 97 rescue =>e 98 Roma::Logging::RLogger.instance.error("#{__FILE__}:#{__LINE__}:#{e}") 99 end 100 end 101 @pool.delete(ap) 102 } 103 end
close_same_host(ap)
click to toggle source
# File lib/roma/messaging/con_pool.rb 84 def close_same_host(ap) 85 host,port = ap.split(/[:_]/) 86 @pool.each_key{|eap| 87 close_at(eap) if eap.split(/[:_]/)[0] == host 88 } 89 end
create_connection(ap)
click to toggle source
# File lib/roma/messaging/con_pool.rb 70 def create_connection(ap) 71 host, port = ap.split(/[:_]/) 72 addr = DNSCache.instance.resolve_name(host) 73 TCPSocket.new(addr, port) 74 end
delete_connection(ap)
click to toggle source
# File lib/roma/messaging/con_pool.rb 76 def delete_connection(ap) 77 @pool.delete(ap) 78 end
get_connection(ap)
click to toggle source
# File lib/roma/messaging/con_pool.rb 22 def get_connection(ap) 23 ret,last = @pool[ap].shift if @pool.key?(ap) && @pool[ap].length > 0 24 if ret && @expire_time != 0 && last < Time.now - @expire_time 25 ret.close 26 ret = nil 27 Logging::RLogger.instance.info("connection expired at #{ap},remains #{@pool[ap].length}") 28 end 29 return create_connection(ap) unless ret 30 ret 31 rescue => e 32 Logging::RLogger.instance.error("#{__FILE__}:#{__LINE__}:#{e}") 33 nil 34 end
return_connection(ap, con)
click to toggle source
# File lib/roma/messaging/con_pool.rb 50 def return_connection(ap, con) 51 if select([con],nil,nil,0.0001) 52 con.gets 53 con.close 54 return 55 end 56 57 if @pool.key?(ap) && @pool[ap].length > 0 58 if @pool[ap].length > @maxlength 59 con.close 60 else 61 @pool[ap] << [con, Time.now] 62 end 63 else 64 @pool[ap] = [[con, Time.now]] 65 end 66 rescue => e 67 Logging::RLogger.instance.error("#{__FILE__}:#{__LINE__}:#{e}") 68 end