Skip to content

Commit ed10818

Browse files
committed
WIP queue
1 parent 079cdd2 commit ed10818

File tree

6 files changed

+204
-120
lines changed

6 files changed

+204
-120
lines changed

examples/streaming/bidirectional.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
while chunk = stream.readpartial(1024)
2323
$stderr.puts "Server chunk: #{chunk.inspect}"
2424
stream.write(chunk)
25+
$stderr.puts "Server waiting for next chunk..."
2526
end
27+
$stderr.puts "Server done reading request."
2628
rescue EOFError
2729
$stderr.puts "Server EOF."
2830
# Ignore EOF errors.
@@ -56,7 +58,7 @@
5658
$stderr.puts "Client EOF."
5759
# Ignore EOF errors.
5860
ensure
59-
$stderr.puts "Client closing stream."
61+
$stderr.puts "Client closing stream: #{$!}"
6062
stream.close
6163
end
6264

examples/streaming/gems.locked

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
PATH
2+
remote: ../../../async-http
3+
specs:
4+
async-http (0.75.0)
5+
async (>= 2.10.2)
6+
async-pool (~> 0.7)
7+
io-endpoint (~> 0.11)
8+
io-stream (~> 0.4)
9+
protocol-http (~> 0.33)
10+
protocol-http1 (~> 0.20)
11+
protocol-http2 (~> 0.18)
12+
traces (>= 0.10)
13+
114
PATH
215
remote: ../..
316
specs:
@@ -10,15 +23,6 @@ GEM
1023
console (~> 1.26)
1124
fiber-annotation
1225
io-event (~> 1.6, >= 1.6.5)
13-
async-http (0.75.0)
14-
async (>= 2.10.2)
15-
async-pool (~> 0.7)
16-
io-endpoint (~> 0.11)
17-
io-stream (~> 0.4)
18-
protocol-http (~> 0.30)
19-
protocol-http1 (~> 0.20)
20-
protocol-http2 (~> 0.18)
21-
traces (>= 0.10)
2226
async-pool (0.8.1)
2327
async (>= 1.25)
2428
metrics
@@ -64,7 +68,7 @@ PLATFORMS
6468

6569
DEPENDENCIES
6670
async
67-
async-http
71+
async-http!
6872
debug
6973
protocol-http!
7074

examples/streaming/gems.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
source "https://rubygems.org"
77

88
gem "async"
9-
gem "async-http"
9+
gem "async-http", path: "../../../async-http"
1010
gem "protocol-http", path: "../../"
1111

1212
gem "debug"

examples/streaming/simple.rb

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
# Released under the MIT License.
5+
# Copyright, 2024, by Samuel Williams.
6+
7+
require 'async'
8+
require 'async/http/client'
9+
require 'async/http/server'
10+
require 'async/http/endpoint'
11+
12+
require 'protocol/http/body/streamable'
13+
require 'protocol/http/body/writable'
14+
require 'protocol/http/body/stream'
15+
16+
endpoint = Async::HTTP::Endpoint.parse('http://localhost:3000')
17+
18+
Async do
19+
server = Async::HTTP::Server.for(endpoint) do |request|
20+
output = Protocol::HTTP::Body::Streamable.response(request) do |stream|
21+
$stderr.puts "Server sending text..."
22+
stream.write("Hello from server!")
23+
rescue EOFError
24+
$stderr.puts "Server EOF."
25+
# Ignore EOF errors.
26+
ensure
27+
$stderr.puts "Server closing stream."
28+
stream.close
29+
end
30+
31+
Protocol::HTTP::Response[200, {}, output]
32+
end
33+
34+
server_task = Async{server.run}
35+
36+
client = Async::HTTP::Client.new(endpoint)
37+
38+
streamable = Protocol::HTTP::Body::Streamable.request do |stream|
39+
while chunk = stream.readpartial(1024)
40+
$stderr.puts "Client chunk: #{chunk.inspect}"
41+
end
42+
rescue EOFError
43+
$stderr.puts "Client EOF."
44+
# Ignore EOF errors.
45+
ensure
46+
$stderr.puts "Client closing stream."
47+
stream.close
48+
end
49+
50+
$stderr.puts "Client sending request..."
51+
response = client.get("/", body: streamable)
52+
$stderr.puts "Client received response and streaming it..."
53+
streamable.stream(response.body)
54+
ensure
55+
server_task.stop
56+
end

gems.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@
2626
gem "bake-test-external"
2727
end
2828

29-
gem "debug", ">= 1.0.0"
29+
gem "async-http", path: "../async-http"

0 commit comments

Comments
 (0)