Skip to content

Commit 717f2e0

Browse files
committed
Persistence is now handled automatically.
1 parent 8117bef commit 717f2e0

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

lib/async/http/faraday/adapter.rb

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
module Async
3131
module HTTP
3232
module Faraday
33-
# Detect whether we can use persistent connections:
34-
PERSISTENT = ::Faraday::Connection.instance_methods.include?(:close)
35-
3633
class Adapter < ::Faraday::Adapter
3734
CONNECTION_EXCEPTIONS = [
3835
Errno::EADDRNOTAVAIL,
@@ -46,12 +43,12 @@ class Adapter < ::Faraday::Adapter
4643
IOError,
4744
SocketError
4845
].freeze
49-
46+
5047
def initialize(*arguments, **options, &block)
5148
super
5249

5350
@internet = Async::HTTP::Internet.new
54-
@persistent = PERSISTENT && options.fetch(:persistent, true)
51+
@persistent = options.fetch(:persistent, true)
5552
@timeout = options[:timeout]
5653
end
5754

@@ -62,19 +59,12 @@ def close
6259
def call(env)
6360
super
6461

65-
parent = Async::Task.current?
66-
6762
Sync do
6863
with_timeout do
6964
response = @internet.call(env[:method].to_s.upcase, env[:url].to_s, env[:request_headers], env[:body] || [])
7065

7166
save_response(env, response.status, response.read, response.headers)
7267
end
73-
ensure
74-
# If we are the top level task, even if we are persistent, we must close the connection:
75-
if parent.nil? || !@persistent
76-
@internet.close
77-
end
7868
end
7969

8070
return @app.call(env)
@@ -85,9 +75,9 @@ def call(env)
8575
rescue *CONNECTION_EXCEPTIONS => e
8676
raise ::Faraday::ConnectionFailed, e
8777
end
88-
78+
8979
private
90-
80+
9181
def with_timeout(task: Async::Task.current)
9282
if @timeout
9383
task.with_timeout(@timeout, ::Faraday::TimeoutError) do

0 commit comments

Comments
 (0)