Faraday Adapter

If you’re using faraday for your http-related interactions, you can use httpx as a backend: according to awesome faraday, it’s the only option that “ticks all the boxes” (streaming, parallel requests, etc…).

For that, you just have to require "httpx/adapters/faraday" and set :httpx as the adapter:

require "httpx/adapters/faraday"

# building a custom faraday client using HTTPX

client = Faraday.new do |builder|
  # middlewares
  builder.use Faraday::Request::Multipart
  builder.use Faraday::Request::UrlEncoded
  builder.use Faraday::Response::RaiseError
  # ...

  builder.adapter :httpx
end

Below is an example of using httpx with the elasticsearch gem, which uses faraday:

# reference: https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/8.9/transport.html
require "httpx/adapters/faraday"
require "elastic/transport"

client = Elastic::Transport::Client.new(adapter: :httpx)

Considerations

The faraday adapter uses the persistent plugin by default.

If you’re using your faraday client to perform requests on multiple servers in the context of a long-lived, multi-threaded environment, and you start getting the Errno::EMFILE - too many open files, that’s because, due to the persistent plugin being turned on, sockets are never closed. In case this bites you, you’ll have to turn persistence off in your client, in this way:

# ...
Faraday.new do |builder|
  builder.adapter :httpx, persistent: false
end

If you know of other use cases where these might be an impediment, or you’d like to suggest more default plugins for it, do let me know.

Next: Datadog