30
30
module Async
31
31
module HTTP
32
32
module Faraday
33
- # Detect whether we can use persistent connections:
34
- PERSISTENT = ::Faraday ::Connection . instance_methods . include? ( :close )
35
-
36
33
class Adapter < ::Faraday ::Adapter
37
34
CONNECTION_EXCEPTIONS = [
38
35
Errno ::EADDRNOTAVAIL ,
@@ -46,12 +43,12 @@ class Adapter < ::Faraday::Adapter
46
43
IOError ,
47
44
SocketError
48
45
] . freeze
49
-
46
+
50
47
def initialize ( *arguments , **options , &block )
51
48
super
52
49
53
50
@internet = Async ::HTTP ::Internet . new
54
- @persistent = PERSISTENT && options . fetch ( :persistent , true )
51
+ @persistent = options . fetch ( :persistent , true )
55
52
@timeout = options [ :timeout ]
56
53
end
57
54
@@ -62,19 +59,12 @@ def close
62
59
def call ( env )
63
60
super
64
61
65
- parent = Async ::Task . current?
66
-
67
62
Sync do
68
63
with_timeout do
69
64
response = @internet . call ( env [ :method ] . to_s . upcase , env [ :url ] . to_s , env [ :request_headers ] , env [ :body ] || [ ] )
70
65
71
66
save_response ( env , response . status , response . read , response . headers )
72
67
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
78
68
end
79
69
80
70
return @app . call ( env )
@@ -85,9 +75,9 @@ def call(env)
85
75
rescue *CONNECTION_EXCEPTIONS => e
86
76
raise ::Faraday ::ConnectionFailed , e
87
77
end
88
-
78
+
89
79
private
90
-
80
+
91
81
def with_timeout ( task : Async ::Task . current )
92
82
if @timeout
93
83
task . with_timeout ( @timeout , ::Faraday ::TimeoutError ) do
0 commit comments