简体   繁体   中英

keep-alive in ruby faraday

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM