Skip to content

Replace Async::IO with native IO. #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions async-http.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +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.28"
spec.add_dependency "async", ">= 2.10.2"
spec.add_dependency "async-pool", ">= 0.6.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
48 changes: 23 additions & 25 deletions fixtures/async/http/a_protocol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def after
it "can't get /" do
expect do
client.get("/")
end.to raise_exception(Async::TimeoutError)
end.to raise_exception(::IO::TimeoutError)
end
end

Expand Down Expand Up @@ -531,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
8 changes: 6 additions & 2 deletions gems.rb
Original file line number Diff line number Diff line change
@@ -1,15 +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 @@ -28,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
9 changes: 3 additions & 6 deletions lib/async/http/body/pipe.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# frozen_string_literal: true

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

require 'async/io/socket'
require 'async/io/stream'

require_relative 'writable'

module Async
Expand All @@ -18,9 +15,9 @@ def initialize(input, output = Writable.new, task: Task.current)
@input = input
@output = output

head, tail = IO::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM)
head, tail = ::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM)

@head = IO::Stream.new(head)
@head = ::IO::Stream::Buffered.new(head)
@tail = tail

@reader = nil
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
8 changes: 5 additions & 3 deletions lib/async/http/protocol/http1.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

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

require_relative 'http1/client'
require_relative 'http1/server'

require 'io/stream/buffered'

module Async
module HTTP
module Protocol
Expand All @@ -21,13 +23,13 @@ def self.trailer?
end

def self.client(peer)
stream = IO::Stream.new(peer, sync: true)
stream = ::IO::Stream::Buffered.wrap(peer)

return HTTP1::Client.new(stream, VERSION)
end

def self.server(peer)
stream = IO::Stream.new(peer, sync: true)
stream = ::IO::Stream::Buffered.wrap(peer)

return HTTP1::Server.new(stream, VERSION)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/async/http/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def concurrency

# Can we use this connection to make requests?
def viable?
@ready && @stream&.connected?
@ready && @stream&.readable?
end

def reusable?
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
6 changes: 3 additions & 3 deletions lib/async/http/protocol/http10.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, 2017-2023, by Samuel Williams.
# Copyright, 2017-2024, by Samuel Williams.

require_relative 'http1'

Expand All @@ -20,13 +20,13 @@ def self.trailer?
end

def self.client(peer)
stream = IO::Stream.new(peer, sync: true)
stream = ::IO::Stream::Buffered.wrap(peer)

return HTTP1::Client.new(stream, VERSION)
end

def self.server(peer)
stream = IO::Stream.new(peer, sync: true)
stream = ::IO::Stream::Buffered.wrap(peer)

return HTTP1::Server.new(stream, VERSION)
end
Expand Down
6 changes: 3 additions & 3 deletions lib/async/http/protocol/http11.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, 2017-2023, by Samuel Williams.
# Copyright, 2017-2024, by Samuel Williams.
# Copyright, 2018, by Janko Marohnić.

require_relative 'http1'
Expand All @@ -21,13 +21,13 @@ def self.trailer?
end

def self.client(peer)
stream = IO::Stream.new(peer, sync: true)
stream = ::IO::Stream::Buffered.wrap(peer)

return HTTP1::Client.new(stream, VERSION)
end

def self.server(peer)
stream = IO::Stream.new(peer, sync: true)
stream = ::IO::Stream::Buffered.wrap(peer)

return HTTP1::Server.new(stream, VERSION)
end
Expand Down
Loading
Loading