Skip to content

Commit 0905976

Browse files
binarycodeioquatix
authored andcommitted
add persistent option to allow automatic connection closing
1 parent a7d5434 commit 0905976

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/async/http/faraday/adapter.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,12 @@
2525
module Async
2626
module HTTP
2727
module Faraday
28-
# Detect whether we can use persistent connections:
29-
PERSISTENT = ::Faraday::Connection.instance_methods.include?(:close)
30-
3128
class Adapter < ::Faraday::Adapter
3229
def initialize(*arguments, **options, &block)
3330
super
3431

3532
@internet = Async::HTTP::Internet.new
33+
@persistent = ::Faraday::Connection.instance_methods.include?(:close) && options.fetch(:persistent, true)
3634
end
3735

3836
def close
@@ -49,7 +47,7 @@ def call(env)
4947
return @app.call(env)
5048
ensure
5149
# Don't retain persistent connections unless they will eventually be closed:
52-
@internet.close unless PERSISTENT
50+
@internet.close unless @persistent
5351
end
5452
end
5553
end

spec/async/http/faraday/adapter_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,13 @@ def get_response(url, path)
9191
expect(response.body).to be_nil
9292
end
9393
end
94+
95+
it 'closes connection automatically if persistent option is set to false' do
96+
run_server(Protocol::HTTP::Response[204]) do
97+
Faraday.new(url: endpoint.url) do |faraday|
98+
faraday.response :logger
99+
faraday.adapter :async_http, persistent: false
100+
end.get('/index')
101+
end
102+
end
94103
end

0 commit comments

Comments
 (0)