5

I have a method like this:

def make_request(path, params, body)
  raise ArgumentError.new('Endpoint not set!') if url.nil?
  conditions          = {url: url}
  conditions[:params] = params unless params.blank?
  connection          = Faraday::Connection.new(conditions)
  connection.run_request(:get, path, body, {'Content-Type' => 'application/json'})
end

Then how can I add keep-alive there? Also, since I instantiate a connection object everytime I call this method(url might be different), does the keep-alive parameter still work?

milodky
  • 443
  • 2
  • 7
  • 18

1 Answers1

6

I found something here and here, but not tested it myself.

Faraday.new(uri) do |f|
  f.adapter :net_http_persistent
end

You can keep the connection creating a new method "connection"

def connection
  @connection ||= Faraday.new(@url_without_path) do |f|
                    f.adapter :net_http_persistent
                  end
end
Ronie
  • 530
  • 5
  • 9