Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to reuse Faraday connection objects?

Tags:

http

ruby

faraday

Is it safe to reuse Faraday connection objects, or is it better to recreate them every time?

def connection
  @connection ||= Faraday.new('http://example.com') do |conn|
    conn.request :url_encoded
    # more configuration
  end
end
like image 401
Yeonho Avatar asked Sep 26 '22 04:09

Yeonho


2 Answers

I think it's safe to reuse them (I have, a lot). I don't see it really covered one way or another in the documentation but the presence of "Per-request options" (as opposed to per-connection) at least implies that you can rely on making multiple requests with the same connection.

like image 159
Sean Redmond Avatar answered Sep 28 '22 04:09

Sean Redmond


https://github.com/lostisland/faraday/blob/52e30bf8e8d79159f332088189cb7f7e536d1ba1/lib/faraday/connection.rb#L502

connection.get .post and all other methods duplicates params etc here. It means each request shares nothing with each other and the parent Connection object.

It's safe to reuse.

like image 43
kuboon Avatar answered Sep 28 '22 04:09

kuboon