Class: Tanker::Http::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tanker/core/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sdk_type, sdk_version, faraday_adapter) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/tanker/core/http.rb', line 131

def initialize(sdk_type, sdk_version, faraday_adapter)
  @sdk_type = sdk_type
  @sdk_version = sdk_version
  @conn = Faraday.new do |conn|
    conn.adapter faraday_adapter || Faraday.default_adapter
  end

  # This could be a proc, but for some reason, ffi gives the wrong type
  # for crequest if we don't specify it explicitly here
  @c_send_request = FFI::Function.new(:pointer, [CTanker::CHttpRequest.by_ref, :pointer]) do |crequest, cdata|
    next send_request crequest, cdata
  rescue Exception => e # rubocop:disable Lint/RescueException I do want to rescue all exceptions
    cresponse = CTanker::CHttpResponse.new_error e.message
    CTanker.tanker_http_handle_response(crequest, cresponse)
  end
  @c_cancel_request = proc do |crequest, request_id, cdata|
    cancel_request crequest, request_id, cdata
  rescue Exception => e # rubocop:disable Lint/RescueException I do want to rescue all exceptions
    # This is not recoverable and won't be logged by FFI, let's do our best and log it here just before we crash
    puts "fatal error when canceling HTTP request:\n#{e.full_message}"
    raise
  end

  @tanker_http_options = CTanker::CHttpOptions.new @c_send_request, @c_cancel_request
end

Instance Attribute Details

#tanker_http_optionsObject (readonly)

Returns the value of attribute tanker_http_options.



129
130
131
# File 'lib/tanker/core/http.rb', line 129

def tanker_http_options
  @tanker_http_options
end

Instance Method Details

#cancel_request(_crequest, prequest_id, _cdata) ⇒ Object



202
203
204
205
# File 'lib/tanker/core/http.rb', line 202

def cancel_request(_crequest, prequest_id, _cdata)
  request_id = prequest_id.to_i
  HttpRequest.cancel(request_id)
end

#process_request(request) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/tanker/core/http.rb', line 157

def process_request(request)
  headers = Faraday::Utils::Headers.new

  request.headers.each do |header|
    # Faraday stores identical headers as a comma separated string
    headers[header.name] = [headers[header.name], header.value].compact
  end

  # overwrite sdk-native headers
  headers['X-Tanker-SdkType'] = @sdk_type
  headers['X-Tanker-SdkVersion'] = @sdk_version

  fresponse = Faraday.run_request(request.method, request.url, request.body, headers)

  request.complete_if_not_canceled do
    # Faraday stores identical headers as a comma separated string
    # So we will only see a single header in sdk-native
    headers = fresponse.headers.map do |name, value|
      HttpHeader.new name, value
    end
    cresponse = CTanker::CHttpResponse.new_ok status_code: fresponse.status,
                                              headers:,
                                              body: fresponse.body
    CTanker.tanker_http_handle_response(request.crequest, cresponse)
  end
rescue Faraday::ConnectionFailed => e
  # This can happen if Faraday is using a proxy, and it rejects the request
  # If we get a 500 from the proxy, we want to differentiate this from a real server error
  cresponse = CTanker::CHttpResponse.new_error "#{e.class}: #{e.message}"
  CTanker.tanker_http_handle_response(request.crequest, cresponse)
rescue Exception => e # rubocop:disable Lint/RescueException I do want to rescue all exceptions
  # NOTE: when debugging, you might want to uncomment this to print a full backtrace
  # puts "HTTP request error:\n#{e.full_message}"
  cresponse = CTanker::CHttpResponse.new_error e.message
  CTanker.tanker_http_handle_response(request.crequest, cresponse)
end

#send_request(crequest, _cdata) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/tanker/core/http.rb', line 194

def send_request(crequest, _cdata)
  request = HttpRequest.new(crequest:)
  ThreadPool.push(proc do
    process_request request
  end)
  FFI::Pointer.new :void, request.id
end