Skip to content

Commit

Permalink
Remove drain buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 1, 2024
1 parent 775c76a commit 0a4b2e2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
50 changes: 29 additions & 21 deletions lib/io/stream/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def initialize(block_size: BLOCK_SIZE, maximum_read_size: MAXIMUM_READ_SIZE)

@read_buffer = StringBuffer.new
@write_buffer = StringBuffer.new
@drain_buffer = StringBuffer.new

# Used as destination buffer for underlying reads.
@input_buffer = StringBuffer.new
Expand Down Expand Up @@ -129,32 +128,37 @@ def gets(separator = $/, **options)
read_until(separator, **options)
end

private def drain(buffer)
begin
syswrite(buffer)
ensure
# If the write operation fails, we still need to clear this buffer, and the data is essentially lost.
buffer.clear
end
end

# Flushes buffered data to the stream.
def flush
return if @write_buffer.empty?

@writing.synchronize do
# Flip the write buffer and drain buffer:
@write_buffer, @drain_buffer = @drain_buffer, @write_buffer

begin
syswrite(@drain_buffer)
ensure
# If the write operation fails, we still need to clear this buffer, and the data is essentially lost.
@drain_buffer.clear
end
self.drain(@write_buffer)
end
end

# Writes `string` to the buffer. When the buffer is full or #sync is true the
# buffer is flushed to the underlying `io`.
# @param string the string to write to the buffer.
# @return the number of bytes appended to the buffer.
def write(string)
@write_buffer << string

if @write_buffer.bytesize >= @block_size
flush
def write(string, flush: false)
@writing.synchronize do
@write_buffer << string

flush |= @write_buffer.bytesize >= @block_size

if flush
self.drain(@write_buffer)
end
end

return string.bytesize
Expand All @@ -168,11 +172,15 @@ def <<(string)
end

def puts(*arguments, separator: $/)
arguments.each do |argument|
@write_buffer << argument << separator
end
return if arguments.empty?

flush
@writing.synchronize do
arguments.each do |argument|
@write_buffer << argument << separator
end

self.drain(@write_buffer)
end
end

def closed?
Expand Down Expand Up @@ -266,13 +274,13 @@ def fill_read_buffer(size = @block_size)

if @read_buffer.empty?
if sysread(size, @read_buffer)
# Console.logger.debug(self, name: "read") {@read_buffer.inspect}
# Console.info(self, name: "read") {@read_buffer.inspect}
return true
end
else
if chunk = sysread(size, @input_buffer)
@read_buffer << chunk
# Console.logger.debug(self, name: "read") {@read_buffer.inspect}
# Console.info(self, name: "read") {@read_buffer.inspect}

return true
end
Expand Down
3 changes: 0 additions & 3 deletions test/io/stream/buffered.rb
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,7 @@ def before
expect(task.wait).to be_a(Errno::EPIPE)

write_buffer = server.instance_variable_get(:@write_buffer)
drain_buffer = server.instance_variable_get(:@drain_buffer)

expect(write_buffer).to be(:empty?)
expect(drain_buffer).to be(:empty?)
end
end
end
Expand Down

0 comments on commit 0a4b2e2

Please sign in to comment.