moved all response callbacks to the client, this will enable better plugin overwrites

This commit is contained in:
HoneyryderChuck 2018-01-06 19:24:35 +00:00
parent 284bb06663
commit 85111a36a9
3 changed files with 22 additions and 21 deletions

View File

@ -7,6 +7,7 @@ module HTTPX
def initialize(options = {}) def initialize(options = {})
@default_options = self.class.default_options.merge(options) @default_options = self.class.default_options.merge(options)
@connection = Connection.new(@default_options) @connection = Connection.new(@default_options)
@responses = {}
if block_given? if block_given?
begin begin
@keep_open = true @keep_open = true
@ -33,10 +34,24 @@ module HTTPX
private private
def on_response(request, response)
@responses[request] = response
end
def fetch_response(request)
response = @responses.delete(request)
if response.is_a?(ErrorResponse) && response.retryable?
channel = find_channel(request)
channel.send(request, retries: response.retries - 1)
return
end
response
end
def find_channel(request) def find_channel(request)
uri = URI(request.uri) uri = URI(request.uri)
@connection.find_channel(uri) || @connection.find_channel(uri) ||
@connection.build_channel(uri) @connection.build_channel(uri, &method(:on_response))
end end
def __build_reqs(*args, **options) def __build_reqs(*args, **options)
@ -71,7 +86,7 @@ module HTTPX
# guarantee ordered responses # guarantee ordered responses
loop do loop do
request = requests.shift request = requests.shift
@connection.next_tick until response = @connection.response(request) @connection.next_tick until response = fetch_response(request)
responses << response responses << response

View File

@ -10,7 +10,6 @@ module HTTPX
@timeout = options.timeout @timeout = options.timeout
@selector = Selector.new @selector = Selector.new
@channels = [] @channels = []
@responses = {}
end end
def running? def running?
@ -39,17 +38,8 @@ module HTTPX
end end
end end
def response(request) def build_channel(uri, &on_response)
response = @responses.delete(request) channel = Channel.by(uri, @options, &on_response)
if response.is_a?(ErrorResponse) && response.retryable?
send(request, retries: response.retries - 1)
return
end
response
end
def build_channel(uri)
channel = Channel.by(uri, @options, &method(:on_response))
register_channel(channel) register_channel(channel)
channel channel
end end
@ -66,10 +56,6 @@ module HTTPX
private private
def on_response(request, response)
@responses[request] = response
end
def register_channel(channel) def register_channel(channel)
monitor = @selector.register(channel, :rw) monitor = @selector.register(channel, :rw)
monitor.value = channel monitor.value = channel

View File

@ -53,7 +53,7 @@ module HTTPX
uri = parameters.uri uri = parameters.uri
io = TCP.new(uri.host, uri.port, @default_options) io = TCP.new(uri.host, uri.port, @default_options)
proxy_type = Parameters.registry(parameters.uri.scheme) proxy_type = Parameters.registry(parameters.uri.scheme)
channel = proxy_type.new(io, parameters, @default_options, &@connection.method(:on_response)) channel = proxy_type.new(io, parameters, @default_options, &method(:on_response))
@connection.__send__(:register_channel, channel) @connection.__send__(:register_channel, channel)
channel channel
end end
@ -78,8 +78,8 @@ module HTTPX
end end
class ProxyChannel < Channel class ProxyChannel < Channel
def initialize(io, parameters, options) def initialize(io, parameters, options, &blk)
super(io, options) super(io, options, &blk)
@parameters = parameters @parameters = parameters
@state = :idle @state = :idle
end end