Class: Kilden::Transport::NetHttp Private

Inherits:
Object
  • Object
show all
Defined in:
lib/kilden/transport.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.

Default transport on Net::HTTP. One connection per request: the SDK flushes at most every few seconds, so pooling buys nothing and costs state that would go stale across forks.

Instance Method Summary collapse

Constructor Details

#initialize(timeout:) ⇒ NetHttp

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



23
24
25
# File 'lib/kilden/transport.rb', line 23

def initialize(timeout:)
  @timeout = timeout
end

Instance Method Details

#post(url, body, headers) ⇒ 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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kilden/transport.rb', line 27

def post(url, body, headers)
  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = @timeout
  http.read_timeout = @timeout
  http.write_timeout = @timeout if http.respond_to?(:write_timeout=)

  response = http.post(uri.path.empty? ? "/" : uri.path, body, headers)
  normalized = {}
  response.each_header { |k, v| normalized[k.downcase] = v }
  payload = response.body.to_s
  # A body shorter than Content-Length is a connection cut mid-response
  # (Net::HTTP returns the partial read silently). Malformed HTTP is a
  # network error per SPEC ยง4.3, so the batch retries.
  declared = normalized["content-length"]&.to_i
  if declared && payload.bytesize < declared
    return Response.new(status: 0, headers: normalized, body: payload,
                        error: EOFError.new("response truncated at #{payload.bytesize}/#{declared} bytes"))
  end
  Response.new(status: response.code.to_i, headers: normalized, body: payload)
rescue StandardError => e
  Response.new(status: 0, headers: {}, body: "", error: e)
ensure
  begin
    http&.finish if http&.started?
  rescue StandardError
    nil
  end
end