Class: Kudzu::Agent::Http::ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/kudzu/agent/http/connection_pool.rb

Instance Method Summary collapse

Constructor Details

#initialize(max_size = 10) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.


5
6
7
# File 'lib/kudzu/agent/http/connection_pool.rb', line 5

def initialize(max_size = 10)
  @max_size = max_size
end

Instance Method Details

#checkout(name) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/kudzu/agent/http/connection_pool.rb', line 9

def checkout(name)
  pool[name] ||= Connection.new(name: name, http: yield)

  conn = pool[name]
  conn.last_used = Time.now

  if pool.size > @max_size
    reduce
  end

  conn.http
end

#closeObject


22
23
24
25
26
27
# File 'lib/kudzu/agent/http/connection_pool.rb', line 22

def close
  pool.values.each do |conn|
    finish_http(conn.http)
  end
  Thread.current[:kudzu_connection] = nil
end