9

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
Yeonho
  • 3,629
  • 4
  • 39
  • 61

2 Answers2

4

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.

Sean Redmond
  • 3,974
  • 22
  • 28
4

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.

kuboon
  • 9,557
  • 3
  • 42
  • 32