Class: Roma::Client::Proxy::Conpool

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/roma/client/proxy/daemon.rb

Overview

module ClientHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConpool

Returns a new instance of Conpool.



79
80
81
82
83
# File 'lib/roma/client/proxy/daemon.rb', line 79

def initialize
  @pool = {}
  @maxlength = 10
  @lock = Mutex.new
end

Instance Attribute Details

#maxlengthObject

Returns the value of attribute maxlength.



77
78
79
# File 'lib/roma/client/proxy/daemon.rb', line 77

def maxlength
  @maxlength
end

Instance Method Details

#close_allObject



111
112
113
# File 'lib/roma/client/proxy/daemon.rb', line 111

def close_all
  @pool.each_key{|ap| close_at(ap) }
end

#close_at(ap) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/roma/client/proxy/daemon.rb', line 115

def close_at(ap)
  return unless @pool.key?(ap)
  @lock.synchronize {
    while(@pool[ap].length > 0)
      begin
        @pool[ap].shift.close_connection
      rescue =>e
        $log.error("#{e} #{$@}")
      end
    end
    @pool.delete(ap)
  }
end

#create_connection(ap, handler) ⇒ Object



104
105
106
107
108
109
# File 'lib/roma/client/proxy/daemon.rb', line 104

def create_connection(ap, handler)
  addr,port = ap.split('_')
  con = EventMachine::connect(addr, port, handler)
  con.ap = ap
  con
end

#get_connection(ap, handler) ⇒ Object



85
86
87
88
89
# File 'lib/roma/client/proxy/daemon.rb', line 85

def get_connection(ap, handler)
  ret = @pool[ap].shift if @pool.key?(ap) && @pool[ap].length > 0
  ret = create_connection(ap, handler) if ret == nil
  ret
end

#return_connection(con) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/roma/client/proxy/daemon.rb', line 91

def return_connection(con)
  return unless con.connected
  if @pool.key?(con.ap) && @pool[con.ap].length > 0
    if @pool[con.ap].length > @maxlength
      con.close_connection
    else
      @pool[con.ap] << con
    end
  else
    @pool[con.ap] = [con]
  end
end