Class: FinchAPI::Transport::PooledNetRequester Private

Inherits:
Object
  • Object
show all
Defined in:
lib/finch-api/transport/pooled_net_requester.rb

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.

Constant Summary collapse

KEEP_ALIVE_TIMEOUT =

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.

from the golang stdlib

https://github.com/golang/go/blob/c8eced8580028328fde7c03cbfcb720ce15b2358/src/net/http/transport.go#L49
30

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size: Etc.nprocessors) ⇒ PooledNetRequester

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 PooledNetRequester.

Parameters:

  • size (Integer) (defaults to: Etc.nprocessors)


175
176
177
178
179
# File 'lib/finch-api/transport/pooled_net_requester.rb', line 175

def initialize(size: Etc.nprocessors)
  @mutex = Mutex.new
  @size = size
  @pools = {}
end

Class Method Details

.build_request(request) {|| ... } ⇒ Net::HTTPGenericRequest

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.

Parameters:

  • request (Hash{Symbol=>Object})

    .

    @option request [Symbol] :method

    @option request [URI::Generic] :url

    @option request [HashString=>String] :headers

  • blk (Proc)

Yield Parameters:

  • (String)

Returns:

  • (Net::HTTPGenericRequest)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/finch-api/transport/pooled_net_requester.rb', line 57

def build_request(request, &)
  method, url, headers, body = request.fetch_values(:method, :url, :headers, :body)
  req = Net::HTTPGenericRequest.new(
    method.to_s.upcase,
    !body.nil?,
    method != :head,
    url.to_s
  )

  headers.each { req[_1] = _2 }

  case body
  in nil
    nil
  in String
    req["content-length"] ||= body.bytesize.to_s unless req["transfer-encoding"]
    req.body_stream = FinchAPI::Util::ReadIOAdapter.new(body, &)
  in StringIO
    req["content-length"] ||= body.size.to_s unless req["transfer-encoding"]
    req.body_stream = FinchAPI::Util::ReadIOAdapter.new(body, &)
  in IO | Enumerator
    req["transfer-encoding"] ||= "chunked" unless req["content-length"]
    req.body_stream = FinchAPI::Util::ReadIOAdapter.new(body, &)
  end

  req
end

.calibrate_socket_timeout(conn, deadline) ⇒ 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.

Parameters:

  • conn (Net::HTTP)
  • deadline (Float)


38
39
40
41
# File 'lib/finch-api/transport/pooled_net_requester.rb', line 38

def calibrate_socket_timeout(conn, deadline)
  timeout = deadline - FinchAPI::Util.monotonic_secs
  conn.open_timeout = conn.read_timeout = conn.write_timeout = conn.continue_timeout = timeout
end

.connect(url) ⇒ Net::HTTP

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.

Parameters:

  • url (URI::Generic)

Returns:

  • (Net::HTTP)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/finch-api/transport/pooled_net_requester.rb', line 17

def connect(url)
  port =
    case [url.port, url.scheme]
    in [Integer, _]
      url.port
    in [nil, "http" | "ws"]
      Net::HTTP.http_default_port
    in [nil, "https" | "wss"]
      Net::HTTP.https_default_port
    end

  Net::HTTP.new(url.host, port).tap do
    _1.use_ssl = %w[https wss].include?(url.scheme)
    _1.max_retries = 0
  end
end

Instance Method Details

#execute(request) ⇒ Array(Integer, Net::HTTPResponse, Enumerable)

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.

Parameters:

  • request (Hash{Symbol=>Object})

    .

    @option request [Symbol] :method

    @option request [URI::Generic] :url

    @option request [HashString=>String] :headers

    @option request [Object] :body

    @option request [Float] :deadline

Returns:

  • (Array(Integer, Net::HTTPResponse, Enumerable))


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/finch-api/transport/pooled_net_requester.rb', line 122

def execute(request)
  url, deadline = request.fetch_values(:url, :deadline)

  eof = false
  finished = false
  enum = Enumerator.new do |y|
    with_pool(url, deadline: deadline) do |conn|
      next if finished

      req = self.class.build_request(request) do
        self.class.calibrate_socket_timeout(conn, deadline)
      end

      self.class.calibrate_socket_timeout(conn, deadline)
      unless conn.started?
        conn.keep_alive_timeout = self.class::KEEP_ALIVE_TIMEOUT
        conn.start
      end

      self.class.calibrate_socket_timeout(conn, deadline)
      conn.request(req) do |rsp|
        y << [conn, req, rsp]
        break if finished

        rsp.read_body do |bytes|
          y << bytes
          break if finished

          self.class.calibrate_socket_timeout(conn, deadline)
        end
        eof = true
      end
    end
  rescue Timeout::Error
    raise FinchAPI::APITimeoutError
  end

  conn, _, response = enum.next
  body = FinchAPI::Util.fused_enum(enum, external: true) do
    finished = true
    tap do
      enum.next
    rescue StopIteration
      nil
    end
    conn.finish if !eof && conn&.started?
  end
  [Integer(response.code), response, (response.body = body)]
end