Skip to content
Pedro Nascimento edited this page Mar 18, 2022 · 15 revisions

Warning: Check https://github.com/httprb/http/issues/558 for thread safety. As of March 2022, HTTP is not thread safe.

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:

require "http"
require "connection_pool"

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

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

(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