class Roma::Event::EMConPool

Attributes

expire_time[RW]
maxlength[RW]
pool[R]

Public Class Methods

new() click to toggle source
   # File lib/roma/event/con_pool.rb
84 def initialize
85   @pool = {}
86   @maxlength = 30
87   @expire_time = 30
88   @lock = Mutex.new
89 end

Public Instance Methods

close_all() click to toggle source
    # File lib/roma/event/con_pool.rb
125 def close_all
126   @pool.each_key{|ap| close_at(ap) }
127 end
close_at(ap) click to toggle source
    # File lib/roma/event/con_pool.rb
136 def close_at(ap)
137   return unless @pool.key?(ap)
138   @lock.synchronize {
139     while(@pool[ap].length > 0)
140       begin
141         @pool[ap].shift.close_connection
142       rescue =>e
143         Roma::Logging::RLogger.instance.error("#{__FILE__}:#{__LINE__}:#{e.inspect}")
144       end
145     end
146     @pool.delete(ap)
147   }
148 end
close_same_host(ap) click to toggle source
    # File lib/roma/event/con_pool.rb
129 def close_same_host(ap)
130   host,port = ap.split(/[:_]/)
131   @pool.each_key{|eap|
132     close_at(eap) if eap.split(/[:_]/)[0] == host
133   }
134 end
create_connection(ap) click to toggle source
    # File lib/roma/event/con_pool.rb
117 def create_connection(ap)
118   host,port = ap.split(/[:_]/)
119   addr = DNSCache.instance.resolve_name(host)
120   con = EventMachine::connect(addr, port, Roma::Event::EMConnection)
121   con.ap = ap
122   con
123 end
get_connection(ap) click to toggle source
    # File lib/roma/event/con_pool.rb
 91 def get_connection(ap)
 92   ret = @pool[ap].shift if @pool.key?(ap) && @pool[ap].length > 0
 93   if ret && @expire_time != 0 && ret.last_access < Time.now - @expire_time
 94     ret.close_connection if ret.connected
 95     ret = nil
 96     Logging::RLogger.instance.info("EM connection expired at #{ap},remains #{@pool[ap].length}")
 97   end
 98   ret = create_connection(ap) if ret == nil || ret.connected != true
 99   ret
100 end
return_connection(ap, con) click to toggle source
    # File lib/roma/event/con_pool.rb
102 def return_connection(ap, con)
103   return if con.connected == false
104 
105   con.last_access = Time.now
106   if @pool.key?(ap) && @pool[ap].length > 0
107     if @pool[ap].length > @maxlength
108       con.close_connection
109     else
110       @pool[ap] << con
111     end
112   else
113     @pool[ap] = [con]
114   end
115 end