Skip to content

Commit

Permalink
Introduce Body#discard for efficiently ignoring body. (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Sep 17, 2024
1 parent 082cc58 commit 4ce40b4
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/protocol/http/body/buffered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def finish
# Ensure that future reads return nil, but allow for rewinding.
def close(error = nil)
@index = @chunks.length

return nil
end

def clear
Expand Down Expand Up @@ -90,6 +92,10 @@ def read
end
end

def discard
self.close
end

def write(chunk)
@chunks << chunk
end
Expand Down
10 changes: 10 additions & 0 deletions lib/protocol/http/body/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ def finish
Buffered.read(self)
end

# Discard the body as efficiently as possible.
#
# The default implementation simply reads all chunks until the body is empty.
#
# Useful for discarding the body when it is not needed, but preserving the underlying connection.
def discard
while chunk = self.read
end
end

def as_json(...)
{
class: self.class.name,
Expand Down
8 changes: 8 additions & 0 deletions lib/protocol/http/body/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def finish
end
end

# Discard the body as efficiently as possible.
def discard
if body = @body
@body = nil
body.discard
end
end

# Buffer the entire request/response body.
# @returns [Reader] itself.
def buffered!
Expand Down
4 changes: 4 additions & 0 deletions lib/protocol/http/body/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def read
@body.read
end

def discard
@body.discard
end

def as_json(...)
{
class: self.class.name,
Expand Down
8 changes: 8 additions & 0 deletions test/protocol/http/body/buffered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,12 @@
expect(body.inspect).to be =~ /\d+ chunks, \d+ bytes/
end
end

with "#discard" do
it "closes the body" do
expect(body).to receive(:close)

expect(body.discard).to be == nil
end
end
end
7 changes: 7 additions & 0 deletions test/protocol/http/body/readable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@
end
end

with "#discard" do
it "should read all chunks" do
expect(body).to receive(:read).and_return(nil)
expect(body.discard).to be_nil
end
end

with "#as_json" do
it "generates a JSON representation" do
expect(body.as_json).to have_keys(
Expand Down
7 changes: 7 additions & 0 deletions test/protocol/http/body/reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def initialize(body)
end
end

with "#discard" do
it 'discards the body' do
expect(body).to receive(:discard)
expect(reader.discard).to be_nil
end
end

with '#buffered!' do
it 'buffers the body' do
expect(reader.buffered!).to be_equal(reader)
Expand Down
7 changes: 7 additions & 0 deletions test/protocol/http/body/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,11 @@
body.call(stream)
end
end

with "#discard" do
it "should proxy discard" do
expect(source).to receive(:discard).and_return(nil)
expect(body.discard).to be_nil
end
end
end

0 comments on commit 4ce40b4

Please sign in to comment.