CodeSampleX

サンプル

faraday-net_http 3.4.4: Show how `in_parallel` behaves on `Faraday::Adapter::NetHttp` when the same shared connection is reused.

検証済みサンプル — gem faraday-net_http 3.4.4: Show how `in_parallel` behaves on `Faraday::Adapter::NetHttp` when the same shared connection is reused. ruby 3 …

sha256:8ed9c36daef7544ca2adfa54cee483a9e3f3f91105ad236314ecd621dc90e85e

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
2
ビルドした署名鍵
2
宣言された環境 ruby linux x64 ruby ruby bundle

検証実行環境

環境 コントラクト ステージ 実行日
ruby 3 · linux debian/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · rubygems@1
2026-08-17
ruby 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · rubygems@1
2026-08-18

ケース

HOW
ゴール
Show how `in_parallel` behaves on `Faraday::Adapter::NetHttp` when the same shared connection is reused.
パッケージ
シンボル
  • Faraday::Adapter::NetHttp
環境
ruby
作成日
2026-08-17T01:15:55Z

コントラクト

  1. In `in_parallel`, the second request starts at or after the first request has finished, showing that this adapter does not actually execute requests in parallel.
  2. The same shared `Faraday::Connection` can still overlap blocking requests when called from two Ruby threads directly, so it is not globally serialized by object reuse alone.

ファイル

  • Gemfile
  • Gemfile.lock
  • NOTES.md
  • csx.json
  • test/contract.rb

ソースアーティファクトをダウンロード (tar.gz)

ソース

Gemfile
source "https://rubygems.org"

gem "faraday-net_http", "3.4.4"
gem "faraday", "2.14.3"
Gemfile.lock
GEM
  remote: https://rubygems.org/
  specs:
    faraday (2.14.3)
      faraday-net_http (>= 2.0, < 3.5)
      json
      logger
    faraday-net_http (3.4.4)
      net-http (~> 0.5)
    json (2.9.1)
    logger (1.6.4)
    net-http (0.6.0)
      uri
    uri (1.0.4)

PLATFORMS
  ruby
  x64-mingw-ucrt

DEPENDENCIES
  faraday (= 2.14.3)
  faraday-net_http (= 3.4.4)

BUNDLED WITH
   2.6.9
NOTES.md
search_known_solution returned sample `case:sha256:351803bfa77c6ce359c495ca0a5a0b46d09ce6d00de295119bb5a00a6818df6c` for `pkg:gem/faraday-net_http@3.4.4` (`Faraday::Adapter::NetHttp`), which proves malformed URI behavior (`http://` vs invalid host) and is intentionally a different trap than this sample.

Wrong-implementation check: if a model assumes this adapter executes `in_parallel` concurrently, this sample fails loudly by observing the second request start no earlier than the first request finish plus the `in_parallel` warning that the adapter is not parallel capable.
csx.json
{"case":{"believed":"A confident model expects multiple requests inside a Net::HTTP Faraday `in_parallel` block to be dispatched concurrently.","caseId":"case:sha256:1fec71617103d6488f8779a204c6f701bd121c99dd36dbc1a8fe17df42bce845","contract":["In `in_parallel`, the second request starts at or after the first request has finished, showing that this adapter does not actually execute requests in parallel.","The same shared `Faraday::Connection` can still overlap blocking requests when called from two Ruby threads directly, so it is not globally serialized by object reuse alone."],"goal":"Show how `in_parallel` behaves on `Faraday::Adapter::NetHttp` when the same shared connection is reused.","kind":"HOW","packages":["pkg:gem/faraday-net_http@3.4.4","pkg:gem/faraday@2.14.3"],"schemaVersion":1,"symbols":["Faraday::Adapter::NetHttp"]},"contractCommand":["ruby","test/contract.rb"],"environment":{"arch":"x64","ecosystem":"gem","executionContext":"ruby","language":"ruby","os":"linux","packageManager":"bundle","runtime":"ruby","schemaVersion":1},"license":"MIT-0","packages":["pkg:gem/faraday-net_http@3.4.4","pkg:gem/faraday@2.14.3"],"schemaVersion":1,"symbols":["Faraday::Adapter::NetHttp"],"verifierAdapter":"rubygems@1"}
test/contract.rb
# frozen_string_literal: true

require 'socket'
require 'uri'
require 'faraday'
require 'stringio'

DELAY_SECONDS = 0.8
HOST = '127.0.0.1'

def assert(condition, message)
  raise RuntimeError, message unless condition
end

def parse_event_time(events, mutex, name, phase)
  mutex.synchronize do
    entry = events.find { |e| e[0] == name && e[1] == phase }
    entry && entry[2]
  end
end

events = []
events_mutex = Mutex.new
server = TCPServer.new(HOST, 0)
port = server.addr[1]

acceptor = Thread.new do
  loop do
    begin
      client = server.accept
      Thread.new(client) do |sock|
        begin
          request_line = sock.gets
          break unless request_line

          while (line = sock.gets)
            break if line == "\r\n"
          end

          path = request_line.split[1].to_s
          query = URI.parse(path).query.to_s
          params = URI.decode_www_form(query).to_h
          delay = params.fetch('delay', '0').to_f
          name = params.fetch('name', 'unnamed')

          events_mutex.synchronize do
            events << [name, :start, Process.clock_gettime(:CLOCK_MONOTONIC)]
          end

          sleep delay

          events_mutex.synchronize do
            events << [name, :finish, Process.clock_gettime(:CLOCK_MONOTONIC)]
          end

          body = 'ok'
          sock.write(
            "HTTP/1.1 200 OK\r\n" \
            "Content-Length: #{body.bytesize}\r\n" \
            "Connection: close\r\n\r\n" \
            "#{body}"
          )
        ensure
          sock.close
        end
      end
    rescue IOError
      break
    end
  end
end

def with_warning_buffer
  buffer = StringIO.new
  original_stderr = $stderr
  $stderr = buffer
  yield
  buffer.string
ensure
  $stderr = original_stderr
end

begin
connection = Faraday.new(url: URI::HTTP.build(host: HOST, port: port).to_s) do |builder|
  builder.adapter :net_http
end

events_mutex.synchronize { events.clear }
warning = with_warning_buffer do
  connection.in_parallel do
    connection.get("/sleep?name=first&delay=#{DELAY_SECONDS}")
    connection.get("/sleep?name=second&delay=#{DELAY_SECONDS}")
  end
end

first_finish = parse_event_time(events, events_mutex, 'first', :finish)
second_start = parse_event_time(events, events_mutex, 'second', :start)
assert(
  !warning.empty?,
  'expected in_parallel to emit a warning that Net::HTTP adapter has no parallel support'
)
assert(
  first_finish && second_start && second_start >= first_finish,
  'in_parallel requests on Net::HTTP were expected to be sequential, but they were not'
)

events_mutex.synchronize { events.clear }
threaded_start = Process.clock_gettime(:CLOCK_MONOTONIC)
thread_a = Thread.new do
  connection.get("/sleep?name=thread_a&delay=#{DELAY_SECONDS}")
end
thread_b = Thread.new do
  connection.get("/sleep?name=thread_b&delay=#{DELAY_SECONDS}")
end
[thread_a, thread_b].each(&:join)
threaded_duration = Process.clock_gettime(:CLOCK_MONOTONIC) - threaded_start

thread_a_start = parse_event_time(events, events_mutex, 'thread_a', :start)
thread_b_start = parse_event_time(events, events_mutex, 'thread_b', :start)

assert(
  thread_a_start && thread_b_start && threaded_duration < 1.4,
  "shared connection threads did not overlap (elapsed=#{threaded_duration.round(3)}s)"
)
assert(
  (thread_a_start - thread_b_start).abs < DELAY_SECONDS + 0.1,
  'shared connection thread scheduling did not start requests close enough to demonstrate overlap'
)
puts "contract passed"
ensure
  server.close
  begin
    TCPSocket.new(HOST, port).close
  rescue StandardError
  end
  acceptor.join(1)
end

オリジンシーダー

csx-seed