/
em_synchrony.rb
153 lines (127 loc) · 4.29 KB
/
em_synchrony.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# frozen_string_literal: true
require 'uri'
module Faraday
class Adapter
# EventMachine Synchrony adapter.
class EMSynchrony < Faraday::Adapter
include EMHttp::Options
dependency do
require 'em-synchrony/em-http'
require 'em-synchrony/em-multi'
require 'fiber'
require 'faraday/adapter/em_synchrony/parallel_manager'
if Faraday::Adapter::EMSynchrony.loaded?
begin
require 'openssl'
rescue LoadError
warn 'Warning: no such file to load -- openssl. ' \
'Make sure it is installed if you want HTTPS support'
else
require 'em-http/version'
if EventMachine::HttpRequest::VERSION < '1.1.6'
require 'faraday/adapter/em_http_ssl_patch'
end
end
end
end
self.supports_parallel = true
# @return [ParallelManager]
def self.setup_parallel_manager(_options = nil)
ParallelManager.new
end
def call(env)
super
request = create_request(env)
http_method = env[:method].to_s.downcase.to_sym
if env[:parallel_manager]
# Queue requests for parallel execution.
execute_parallel_request(env, request, http_method)
else
# Execute single request.
execute_single_request(env, request, http_method)
end
@app.call env
rescue Errno::ECONNREFUSED
raise Faraday::ConnectionFailed, $ERROR_INFO
rescue EventMachine::Connectify::CONNECTError => e
if e.message.include?('Proxy Authentication Required')
raise Faraday::ConnectionFailed,
%(407 "Proxy Authentication Required")
end
raise Faraday::ConnectionFailed, e
rescue Errno::ETIMEDOUT => e
raise Faraday::TimeoutError, e
rescue RuntimeError => e
if e.message == 'connection closed by server'
raise Faraday::ConnectionFailed, e
end
raise Faraday::TimeoutError, e if e.message.include?('timeout error')
raise
rescue StandardError => e
if defined?(OpenSSL) && e.is_a?(OpenSSL::SSL::SSLError)
raise Faraday::SSLError, e
end
raise
end
def create_request(env)
EventMachine::HttpRequest.new(
Utils::URI(env[:url].to_s),
connection_config(env).merge(@connection_options)
)
end
private
def execute_parallel_request(env, request, http_method)
env[:parallel_manager].add(request, http_method,
request_config(env)) do |resp|
if (req = env[:request]).stream_response?
warn "Streaming downloads for #{self.class.name} " \
'are not yet implemented.'
req.on_data.call(resp.response, resp.response.bytesize)
end
save_response(env, resp.response_header.status,
resp.response) do |resp_headers|
resp.response_header.each do |name, value|
resp_headers[name.to_sym] = value
end
end
# Finalize the response object with values from `env`.
env[:response].finish(env)
end
end
def execute_single_request(env, request, http_method)
block = -> { request.send(http_method, request_config(env)) }
client = call_block(block)
raise client.error if client&.error
if env[:request].stream_response?
warn "Streaming downloads for #{self.class.name} " \
'are not yet implemented.'
env[:request].on_data.call(
client.response,
client.response.bytesize
)
end
status = client.response_header.status
reason = client.response_header.http_reason
save_response(env, status, client.response, nil, reason) do |headers|
client.response_header.each do |name, value|
headers[name.to_sym] = value
end
end
end
def call_block(block)
client = nil
if EM.reactor_running?
client = block.call
else
EM.run do
Fiber.new do
client = block.call
EM.stop
end.resume
end
end
client
end
end
end
end