Skip to content
Tony Arcieri edited this page Jul 12, 2016 · 15 revisions

This gem provides no explicit mechanisms for thread-safe operation. That said, this gem actively avoids all global state, preferring to use an API built on immutable context objects which can be safely shared across threads.

Thread safety comes into play when you:

If you would like to open a thread-safe connection pool, we would recommend you use persistent connections and a third party connection pool gem, such as connection_pool, e.g.:

require "http"
require "connection_pool"

pool = ConnectionPool.new(size: 5, timeout: 5) do
  HTTP.persistent("https://github.com")
end

# Fetch multiple paths concurrently
responses = %w(/ruby /httprb /sparklemotion).map do |path|
  Thread.new do
    pool.with { |conn| conn.get(path).to_s }
  end.value
end

Unlike almost every other Ruby HTTP client (except native libraries like curl and EventMachine-based ones), this gem uses an asynchronous timeout backend which does not use the thread-unsafe timeout.rb library (except to timeout DNS resolution). This means (for the most part) this gem provides advanced thread-safe timeout support which is not commonly found in other Ruby HTTP libraries, including Net::HTTP and all of its wrappers/derivatives.

(NOTE: this gem previously supported Celluloid::IO, but that support was removed to add the current timeout backend. It may be added back in a future version)

Clone this wiki locally