Skip to content

Commit

Permalink
Replace Async::IO::Endpoint with IO::Endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Apr 23, 2024
1 parent 5029713 commit 2597b21
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 80 deletions.
6 changes: 3 additions & 3 deletions async-http.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ">= 3.1"

spec.add_dependency "async", ">= 1.25"
spec.add_dependency "async-io", ">= 1.43.1"
spec.add_dependency "async", ">= 2.10.2"
spec.add_dependency "async-pool", ">= 0.6.1"
spec.add_dependency "io-stream", "~> 0.1.1"
spec.add_dependency "io-endpoint", "~> 0.10.0"
spec.add_dependency "io-stream", "~> 0.3.0"
spec.add_dependency "protocol-http", "~> 0.26.0"
spec.add_dependency "protocol-http1", "~> 0.19.0"
spec.add_dependency "protocol-http2", "~> 0.17.0"
Expand Down
6 changes: 3 additions & 3 deletions bake/async/http/h2spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2023, by Samuel Williams.
# Copyright, 2019-2024, by Samuel Williams.

def build
# Fetch the code:
Expand All @@ -24,9 +24,9 @@ def server
require 'async'
require 'async/container'
require 'async/http/server'
require 'async/io/host_endpoint'
require 'io/endpoint/host_endpoint'

endpoint = Async::IO::Endpoint.tcp('127.0.0.1', 7272)
endpoint = IO::Endpoint.tcp('127.0.0.1', 7272)

container = Async::Container.new

Expand Down
2 changes: 1 addition & 1 deletion config/external.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async-websocket:
command: bundle exec sus
async-http-faraday:
url: https://github.com/socketry/async-http-faraday.git
command: bundle exec rspec
command: bundle exec bake test
# async-http-cache:
# url: https://github.com/socketry/async-http-cache.git
# command: bundle exec rspec
4 changes: 1 addition & 3 deletions examples/google/codeotaku.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2020-2023, by Samuel Williams.
# Copyright, 2020-2024, by Samuel Williams.

require "async"
require "async/clock"
Expand All @@ -12,8 +12,6 @@
URL = "https://www.codeotaku.com/index"
ENDPOINT = Async::HTTP::Endpoint.parse(URL)

Console.logger.enable(Async::IO::Stream, Console::Logger::DEBUG)

if count = ENV['COUNT']&.to_i
terms = terms.first(count)
end
Expand Down
4 changes: 1 addition & 3 deletions examples/google/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2023, by Samuel Williams.
# Copyright, 2019-2024, by Samuel Williams.

require "async"
require "async/clock"
Expand All @@ -12,8 +12,6 @@
URL = "https://www.google.com/search"
ENDPOINT = Async::HTTP::Endpoint.parse(URL)

# Console.logger.enable(Async::IO::Stream, Console::Logger::DEBUG)

class Google < Protocol::HTTP::Middleware
def search(term)
Console.logger.info(self) {"Searching for #{term}..."}
Expand Down
51 changes: 22 additions & 29 deletions fixtures/async/http/a_protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,6 @@ module HTTP
end

it "disconnects slow clients" do
# We won't be able to disconnect slow clients if IO#timeout is not available:
skip_unless_method_defined(:timeout, IO)

response = client.get("/")
response.read

Expand Down Expand Up @@ -481,8 +478,6 @@ def after
end

it "can't get /" do
skip_unless_method_defined(:timeout, IO)

expect do
client.get("/")
end.to raise_exception(::IO::TimeoutError)
Expand Down Expand Up @@ -536,47 +531,45 @@ def after
end
end

def around
current = Console.logger.level
Console.logger.fatal!

super
ensure
Console.logger.level = current
end

it "doesn't cancel all requests" do
tasks = []
task = Async::Task.current
tasks = []
stopped = []

10.times do
tasks << task.async {
begin
loop do
client.get('http://127.0.0.1:8080/a').finish
end
task.async do |child|
tasks << child

loop do
response = client.get('/a')
response.finish
ensure
stopped << 'a'
response&.close
end
}
ensure
stopped << 'a'
end
end

10.times do
tasks << task.async {
begin
loop do
client.get('http://127.0.0.1:8080/b').finish
end
task.async do |child|
tasks << child

loop do
response = client.get('/b')
response.finish
ensure
stopped << 'b'
response&.close
end
}
ensure
stopped << 'b'
end
end

tasks.each do |child|
sleep 0.01
child.stop
child.wait
end

expect(stopped.sort).to be == stopped
Expand Down
7 changes: 5 additions & 2 deletions gems.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2017-2023, by Samuel Williams.
# Copyright, 2017-2024, by Samuel Williams.

source 'https://rubygems.org'

gemspec

# gem "async", path: "../async"
# gem "async-io", path: "../async-io"
# gem "io-endpoint", path: "../io-endpoint"
# gem "io-stream", path: "../io-stream"
# gem "openssl", git: "https://github.com/ruby/openssl.git"
# gem "traces", path: "../traces"
# gem "sus-fixtures-async-http", path: "../sus-fixtures-async-http"

# gem "protocol-http", path: "../protocol-http"
# gem "protocol-http1", path: "../protocol-http1"
Expand All @@ -29,7 +32,7 @@
gem "covered"
gem "sus"
gem "sus-fixtures-async"
gem "sus-fixtures-async-http", "~> 0.7"
gem "sus-fixtures-async-http", "~> 0.8"
gem "sus-fixtures-openssl"

gem "bake"
Expand Down
8 changes: 4 additions & 4 deletions lib/async/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# Copyright, 2017-2024, by Samuel Williams.
# Copyright, 2022, by Ian Ker-Seymer.

require 'async/io/endpoint'
require 'async/io/stream'
require 'io/endpoint'

require 'async/pool/controller'

Expand Down Expand Up @@ -107,7 +106,6 @@ def call(request)

# This signals that the ensure block below should not try to release the connection, because it's bound into the response which will be returned:
connection = nil

return response
rescue Protocol::RequestFailed
# This is a specific case where the entire request wasn't sent before a failure occurred. So, we can even resend non-idempotent requests.
Expand All @@ -133,7 +131,9 @@ def call(request)
raise
end
ensure
@pool.release(connection) if connection
if connection
@pool.release(connection)
end
end
end

Expand Down
13 changes: 6 additions & 7 deletions lib/async/http/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
# Copyright, 2019-2024, by Samuel Williams.
# Copyright, 2021-2022, by Adam Daniels.

require 'async/io/host_endpoint'
require 'async/io/ssl_endpoint'
require 'async/io/ssl_socket'
require 'async/io/shared_endpoint'
require 'io/endpoint'
require 'io/endpoint/host_endpoint'
require 'io/endpoint/ssl_endpoint'

require_relative 'protocol/http1'
require_relative 'protocol/https'

module Async
module HTTP
# Represents a way to connect to a remote HTTP server.
class Endpoint < Async::IO::Endpoint
class Endpoint < ::IO::Endpoint::Generic
def self.parse(string, endpoint = nil, **options)
url = URI.parse(string).normalize

Expand Down Expand Up @@ -164,7 +163,7 @@ def build_endpoint(endpoint = nil)

if secure?
# Wrap it in SSL:
return Async::IO::SSLEndpoint.new(endpoint,
return ::IO::Endpoint::SSLEndpoint.new(endpoint,
ssl_context: self.ssl_context,
hostname: @url.hostname,
timeout: self.timeout,
Expand Down Expand Up @@ -226,7 +225,7 @@ def tcp_options
end

def tcp_endpoint
Async::IO::Endpoint.tcp(self.hostname, port, **tcp_options)
::IO::Endpoint.tcp(self.hostname, port, **tcp_options)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/async/http/protocol/http1/request.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2023, by Samuel Williams.
# Copyright, 2018-2024, by Samuel Williams.

require_relative '../request'

Expand All @@ -15,7 +15,7 @@ def self.read(connection)
self.new(connection, *parts)
end
end

UPGRADE = 'upgrade'

def initialize(connection, authority, method, path, version, headers, body)
Expand Down
13 changes: 6 additions & 7 deletions lib/async/http/protocol/http2/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,18 @@ def read_in_background(parent: Task.current)
self.consume_window
self.read_frame
end
rescue SocketError, IOError, EOFError, Errno::ECONNRESET, Errno::EPIPE, Async::Wrapper::Cancelled
# Ignore.
rescue ::Protocol::HTTP2::GoawayError => error
rescue Async::Stop, ::IO::TimeoutError, ::Protocol::HTTP2::GoawayError => error
# Error is raised if a response is actively reading from the
# connection. The connection is silently closed if GOAWAY is
# received outside the request/response cycle.
if @reader
self.close(error)
end
rescue SocketError, IOError, EOFError, Errno::ECONNRESET, Errno::EPIPE => ignored_error
# Ignore.
rescue => error
# Every other error.
ensure
# Don't call #close twice.
if @reader
self.close($!)
self.close(error)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/async/http/proxy.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2023, by Samuel Williams.
# Copyright, 2019-2024, by Samuel Williams.

require_relative 'client'
require_relative 'endpoint'
Expand Down Expand Up @@ -46,7 +46,7 @@ def proxied_endpoint(endpoint, headers = nil)
# @param host [String] the hostname or address to connect to.
# @param port [String] the port number to connect to.
# @param headers [Array] an optional list of headers to use when establishing the connection.
# @see Async::IO::Endpoint#tcp
# @see IO::Endpoint#tcp
def self.tcp(client, host, port, headers = nil)
self.new(client, "#{host}:#{port}", headers)
end
Expand Down
3 changes: 1 addition & 2 deletions lib/async/http/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# Copyright, 2017-2024, by Samuel Williams.
# Copyright, 2019, by Brian Morearty.

require 'async/io/endpoint'
require 'async/io/stream'
require 'io/endpoint'

require 'protocol/http/middleware'

Expand Down
7 changes: 4 additions & 3 deletions test/async/http/body.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2023, by Samuel Williams.
# Copyright, 2018-2024, by Samuel Williams.

require 'async/http/body'

require 'sus/fixtures/async'
require 'sus/fixtures/openssl'
require 'sus/fixtures/async/http'
require 'localhost/authority'
require 'io/endpoint/ssl_endpoint'

ABody = Sus::Shared("a body") do
with 'echo server' do
Expand Down Expand Up @@ -97,11 +98,11 @@
let(:client_context) {authority.client_context}

def make_server_endpoint(bound_endpoint)
Async::IO::SSLEndpoint.new(super, ssl_context: server_context)
::IO::Endpoint::SSLEndpoint.new(super, ssl_context: server_context)
end

def make_client_endpoint(bound_endpoint)
Async::IO::SSLEndpoint.new(super, ssl_context: client_context)
::IO::Endpoint::SSLEndpoint.new(super, ssl_context: client_context)
end

it_behaves_like ABody
Expand Down
6 changes: 3 additions & 3 deletions test/async/http/endpoint.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2023, by Samuel Williams.
# Copyright, 2018-2024, by Samuel Williams.
# Copyright, 2021-2022, by Adam Daniels.

require 'async/http/endpoint'
Expand Down Expand Up @@ -36,7 +36,7 @@
end

it "should be connecting to 127.0.0.1" do
expect(subject.endpoint).to be_a Async::IO::SSLEndpoint
expect(subject.endpoint).to be_a ::IO::Endpoint::SSLEndpoint
expect(subject.endpoint).to have_attributes(hostname: be == '127.0.0.1')
expect(subject.endpoint.endpoint).to have_attributes(hostname: be == '127.0.0.1')
end
Expand All @@ -49,7 +49,7 @@
end

it "should be connecting to localhost" do
expect(subject.endpoint).to be_a Async::IO::SSLEndpoint
expect(subject.endpoint).to be_a ::IO::Endpoint::SSLEndpoint
expect(subject.endpoint).to have_attributes(hostname: be == '127.0.0.1')
expect(subject.endpoint.endpoint).to have_attributes(hostname: be == 'localhost')
end
Expand Down
Loading

0 comments on commit 2597b21

Please sign in to comment.