Skip to content

Commit de35b62

Browse files
committed
Async::HTTP::Body::Writable is moved to Protocol::HTTP::Body::Writable.
1 parent f9cef46 commit de35b62

File tree

2 files changed

+3
-94
lines changed

2 files changed

+3
-94
lines changed

async-http.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
2828
spec.add_dependency "async-pool", "~> 0.7"
2929
spec.add_dependency "io-endpoint", "~> 0.11"
3030
spec.add_dependency "io-stream", "~> 0.4"
31-
spec.add_dependency "protocol-http", "~> 0.30"
31+
spec.add_dependency "protocol-http", "~> 0.33"
3232
spec.add_dependency "protocol-http1", "~> 0.20"
3333
spec.add_dependency "protocol-http2", "~> 0.18"
3434
spec.add_dependency "traces", ">= 0.10"

lib/async/http/body/writable.rb

+2-93
Original file line numberDiff line numberDiff line change
@@ -3,104 +3,13 @@
33
# Released under the MIT License.
44
# Copyright, 2018-2023, by Samuel Williams.
55

6-
require 'protocol/http/body/readable'
6+
require 'protocol/http/body/writable'
77
require 'async/queue'
88

99
module Async
1010
module HTTP
1111
module Body
12-
include ::Protocol::HTTP::Body
13-
14-
# A dynamic body which you can write to and read from.
15-
class Writable < Readable
16-
class Closed < StandardError
17-
end
18-
19-
# @param [Integer] length The length of the response body if known.
20-
# @param [Async::Queue] queue Specify a different queue implementation, e.g. `Async::LimitedQueue.new(8)` to enable back-pressure streaming.
21-
def initialize(length = nil, queue: Async::Queue.new)
22-
@queue = queue
23-
24-
@length = length
25-
26-
@count = 0
27-
28-
@finished = false
29-
30-
@closed = false
31-
@error = nil
32-
end
33-
34-
def length
35-
@length
36-
end
37-
38-
# Stop generating output; cause the next call to write to fail with the given error.
39-
def close(error = nil)
40-
unless @closed
41-
@queue.enqueue(nil)
42-
43-
@closed = true
44-
@error = error
45-
end
46-
47-
super
48-
end
49-
50-
def closed?
51-
@closed
52-
end
53-
54-
def ready?
55-
!@queue.empty?
56-
end
57-
58-
# Has the producer called #finish and has the reader consumed the nil token?
59-
def empty?
60-
@finished
61-
end
62-
63-
# Read the next available chunk.
64-
def read
65-
return if @finished
66-
67-
unless chunk = @queue.dequeue
68-
@finished = true
69-
end
70-
71-
return chunk
72-
end
73-
74-
# Write a single chunk to the body. Signal completion by calling `#finish`.
75-
def write(chunk)
76-
# If the reader breaks, the writer will break.
77-
# The inverse of this is less obvious (*)
78-
if @closed
79-
raise(@error || Closed)
80-
end
81-
82-
@count += 1
83-
@queue.enqueue(chunk)
84-
end
85-
86-
alias << write
87-
88-
def inspect
89-
"\#<#{self.class} #{@count} chunks written, #{status}>"
90-
end
91-
92-
private
93-
94-
def status
95-
if @finished
96-
'finished'
97-
elsif @closed
98-
'closing'
99-
else
100-
'waiting'
101-
end
102-
end
103-
end
12+
Writable = ::Protocol::HTTP::Body::Writable
10413
end
10514
end
10615
end

0 commit comments

Comments
 (0)