Skip to content

Commit bab27db

Browse files
committed
Better support for running without top level reactor.
1 parent ab16988 commit bab27db

File tree

3 files changed

+56
-6
lines changed

3 files changed

+56
-6
lines changed

lib/async/http/faraday/adapter.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020

2121
require 'faraday'
2222
require 'faraday/adapter'
23+
require 'kernel/sync'
2324
require 'async/http/internet'
2425

26+
require_relative 'agent'
27+
2528
module Async
2629
module HTTP
2730
module Faraday
@@ -57,10 +60,20 @@ def close
5760
def call(env)
5861
super
5962

60-
with_timeout do
61-
response = @internet.call(env[:method].to_s.upcase, env[:url].to_s, env[:request_headers], env[:body] || [])
63+
parent = Async::Task.current?
6264

63-
save_response(env, response.status, response.read, response.headers)
65+
Sync do
66+
with_timeout do
67+
response = @internet.call(env[:method].to_s.upcase, env[:url].to_s, env[:request_headers], env[:body] || [])
68+
69+
save_response(env, response.status, response.read, response.headers)
70+
end
71+
ensure
72+
# If we are the top level task, even if we are persistent, we must close the connection:
73+
if parent.nil? || !@persistent
74+
Async.logger.debug(self) {"Closing persistent connections."}
75+
@internet.close
76+
end
6477
end
6578

6679
return @app.call(env)
@@ -70,9 +83,6 @@ def call(env)
7083
raise ::Faraday::SSLError, e
7184
rescue *CONNECTION_EXCEPTIONS => e
7285
raise ::Faraday::ConnectionFailed, e
73-
ensure
74-
# Don't retain persistent connections unless they will eventually be closed:
75-
@internet.close unless @persistent
7686
end
7787

7888
private

lib/async/http/faraday/agent.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
begin
22+
require 'sawyer/agent'
23+
24+
# This is a nasty hack until https://github.com/lostisland/sawyer/pull/67 is resolved:
25+
unless Sawyer::Agent.instance_methods.include?(:close)
26+
class Sawyer::Agent
27+
def close
28+
@conn.close if @conn.respond_to?(:close)
29+
end
30+
end
31+
end
32+
rescue LoadError
33+
# Ignore.
34+
end

spec/async/http/faraday/adapter_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ def get_response(url = endpoint.url, path = '/index', adapter_options: {})
7171
end
7272
end
7373

74+
it "works without top level reactor" do
75+
response = get_response("https://www.google.com", "/search?q=ruby")
76+
77+
expect(response).to be_success
78+
end
79+
7480
it "can get remote resource" do
7581
Async do
7682
response = get_response('http://www.google.com', '/search?q=cats')

0 commit comments

Comments
 (0)