Class: Net::HTTP::Persistent::Pool

Inherits:
ConnectionPool
  • Object
show all
Defined in:
lib/net/http/persistent/pool.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Pool

Returns a new instance of Pool.



6
7
8
9
10
11
# File 'lib/net/http/persistent/pool.rb', line 6

def initialize(options = {}, &block)
  super

  @available = Net::HTTP::Persistent::TimedStackMulti.new(@size, &block)
  @key = "current-#{@available.object_id}"
end

Instance Attribute Details

#availableObject (readonly)

:nodoc:



3
4
5
# File 'lib/net/http/persistent/pool.rb', line 3

def available
  @available
end

#keyObject (readonly)

:nodoc:



4
5
6
# File 'lib/net/http/persistent/pool.rb', line 4

def key
  @key
end

Instance Method Details

#checkin(net_http_args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/net/http/persistent/pool.rb', line 13

def checkin net_http_args
  if net_http_args.is_a?(Hash) && net_http_args.size == 1 && net_http_args[:force]
    # ConnectionPool 2.4+ calls `checkin(force: true)` after fork.
    # When this happens, we should remove all connections from Thread.current
    if stacks = Thread.current[@key]
      stacks.each do |http_args, connections|
        connections.each do |conn|
          @available.push conn, connection_args: http_args
        end
        connections.clear
      end
    end
  else
    stack = Thread.current[@key][net_http_args] ||= []

    raise ConnectionPool::Error, 'no connections are checked out' if
      stack.empty?

    conn = stack.pop

    if stack.empty?
      @available.push conn, connection_args: net_http_args

      Thread.current[@key].delete(net_http_args)
      Thread.current[@key] = nil if Thread.current[@key].empty?
    end
  end
  nil
end

#checkout(net_http_args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/net/http/persistent/pool.rb', line 43

def checkout net_http_args
  stacks = Thread.current[@key] ||= {}
  stack  = stacks[net_http_args] ||= []

  if stack.empty? then
    conn = @available.pop connection_args: net_http_args
  else
    conn = stack.last
  end

  stack.push conn

  conn
end

#shutdownObject



58
59
60
61
# File 'lib/net/http/persistent/pool.rb', line 58

def shutdown
  Thread.current[@key] = nil
  super
end