Skip to content

Commit 4ce40b4

Browse files
authored
Introduce Body#discard for efficiently ignoring body. (#69)
1 parent 082cc58 commit 4ce40b4

File tree

8 files changed

+57
-0
lines changed

8 files changed

+57
-0
lines changed

lib/protocol/http/body/buffered.rb

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ def finish
5959
# Ensure that future reads return nil, but allow for rewinding.
6060
def close(error = nil)
6161
@index = @chunks.length
62+
63+
return nil
6264
end
6365

6466
def clear
@@ -90,6 +92,10 @@ def read
9092
end
9193
end
9294

95+
def discard
96+
self.close
97+
end
98+
9399
def write(chunk)
94100
@chunks << chunk
95101
end

lib/protocol/http/body/readable.rb

+10
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ def finish
133133
Buffered.read(self)
134134
end
135135

136+
# Discard the body as efficiently as possible.
137+
#
138+
# The default implementation simply reads all chunks until the body is empty.
139+
#
140+
# Useful for discarding the body when it is not needed, but preserving the underlying connection.
141+
def discard
142+
while chunk = self.read
143+
end
144+
end
145+
136146
def as_json(...)
137147
{
138148
class: self.class.name,

lib/protocol/http/body/reader.rb

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ def finish
4040
end
4141
end
4242

43+
# Discard the body as efficiently as possible.
44+
def discard
45+
if body = @body
46+
@body = nil
47+
body.discard
48+
end
49+
end
50+
4351
# Buffer the entire request/response body.
4452
# @returns [Reader] itself.
4553
def buffered!

lib/protocol/http/body/wrapper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ def read
5959
@body.read
6060
end
6161

62+
def discard
63+
@body.discard
64+
end
65+
6266
def as_json(...)
6367
{
6468
class: self.class.name,

test/protocol/http/body/buffered.rb

+8
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,12 @@
175175
expect(body.inspect).to be =~ /\d+ chunks, \d+ bytes/
176176
end
177177
end
178+
179+
with "#discard" do
180+
it "closes the body" do
181+
expect(body).to receive(:close)
182+
183+
expect(body.discard).to be == nil
184+
end
185+
end
178186
end

test/protocol/http/body/readable.rb

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@
5858
end
5959
end
6060

61+
with "#discard" do
62+
it "should read all chunks" do
63+
expect(body).to receive(:read).and_return(nil)
64+
expect(body.discard).to be_nil
65+
end
66+
end
67+
6168
with "#as_json" do
6269
it "generates a JSON representation" do
6370
expect(body.as_json).to have_keys(

test/protocol/http/body/reader.rb

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ def initialize(body)
2929
end
3030
end
3131

32+
with "#discard" do
33+
it 'discards the body' do
34+
expect(body).to receive(:discard)
35+
expect(reader.discard).to be_nil
36+
end
37+
end
38+
3239
with '#buffered!' do
3340
it 'buffers the body' do
3441
expect(reader.buffered!).to be_equal(reader)

test/protocol/http/body/wrapper.rb

+7
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,11 @@
104104
body.call(stream)
105105
end
106106
end
107+
108+
with "#discard" do
109+
it "should proxy discard" do
110+
expect(source).to receive(:discard).and_return(nil)
111+
expect(body.discard).to be_nil
112+
end
113+
end
107114
end

0 commit comments

Comments
 (0)