changed the concept of an empty http1 channel

This commit is contained in:
HoneyryderChuck 2017-12-06 18:24:43 +00:00
parent ee59c71274
commit 3ff56ceb09
2 changed files with 45 additions and 39 deletions

View File

@ -27,7 +27,9 @@ module HTTPX
alias :close :reset
def empty?
@requests.empty?
# this means that for every request there's an available
# partial response, so there are no in-flight requests waiting.
@requests.size == @responses.size
end
def <<(data)

View File

@ -20,29 +20,6 @@ module HTTPX
!@channels.empty?
end
# opens a channel to the IP reachable through +uri+.
# Many hostnames are reachable through the same IP, so we try to
# maximize pipelining by opening as few channels as possible.
#
def bind(uri)
uri = URI(uri)
ip = TCPSocket.getaddress(uri.host)
return @channels.find do |channel|
ip == channel.remote_ip &&
uri.port == channel.remote_port &&
uri.scheme == channel.uri.scheme
end || begin
channel = Channel.by(self, uri, @options) do |request, response|
@responses[request] = response
end
@channels << channel
monitor = @selector.register(channel, :rw)
monitor.value = channel
channel
end
end
def send(request, **args)
channel = bind(request.uri)
raise Error, "no channel available" unless channel
@ -51,21 +28,6 @@ module HTTPX
end
alias :<< :send
def response(request)
response = @responses.delete(request)
case response
when ErrorResponse
if response.retryable?
send(request, retries: response.retries - 1)
nil
else
response
end
else
response
end
end
def next_tick(timeout: @timeout.timeout)
@selector.select(timeout) do |monitor|
if task = monitor.value
@ -89,5 +51,47 @@ module HTTPX
end
end
end
def response(request)
response = @responses.delete(request)
case response
when ErrorResponse
if response.retryable?
send(request, retries: response.retries - 1)
nil
else
response
end
else
response
end
end
private
# opens a channel to the IP reachable through +uri+.
# Many hostnames are reachable through the same IP, so we try to
# maximize pipelining by opening as few channels as possible.
#
def bind(uri)
uri = URI(uri)
ip = TCPSocket.getaddress(uri.host)
return @channels.find do |channel|
ip == channel.remote_ip &&
uri.port == channel.remote_port &&
uri.scheme == channel.uri.scheme
end || begin
channel = Channel.by(self, uri, @options) do |request, response|
@responses[request] = response
end
@channels << channel
monitor = @selector.register(channel, :rw)
monitor.value = channel
channel
end
end
end
end