Class: Puppet::Network::HTTP::Pool Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/network/http/pool.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A pool for persistent Net::HTTP connections. Connections are stored in the pool indexed by their Site. Connections are borrowed from the pool, yielded to the caller, and released back into the pool. If a connection is expired, it will be closed either when a connection to that site is requested, or when the pool is closed. The pool can store multiple connections to the same site, and will be reused in MRU order.

Constant Summary collapse

FIFTEEN_SECONDS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

15

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keepalive_timeout = FIFTEEN_SECONDS) ⇒ Pool

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Pool.



16
17
18
19
20
# File 'lib/puppet/network/http/pool.rb', line 16

def initialize(keepalive_timeout = FIFTEEN_SECONDS)
  @pool = {}
  @factory = Puppet::Network::HTTP::Factory.new
  @keepalive_timeout = keepalive_timeout
end

Instance Attribute Details

#factoryObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



14
15
16
# File 'lib/puppet/network/http/pool.rb', line 14

def factory
  @factory
end

Instance Method Details

#active_sessions(site) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns an Array of sessions whose connections are not expired.



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/puppet/network/http/pool.rb', line 116

def active_sessions(site)
  now = Time.now

  sessions = @pool[site] || []
  sessions.select do |session|
    if session.expired?(now)
      close_connection(site, session.connection)
      false
    else
      true
    end
  end
end

#borrow(site, verify) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Borrow and take ownership of a persistent connection. If a new connection is created, it will be started prior to being returned.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet/network/http/pool.rb', line 72

def borrow(site, verify)
  @pool[site] = active_sessions(site)
  session = @pool[site].shift
  if session
    Puppet.debug("Using cached connection for #{site}")
    session.connection
  else
    http = @factory.create_connection(site)
    verify.setup_connection(http)

    Puppet.debug("Starting connection for #{site}")
    http.start
    setsockopts(http.instance_variable_get(:@socket))
    http
  end
end

#closeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
47
48
49
50
51
# File 'lib/puppet/network/http/pool.rb', line 44

def close
  @pool.each_pair do |site, sessions|
    sessions.each do |session|
      close_connection(site, session.connection)
    end
  end
  @pool.clear
end

#close_connection(site, http) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Safely close a persistent connection.



61
62
63
64
65
66
# File 'lib/puppet/network/http/pool.rb', line 61

def close_connection(site, http)
  Puppet.debug("Closing connection for #{site}")
  http.finish
rescue => detail
  Puppet.log_exception(detail, _("Failed to close connection for %{site}: %{detail}") % { site: site, detail: detail })
end

#poolObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
# File 'lib/puppet/network/http/pool.rb', line 54

def pool
  @pool
end

#release(site, http) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Release a connection back into the pool.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/puppet/network/http/pool.rb', line 100

def release(site, http)
  expiration = Time.now + @keepalive_timeout
  session = Puppet::Network::HTTP::Session.new(http, expiration)
  Puppet.debug("Caching connection for #{site}")

  sessions = @pool[site]
  if sessions
    sessions.unshift(session)
  else
    @pool[site] = [session]
  end
end

#setsockopts(netio) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set useful socket option(s) which lack from default settings in Net:HTTP



92
93
94
95
# File 'lib/puppet/network/http/pool.rb', line 92

def setsockopts(netio)
  socket = netio.io
  socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true)
end

#with_connection(site, verify, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet/network/http/pool.rb', line 22

def with_connection(site, verify, &block)
  reuse = true

  http = borrow(site, verify)
  begin
    if http.use_ssl? && http.verify_mode != OpenSSL::SSL::VERIFY_PEER
      reuse = false
    end

    yield http
  rescue => detail
    reuse = false
    raise detail
  ensure
    if reuse
      release(site, http)
    else
      close_connection(site, http)
    end
  end
end