Skip to content

Commit 0ebae08

Browse files
committed
Support io-endpoint gem.
1 parent f681cd4 commit 0ebae08

File tree

23 files changed

+91
-87
lines changed

23 files changed

+91
-87
lines changed

async-http.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Gem::Specification.new do |spec|
2020
spec.required_ruby_version = ">= 3.0"
2121

2222
spec.add_dependency "async", ">= 1.25"
23-
spec.add_dependency "async-io", ">= 1.28"
2423
spec.add_dependency "async-pool", ">= 0.2"
24+
spec.add_dependency "io-endpoint", "~> 0.6"
2525
spec.add_dependency "protocol-http", "~> 0.26.0"
2626
spec.add_dependency "protocol-http1", "~> 0.18.0"
2727
spec.add_dependency "protocol-http2", "~> 0.16.0"

examples/google/codeotaku.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
URL = "https://www.codeotaku.com/index"
1313
ENDPOINT = Async::HTTP::Endpoint.parse(URL)
1414

15-
Console.logger.enable(Async::IO::Stream, Console::Logger::DEBUG)
16-
1715
if count = ENV['COUNT']&.to_i
1816
terms = terms.first(count)
1917
end

examples/google/search.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
URL = "https://www.google.com/search"
1313
ENDPOINT = Async::HTTP::Endpoint.parse(URL)
1414

15-
# Console.logger.enable(Async::IO::Stream, Console::Logger::DEBUG)
16-
1715
class Google < Protocol::HTTP::Middleware
1816
def search(term)
1917
Console.logger.info(self) {"Searching for #{term}..."}

fixtures/async/http/a_protocol.rb

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,10 @@ def after
481481

482482
it "can't get /" do
483483
expect do
484-
client.get("/")
485-
end.to raise_exception(Async::TimeoutError)
484+
Console.debug(self) {"Connecting to #{endpoint.inspect}"}
485+
response = client.get("/")
486+
Console.debug(self) {"Got response #{response.inspect}"}
487+
end.to raise_exception(::IO::TimeoutError)
486488
end
487489
end
488490

@@ -542,33 +544,41 @@ def around
542544
Console.logger.level = current
543545
end
544546

547+
def timeout = nil
548+
545549
it "doesn't cancel all requests" do
546-
tasks = []
547550
task = Async::Task.current
551+
tasks = []
548552
stopped = []
549553

550554
10.times do
551-
tasks << task.async {
552-
begin
553-
loop do
554-
client.get('http://127.0.0.1:8080/a').finish
555-
end
555+
task.async do |child|
556+
tasks << child
557+
558+
loop do
559+
response = client.get('/a')
560+
response.finish
556561
ensure
557-
stopped << 'a'
562+
response&.close
558563
end
559-
}
564+
ensure
565+
stopped << 'a'
566+
end
560567
end
561568

562569
10.times do
563-
tasks << task.async {
564-
begin
565-
loop do
566-
client.get('http://127.0.0.1:8080/b').finish
567-
end
570+
task.async do |child|
571+
tasks << child
572+
573+
loop do
574+
response = client.get('/b')
575+
response.finish
568576
ensure
569-
stopped << 'b'
577+
response&.close
570578
end
571-
}
579+
ensure
580+
stopped << 'b'
581+
end
572582
end
573583

574584
tasks.each do |child|

gems.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
gemspec
99

1010
# gem "async", path: "../async"
11-
# gem "async-io", path: "../async-io"
1211
# gem "traces", path: "../traces"
12+
gem "io-endpoint", path: "../io-endpoint"
13+
# gem "sus-fixtures-async-http", path: "../sus-fixtures-async-http"
1314

1415
# gem "protocol-http", path: "../protocol-http"
1516
# gem "protocol-http1", path: "../protocol-http1"
@@ -28,7 +29,7 @@
2829
gem "covered"
2930
gem "sus"
3031
gem "sus-fixtures-async"
31-
gem "sus-fixtures-async-http", "~> 0.7"
32+
gem "sus-fixtures-async-http", "~> 0.8"
3233
gem "sus-fixtures-openssl"
3334

3435
gem "bake"

lib/async/http/body/pipe.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ def initialize(input, output = Writable.new, task: Task.current)
1818
@input = input
1919
@output = output
2020

21-
head, tail = IO::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM)
22-
23-
@head = IO::Stream.new(head)
24-
@tail = tail
21+
@head, @tail = ::Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM)
2522

2623
@reader = nil
2724
@writer = nil
@@ -68,7 +65,7 @@ def writer(task)
6865

6966
task.annotate "#{self.class} writer."
7067

71-
while chunk = @head.read_partial
68+
while chunk = @head.readpartial(1024)
7269
@output.write(chunk)
7370
end
7471
ensure

lib/async/http/client.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ def call(request)
9393

9494
# 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:
9595
connection = nil
96-
9796
return response
9897
rescue Protocol::RequestFailed
9998
# This is a specific case where the entire request wasn't sent before a failure occurred. So, we can even resend non-idempotent requests.
@@ -119,7 +118,9 @@ def call(request)
119118
raise
120119
end
121120
ensure
122-
@pool.release(connection) if connection
121+
if connection
122+
@pool.release(connection)
123+
end
123124
end
124125
end
125126

lib/async/http/endpoint.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@
44
# Copyright, 2019-2023, by Samuel Williams.
55
# Copyright, 2021-2022, by Adam Daniels.
66

7-
require 'async/io/host_endpoint'
8-
require 'async/io/ssl_endpoint'
9-
require 'async/io/ssl_socket'
10-
require 'async/io/shared_endpoint'
7+
require 'io/endpoint'
8+
require 'io/endpoint/host_endpoint'
9+
require 'io/endpoint/ssl_endpoint'
10+
require 'io/endpoint/shared_endpoint'
1111

1212
require_relative 'protocol/http1'
1313
require_relative 'protocol/https'
1414

1515
module Async
1616
module HTTP
1717
# Represents a way to connect to a remote HTTP server.
18-
class Endpoint < Async::IO::Endpoint
18+
class Endpoint < ::IO::Endpoint::Generic
1919
def self.parse(string, endpoint = nil, **options)
2020
url = URI.parse(string).normalize
2121

@@ -164,7 +164,7 @@ def build_endpoint(endpoint = nil)
164164

165165
if secure?
166166
# Wrap it in SSL:
167-
return Async::IO::SSLEndpoint.new(endpoint,
167+
return ::IO::Endpoint::SSLEndpoint.new(endpoint,
168168
ssl_context: self.ssl_context,
169169
hostname: @url.hostname,
170170
timeout: self.timeout,
@@ -226,7 +226,7 @@ def tcp_options
226226
end
227227

228228
def tcp_endpoint
229-
Async::IO::Endpoint.tcp(self.hostname, port, **tcp_options)
229+
::IO::Endpoint.tcp(self.hostname, port, **tcp_options)
230230
end
231231
end
232232
end

lib/async/http/protocol/http1.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,11 @@ def self.trailer?
2121
end
2222

2323
def self.client(peer)
24-
stream = IO::Stream.new(peer, sync: true)
25-
26-
return HTTP1::Client.new(stream, VERSION)
24+
return HTTP1::Client.new(peer, VERSION)
2725
end
2826

2927
def self.server(peer)
30-
stream = IO::Stream.new(peer, sync: true)
31-
32-
return HTTP1::Server.new(stream, VERSION)
28+
return HTTP1::Server.new(peer, VERSION)
3329
end
3430

3531
def self.names

lib/async/http/protocol/http1/client.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def call(request, task: Task.current)
5959
write_body(@version, body, false, trailer)
6060
end
6161

62+
Console.logger.debug(self) {"Waiting for response..."}
6263
response = Response.read(self, request)
6364
@ready = true
6465

0 commit comments

Comments
 (0)