lib/httpx/session.rb in httpx-0.6.7 vs lib/httpx/session.rb in httpx-0.7.0
- old
+ new
@@ -58,36 +58,47 @@
def find_connection(request, connections, options)
uri = URI(request.uri)
connection = pool.find_connection(uri, options) || build_connection(uri, options)
unless connections.nil? || connections.include?(connection)
connections << connection
- set_connection_callbacks(connection, options)
+ set_connection_callbacks(connection, connections, options)
end
connection
end
- def set_connection_callbacks(connection, options)
+ def set_connection_callbacks(connection, connections, options)
connection.on(:uncoalesce) do |uncoalesced_uri|
other_connection = build_connection(uncoalesced_uri, options)
+ connections << other_connection
connection.unmerge(other_connection)
end
connection.on(:altsvc) do |alt_origin, origin, alt_params|
- build_altsvc_connection(connection, alt_origin, origin, alt_params, options)
+ other_connection = build_altsvc_connection(connection, connections, alt_origin, origin, alt_params, options)
+ connections << other_connection if other_connection
end
+ connection.on(:exhausted) do
+ other_connection = connection.create_idle
+ other_connection.merge(connection)
+ catch(:coalesced) do
+ pool.init_connection(other_connection, options)
+ end
+ set_connection_callbacks(other_connection, connections, options)
+ connections << other_connection
+ end
end
- def build_altsvc_connection(existing_connection, alt_origin, origin, alt_params, options)
+ def build_altsvc_connection(existing_connection, connections, alt_origin, origin, alt_params, options)
altsvc = AltSvc.cached_altsvc_set(origin, alt_params.merge("origin" => alt_origin))
# altsvc already exists, somehow it wasn't advertised, probably noop
return unless altsvc
connection = pool.find_connection(alt_origin, options) || build_connection(alt_origin, options)
# advertised altsvc is the same origin being used, ignore
return if connection == existing_connection
- set_connection_callbacks(connection, options)
+ set_connection_callbacks(connection, connections, options)
log(level: 1) { "#{origin} alt-svc: #{alt_origin}" }
# get uninitialized requests
# incidentally, all requests will be re-routed to the first
@@ -97,12 +108,14 @@
request.state == :idle &&
!request.headers.key?("alt-used")
end
connection.merge(existing_connection)
+ connection
rescue UnsupportedSchemeError
altsvc["noop"] = true
+ nil
end
def build_requests(*args, options)
request_options = @options.merge(options)
@@ -153,11 +166,10 @@
requests.each do |request|
error = catch(:resolve_error) do
connection = find_connection(request, connections, request_options)
connection.send(request)
- set_request_timeout(connection, request, request_options)
end
next unless error.is_a?(ResolveError)
request.emit(:response, ErrorResponse.new(request, error, options))
end
@@ -187,24 +199,9 @@
rklass = @options.request_class
request = rklass.new(verb, uri, @options.merge(options))
request.on(:response, &method(:on_response).curry[request])
request.on(:promise, &method(:on_promise))
request
- end
-
- def set_request_timeout(connection, request, options)
- total = options.timeout.total_timeout
- return unless total
-
- timer = pool.after(total) do
- unless @responses[request]
- error = TotalTimeoutError.new(total, "Timed out after #{total} seconds")
- response = ErrorResponse.new(request, error, options)
- request.emit(:response, response)
- connection.reset
- end
- end
- request.timer = timer
end
@default_options = Options.new
@default_options.freeze
@plugins = []