Class: Roma::Messaging::ConPool

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/roma/messaging/con_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#maxlengthObject

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_allObject



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