class Kudzu::Agent::Http::ConnectionPool

Public Class Methods

new(max_size = 10) click to toggle source
# File lib/kudzu/agent/http/connection_pool.rb, line 5
def initialize(max_size = 10)
  @max_size = max_size
end

Public Instance Methods

checkout(name) { || ... } click to toggle source
# 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
close() click to toggle source
# 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

Private Instance Methods

finish_http(http) click to toggle source
# File lib/kudzu/agent/http/connection_pool.rb, line 44
def finish_http(http)
  http.finish if http && http.started?
end
pool() click to toggle source
# File lib/kudzu/agent/http/connection_pool.rb, line 31
def pool
  Thread.current[:kudzu_connection] ||= {}
  Thread.current[:kudzu_connection]
end
reduce() click to toggle source
# File lib/kudzu/agent/http/connection_pool.rb, line 36
def reduce
  conns = pool.values.sort_by { |conn| conn.last_used }
  conns.first(pool.size - @max_size).each do |conn|
    finish_http(conn.http)
    pool.delete(conn.name)
  end
end