Class: ElasticGraph::Support::FaradayMiddleware::SupportTimeouts

Inherits:
Data
  • Object
show all
Defined in:
lib/elastic_graph/support/faraday_middleware/support_timeouts.rb

Overview

Faraday supports specifying a timeout at both the client level (when building the Faraday connection) or on a per-request basis. We want to specify it on a per-request basis, but unfortunately, the Elasticsearch/OpenSearch clients don’t provide any per-request API to specify the timeout (it only supports it when instantiating your client).

This middleware helps us work around this deficiency by looking for the TIMEOUT_MS_HEADER. If present, it deletes it from the headers and instead sets it as the request timeout.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app

Returns:

  • (Object)

    the current value of app



24
25
26
# File 'lib/elastic_graph/support/faraday_middleware/support_timeouts.rb', line 24

def app
  @app
end

Instance Method Details

#call(env) ⇒ Faraday::Response

Processes a Faraday request, extracting timeout from headers and applying it to the request. Converts TIMEOUT_MS_HEADER from request headers into a Faraday request timeout setting.

Parameters:

  • env (Faraday::Env)

    the Faraday request environment

Returns:

  • (Faraday::Response)

    the response from the next middleware in the stack

Raises:



33
34
35
36
37
38
39
40
41
# File 'lib/elastic_graph/support/faraday_middleware/support_timeouts.rb', line 33

def call(env)
  if (timeout_ms = env.request_headers.delete(TIMEOUT_MS_HEADER))
    env.request.timeout = Integer(timeout_ms) / 1000.0
  end

  app.call(env)
rescue ::Faraday::TimeoutError
  raise Errors::RequestExceededDeadlineError, "Datastore request exceeded timeout of #{timeout_ms} ms."
end